Question

In: Computer Science

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

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.

Solutions

Expert Solution

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();


Related Solutions

guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
This is for java For my assignment, I was supposed to make a number guessing game....
This is for java For my assignment, I was supposed to make a number guessing game. The class, HiLo, should pick a random number between 1 and 100 (inclusive). Then, prompt the user to guess the number. On each guess, print if the user is correct or if the guess is too high or too low. Allow only 5 guesses. In the end, print out the correct answer and how many attempts were made. Then give the user the option...
Number guessing Game (20 Marks) Write a C program that implements the “guess my number” game....
Number guessing Game Write a C program that implements the “guess my number” game. The computer chooses a random number using the following random generator function srand(time(NULL)); int r = rand() % 100 + 1; that creates a random number between 1 and 100 and puts it in the variable r. (Note that you have to include <time.h>) Then it asks the user to make a guess. Each time the user makes a guess, the program tells the user if...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1...
Project 5-3: Guessing Game Create an application that lets a user guess a number between 1 and 100. Console Welcome to the Guess the Number Game ++++++++++++++++++++++++++++++++++++ I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 You got it in 1 tries. Great work! You are a mathematical wizard. Try again? (y/n): y I'm thinking of a number from 1 to 100. Try to guess it. Enter number: 50 Way too high! Guess...
I need to write PYTHON program which is like a guessing game using randint function which...
I need to write PYTHON program which is like a guessing game using randint function which is already done, the user has to guess a number between 1 and 1000 and the game musn't end until you guess the number, if you input a smaller number you must get hints, the same goes if your input is bigger than the wining number , also you must get notified if you repeat a number (example, you pick 1 and in the...
10. Why does it make sense to study personality, even though correlations between measured personality and...
10. Why does it make sense to study personality, even though correlations between measured personality and behavior in specific situations are often quite low?
How efficient is an electric heater? My guess: greater than 95%. Possibly even 99%. I say...
How efficient is an electric heater? My guess: greater than 95%. Possibly even 99%. I say this because most energy is converted into heat; some is converted into light and kinetic energy, and possibly other forms of energy. Anyone other opinions? (This is not homework. I am just curious and I'm having a discussion with a friend who says an electric heater is horribly inefficient, less than 5%.)
a. Consider this statement: "Quickbooks Accountant records revenue when an invoice is generated even though cash...
a. Consider this statement: "Quickbooks Accountant records revenue when an invoice is generated even though cash has not been received." Is this practice acceptable? Why or why not? b. When you've finished reconciling a bank account, what should be the difference between the ending balance and the cleared balance? c. What information is included in the Reconciliation Summary report? d. Explain the typical relationship between sales and cost of goods sold, and describe how this information is included in the...
I will not give credit for any unsupported answer -- even if it is correct!!. All...
I will not give credit for any unsupported answer -- even if it is correct!!. All cash flows are end-of-period unless otherwise stated . Problems 1 and 2 relate to this information : The current monthly income statement for JRT, Inc., represents the results of selling 8,000 coffee mugs: Sales (revenue) $56,000 Cost of goods sold (33,000) Gross margin $23,000 S & A expenses (16,000) Income before taxes $ 7,000 Income taxes (@ 30%) (2,100) Net income $ 4,900 Cost...
Even though most corporate bonds in the United States make coupon payments semiannually, bonds issued elsewhere...
Even though most corporate bonds in the United States make coupon payments semiannually, bonds issued elsewhere often have annual coupon payments. Suppose a German company issues a bond with a par value of €1,000, 14 years to maturity, and a coupon rate of 8 percent paid annuallly. If the yield to maturity is 11 percent, what is the current price of the bond? Multiple Choice €830.07 €1,030.00 $751.02 €1,251.33 €790.54
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT