In: Computer Science
Simple Flashcard game
There is an error with the trimWSFuntion - it should remove the leading and trailing whitespaces and then return the string. It will intake "line two" but will only output 'o'. I need help to know why the whole string is not output.
begin txt file :
786
lineone
line two
begin program :
#include
#include
#include
#include
using namespace std;
string trimFunction(string);
void guessFunction(string, string);
string lowercaseFuntion(string);
int main()
{
string firstLine;
string secondLine;
string trimLine;
ifstream myFile;
string read_file;
cout << "File? ";
cin >> read_file;
cin.ignore(100, '\n');
cout << endl;
myFile.open(read_file.c_str());
getline(myFile, firstLine);
getline(myFile, secondLine);
getline(myFile, trimLine);
string trimmedLine = trimFunction(trimLine);
guessFunction(trimmedLine, secondLine);
myFile.close();
}
string trimFunction(string u)
{
char c;
string f = "";
int i = 0;
c = u.at(i);
while (!isspace(c) && i < u.length())
{
f = u.substr(i, u.length());
i++;
}
u = f;
int j = (u.length() - 1);
c = u.at(j);
while (!isspace(c) && j > 0)
{
f = u.substr(0, j + 1);
j--;
}
return f;
}
void guessFunction(string w, string v)
{
string guess = "";
int i = 1;
while (guess != w && i <= 3)
{
cout << v << "? "
<< endl;
getline(cin, guess);
guess =
trimFunction(guess);
guess =
lowercaseFuntion(guess);
if (i == 3)
{
cout <<
"Sorry, it's " << w << endl;
}
else if (guess == w)
{
cout <<
"Yay" << endl;
}
else
{
cout <<
"Try again" << endl;
}
i++;
}
}
string lowercaseFuntion(string t)
{
for (int j = 0; t[j]; j++)
{
t[j] = tolower(t[j]);
}
return t;
}