Цель: Android / iOS приложение, которое отображает непростые слова, когда пользователь вводит букву.
Код запускается правильно с первого раза, но после запроса пользователя, хотят ли они ввести другая буква, список слов из файла отображается несколько раз. Количество раз, когда распечатывается список, увеличивается, например, 1,2,3 ...
Ожидаемый результат: список слов для одной буквы будет напечатан один раз
Фактический результат: список слов печатается более одного раза при каждом цикле выполнения программы. Увеличивается с помощью переменной i.
Сообщения об ошибках: нет
Попытка:
Изменение printWords () с использованием декремента.
if (i == 2) {i--; }
https://www.youtube.com/watch?v=Iho2EdJgusQ
- Почему ответ печатается дважды?
- http://www.cplusplus.com/forum/beginner/199381/
- https://codereview.stackexchange.com/questions/139841/printing-out-vectorstring-char-by-char
- Изменено время выполнения на while
Код:
/*
Description: Android/iOS application that takes in a letter and displays
tricky-to-spell words.
*/
#include "includeDeclarations.h"
#include "usingDeclarations.h"
char userLetterInput;
char userChoiceContinue;
string userFirstName;
string line;
vector <string> trickyWordsVector;
void printWords();
int main() {
cout << "----------------<>-----------\n";
cout << "Welcome to Your TRICKY WORDS Helper!\n";
cout << "----------------<>-----------\n";
cout << "\n\nEnter your first name: ";
cin >> userFirstName;
do {
cout << "\nEnter a letter [a - z]: ";
cin >> userLetterInput;
userLetterInput = toupper(userLetterInput);
if(isalpha(userLetterInput)){
cout << "\n" << userFirstName << ",\n\nHere's your list of tricky words for the letter (" << char(toupper(userLetterInput)) << "):\n" << endl;
ifstream trickyWordsFile("trickyWordsList.txt");
if (trickyWordsFile.is_open()) {
while (getline(trickyWordsFile, line)) {
if (line.size() > 0) {
trickyWordsVector.push_back(line);
}
}
} else {
cerr << "Cannot open the file.";
}
trickyWordsFile.close();
printWords();
}
cout << "\nWould you like to enter another letter [y,n]?: "; //TODO data validation
cin >> userChoiceContinue;
} while(char(tolower(userChoiceContinue)) == 'y');
cout << "\n----------------<>-----------\n";
cout << "Thank you for using Your TRICKY WORDS Helper!\n";
cout << "----------------<>-----------\n";
return 0;
} // end main()
void printWords(){
//TODO error prints list more than once.
for (int i = 0; i < trickyWordsVector.size(); i++) {
if(trickyWordsVector[i][0] == userLetterInput){
cout << trickyWordsVector[i];
cout << "\n";
}
}
} // end printWords()
usingDeclarations.h
#define cout std::cout
#define cin std::cin
#define getline std::getline
#define endl std::endl
#define string std::string
#define ifstream std::ifstream
#define cerr std::cerr
#define vector std::vector
includeDeclarations.h
#include <iostream>
#include <fstream>
#include <locale>
#include <string>
#include <vector>
trickyWordsList.txt или trickyWordsFile
Argument
Atheist
Axle
Bellwether
Broccoli
Bureau
Caribbean
Calendar
Camaraderie
Desiccate
Desperate
Deterrence
Спасибо за любой совет.
Выполнить код с помощью https://repl.it/~