1

So I wrote these codes when I ran it and input the right answer, it works perfectly fine, but if I put the wrong answer and use the while loop to run the program again, it outcome will store the last input and does not let me input anymore.

I found out that you can add guess.clear(); to clear the memory which is stored in the variable "guess".

#include <iostream>
#include <string>
#include <inttypes.h>

using namespace std;

int main(){

    int replay = 0;
    int input = 0;

    while(input == replay){
        string celebrity = "Keeves Reeves";
        string guess;
        guess.clear();


        cout << "Hint: He acted as John Wick." << endl;
        cout << "Guess who's this celebrity is: ";
        getline(cin, guess);


        if(guess == celebrity){
            cout << "Congratulations! You got it!\n" << endl;
            input = 2;
        } else if(guess != celebrity){
            cout << "Whoops, that not the right answer..." << endl;
            cout << "Do you want to try again? Enter '0' to replay or enter '1' to view the answer: ";
            cin >> input;
            if (input == 1){
                cout << "The answer is " + celebrity  + "." << endl;
                input = 2;
            }
        }
    }


    cout << "Thank you for playing. The end." << endl;
    system("pause");

    return 0;
}

Outcome:

Hint: He acted as John Wick.
Guess who's this celebrity is: Michael
Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer: 0
Hint: He acted as John Wick.
Guess who's this celebrity is: Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer:
FlashSonic526
  • 101
  • 6
  • 12

2 Answers2

1

If you are using getline() you could add the cin.ignore() to clear the buffer would look like:

int main(){

    int replay = 0;
    int input = 0;

    while(input == replay){
        string celebrity = "Keeves Reeves";
        string guess;
        //guess.clear();


        cout << "Hint: He acted as John Wick." << endl;
        cout << "Guess who's this celebrity is: ";
        getline(cin, guess);


        if(guess == celebrity){
            cout << "Congratulations! You got it!\n" << endl;
            input = 2;
        } else if(guess != celebrity){
            cout << "Whoops, that not the right answer..." << endl;
            cout << "Do you want to try again? Enter '0' to replay or enter '1' to view the answer: ";
            cin >> input;
            cin.ignore()  // here add the ignore() function.
            if (input == 1){
                cout << "The answer is " + celebrity  + "." << endl;
                input = 2;
            }
        }
    }
    cout << "Thank you for playing. The end." << endl;    
}
Josh W.
  • 102
  • 7
  • I wonder what's "buffer" means? Also, I wonder what .clear() do, does it like clear the string that is stored in the string variable? – FlashSonic526 Oct 13 '19 at 07:27
  • see: https://stackoverflow.com/questions/5131647/why-would-we-call-cin-clear-and-cin-ignore-after-reading-input – Josh W. Oct 13 '19 at 07:52
0

Is using getline necessary for you? It'll work as intended by using cin >> guess.

Code:

int replay = 0;
int input = 0;

while (input == replay) {
    string celebrity = "Keeves Reeves";
    string guess = "";

    cout << "Hint: He acted as John Wick." << endl;
    cout << "Guess who's this celebrity is: ";
    cin >> guess;


    if (guess == celebrity) {
        cout << "Congratulations! You got it!\n" << endl;
        input = 2;
    }
    else if (guess != celebrity) {
        cout << "Whoops, that not the right answer..." << endl;
        cout << "Do you want to try again? Enter '0' to replay or enter '1' to view the answer: ";
        cin >> input;
        if (input == 1) {
            cout << "The answer is " + celebrity + "." << endl;
            input = 2;
        }
    }
}


cout << "Thank you for playing. The end." << endl;
system("pause");

return 0;

}

Output:

Guess who's this celebrity is: a
Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer: 0
Hint: He acted as John Wick.
Guess who's this celebrity is: a
Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer: 0
Hint: He acted as John Wick.
Guess who's this celebrity is: a
Whoops, that not the right answer...
Do you want to try again? Enter '0' to replay or enter '1' to view the answer: 1
The answer is Keeves Reeves.
Thank you for playing. The end.
Press any key to continue . . .```
Francesco Montesano
  • 8,485
  • 2
  • 40
  • 64