In: Computer Science
Guessing game - when I guess, the guess that I make doesn't check correct even though it is correct.
// use external file:
line1
linetwo
line three
linefour
line five
// end external file.
// begin program:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <time.h>
#include <stdlib.h>
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 and
skip 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: "Example
sentence "
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: "Example
sentence "
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.
In guessMess() function, you are using cin >> guessWord to read word from user.
But cin only reads input upto first whitespace.
So if you are entering "line three", cin will read only "three".
To read input strings with spaces in them, use getline(cin, guessWord).
Below is the edited guessMess function:
void guessMess(string w, string v)
{
string guessWord = "";
int counter = 0;
while ((guessWord != w) && (counter < 3))
{
cout << v << "?" << endl;
cout << w << endl;
getchar();
getline(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;
}
}
}
Also use getchar() just before calling guessMess() function from main() method. This will consume the newline character in input stream.
Output:
If you have any problem regarding to the solution, tell me in comments.
if it helps you, do upvote as it motivates us a lot!
getchar();