int main( )
{
int numberToGuess;
int myGuess;
numberToGuess=92;
while (myGuess != numberToGuess);
cout << "Please guess an integer number between 1 and 100" << endl;
cin >> myGuess;
if (myGuess == numberToGuess)
cout << "You guessed the correct number" << endl;
else;
cout << "The number you guessed was incorrect. Try again!"<<endl ;
endif;
endwhile;
cout << "Thanks for playing the guessing game. Have a great day!" <<endl;
system("PAUSE");
return 0;
}
Use matching opening and closing braces { } to indicate the scope of loops or if statements. And beware of putting a semicolon where it is not needed!
1 2 3 4 5 6 7 8 9
while (myGuess != numberToGuess)
{
cout << "Please guess an integer number between 1 and 100" << endl;
cin >> myGuess;
if (myGuess == numberToGuess)
cout << "You guessed the correct number" << endl;
else
cout << "The number you guessed was incorrect. Try again!"<<endl ;
}
You don't necessarily need braces where there is just one statement as the body of the if / else, but you can add then for clarity:
1 2 3 4 5 6 7 8 9 10 11 12 13
while (myGuess != numberToGuess)
{
cout << "Please guess an integer number between 1 and 100" << endl;
cin >> myGuess;
if (myGuess == numberToGuess)
{
cout << "You guessed the correct number" << endl;
}
else
{
cout << "The number you guessed was incorrect. Try again!"<<endl ;
}
}
thnak u so much for ur answer... I added the braces and removed the semi colon and still same error msg "error: endif was not declared in this scope" :(
#include <iostream>
#include <cstdlib>
using namespace std;
int main( )
{
int numberToGuess;
int myGuess;
numberToGuess=92;
while (myGuess != numberToGuess)
{
cout << "Please guess an integer number between 1 and 100" << endl;
cin >> myGuess;
if (myGuess == numberToGuess)
cout << "You guessed the correct number" << endl;
else
cout << "The number you guessed was incorrect. Try again!"<<endl ;
}
endif
endwhile
cout << "Thanks for playing the guessing game. Have a great day!" <<endl;