When I type in 12, it should say "my age is 12" and that is it. HOWEVER, it also says the cout for the magic word which is "you guessed goodness, congratz..", I only want "my age is 12" how can I fix this little bug?
#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int age = 12;
string item= "bag";//you must use meaningful variables
cout << "guess my age" << endl;
cout << " " << endl;
cout << " " << endl;
cin >> age;
if ( age == 12 ) {
//cout << "my age is 12" << endl; -> Dont hard code the 12, use your variables
cout<<"My age is "<<age<<endl;
}
elsewhile(age != 12)
{
cin>>age;
}
string answer;
cout<<"Guess the item: ";
cin>>answer;
if (answer == item) {
cout << "you guessed goodness, congratz.." << endl;
}
else
cout<<"Too bad for you!"<<endl;
system("PAUSE");
return 0;
}
I only want "my age is 12" how can I fix this little bug?
Remove lines 25--27?
The first ifelse covers only lines 18--23. It executes either line 19 or line 23, depending on what the user types.
The second if (25--27) is unrelated.
Do notice that you wrote braces around line 19 and around line 26, but not around line 23. You could have multiple statements within braces. You could have braces after the else, i.e. around line 23 (or even enclosing lines 23-27, if that is what you want).