Pretty much, whenever I try to run the code, the result is outputted before it asks for the color prompt. I don’t know why, when I run the color question standalone it works fine. It’s only in this code, any pointers?
Output:
Welcome to my twisted little game, are you little flubbers ready to engage in some games? Y=yes, N=no.
Y
Happy to hear it!
First question, here we go!
What is the best color?
NOPE WRONG ANSWER! TRY AGAIN SILLY!
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Put variables below this
char guess12;
// Opening question
cout << "Welcome to my twisted little game, are you little flubbers ready to engage in some games? Y=yes, N=no." << endl;
cin >> guess12;
if(guess12 == 'Y'||'y')
cout << "Happy to hear it!" << endl;
else
cout << "Oh well, we continue anyways. Not like you have much of a choice." << endl;
string color;
// Color question
cout << "First question, here we go!n"
<< "What is the best color?" << endl;
getline(cin, color);
while(color != "Purple" && color != "purple")
{
cout << "NOPE WRONG ANSWER! TRY AGAIN SILLY!n";
getline(cin, color);
}
cout<< "Well done! Purple is the best color!n";
}
It does compile without error and the first text block is the output. I have been searching for an answer for hours now and I have been trying to debug in any way I could find. I just don’t understand why it gives the “NOPE WRONG ANSWER! TRY AGAIN SILLY!” output to the while loop before it asks for the user input on line 22 (the getline).
I have tried swapping the order, finding alternatives to get the answer, and even trying to fit it into another function. I think I’m just missing something and I need help.
Adam Odens MidnightAJO is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3