Guessing game - when I guess, the guess that I make doesn'tcheck correct even though it is correct.
// use external file:
line1
linetwo
line three
linefour
line five
// end external file.
// begin program:
#include
#include
#include
#include
#include
#include
using namespace std;
string trimWSFuntion(string);
void guessMess(string, string);
int main()
{
//to store 3 lines
string lineFromWordtxt1;
string lineFromWordtxt2;
string lineToTrim;
ifstream myFile;
string read_file_name;
// this block gets the filename
cout << \"File? \";
cin >> read_file_name;
cout << endl;
myFile.open(read_file_name.c_str());
// need to read the first line only once read first line andskip it
getline(myFile, lineFromWordtxt1);
// read second line and add to variable
getline(myFile, lineFromWordtxt2);
getline(myFile, lineToTrim);
string trimmedLine = trimWSFuntion(lineToTrim);
guessMess(trimmedLine, lineFromWordtxt2);
myFile.close();
}
string trimWSFuntion(string u)
{
char c;
string f = \"\";
// trim all white space at beggining. \"Example sentence \"
for (int i = 0; i < u.length(); ++i)
{
// removing all the white space
c = u.at(i);
if (!isspace(c) /*== false*/)
{
f = u.substr(i, u.length());
// stop trimming white space and assign to the string: \"Examplesentence \"
break;
}
}
u = f; // u \"Example sentence
// trim all white space at the end: \"Example sentence\";
for (int j = (u.length() - 1); j > 0; --j)
{
c = u.at(j);
if (!isspace(c)/* == false*/)
{
f = u.substr(0, j + 1);
// stop trimming white space and assign to the string: \"Examplesentence \"
break;
}
}
return f;
}
void guessMess(string w, string v)
{
string guessWord = \"\";
int counter = 0;
while ((guessWord != w) && (counter < 3))
{
cout << v << \"?\" << endl;
cout << w << endl;
cin >> guessWord;
counter++;
if (guessWord == w)
{
cout << \"Yay\" << endl;
}
else if (counter == 3)
{
cout << endl;
cout << \"Sorry, it's \" << w << endl;
}
else if (counter < 3)
{
cout << \"Try again\" << endl;
}
}
}
// end program.