Guessing game - when I guess, the guess that I make doesn't check correct even though...

70.2K

Verified Solution

Question

Programming

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.

Answer & Explanation Solved by verified expert
4.5 Ratings (674 Votes)
In guessMess function you are usingcin guessWord to read word from userBut    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students