HI I have pasted my code below for a program we are suppose to hand in. I'm new at this and can't figure out whats wrong with my code. Can someone please help me out? We are to write a program for a guessing game and include a start menu, here is what I got:
#include <cstdlib>
#include <iostream>
#include <process.h>
#include <time.h>
#include <windows.h>
void showMenu();
void gameInstructions();
void playGame ();
bool QuitGame();
int main()
{
system ("cls");
int choice;
cout <<"Welcome to my guessing game";
cout <<"1. Explain Game";
cout <<"2. Play Game";
cout <<"3. Exit";
switch (choice)
{
case 1: explainGame();
break;
case 2:
playGame();
break;
case 3:
quitGame();
break;
default:
cout << "Choose another number";
}
system ("cls");
return 0;
}
void explainGame()
{
system ("cls");
cout <<"Explain Game";
cout<<"Guess a number between 1 and 20, you have three guesses";
system ("pause");
main();
}
void playGame()
{
int guess;
srand(time(NULL));
int randomNumber = rand()%20 +1;
system ("cls");
for (int i=1; i<=3; i++)
{
cout <<"Guess The Number";
cin >> guess;
cin.ignore();
if (guess == randomNumber)
{
cout << "Congratulations, you got it!";
cout << "You guessed the number in" <<i<<"tries";
system("pause");
return;
}
elseif (guess > randomNumber)
{
cout <<"Your guess is too high Try again!";
}
elseif (guess < randomNumber)
{
cout<<" Your guess is too low Try again!";
}
elseif (i=3)
cout<<" Sorry, you have no more guesses.";
system("pause");
main();
}
}
bool quitGame()
{
bool quit;
char y='y';
cout<<" Are you sure you want to quit? (y/n)";
cin >> quit;
if (quit==y)
{
quit = false;
return quit;
}
else
{
quit = true;
return main();
}
}
These are the error codes I'm Getting:
Error 12 error C2065: 'cin' : undeclared identifier
Error 1 error C2065: 'cout' : undeclared identifier
Error 5 error C3861: 'explainGame': identifier not found
Ok I fixed a few errors but I still am getting one error message & 2 warrings:
Error 1 error C3861: 'quitGame': identifier not found
Warning 3 warning C4800: 'int' : forcing value to bool 'true' or 'false' (performance warning)
Warning 2 warning C4805: '==' : unsafe mix of type 'bool' and type 'char' in operation
#include <cstdlib>
#include <iostream>
#include <process.h>
#include <time.h>
#include <windows.h>
void showMenu();
void explainGame();
void playGame ();
bool QuitGame();
usingnamespace std;
int main()
{
system ("cls");
int choice;
cout <<"Welcome to my guessing game";
cout <<"Please enter choice";
cout <<"1. Explain Game";
cout <<"2. Play Game";
cout <<"3. Exit";
switch (choice)
{
case 1: explainGame();
break;
case 2:
playGame();
break;
case 3:
quitGame();
break;
default:
cout << "Choose another number";
}
system ("cls");
return 0;
}
void explainGame()
{
system ("cls");
cout <<"Explain Game";
cout<<"Guess a number between 1 and 20, you have three guesses";
system ("pause");
main();
}
void playGame()
{
int guess;
int randomNumber = rand()%20 +1;
system ("cls");
for (int i=1; i<=3; i++)
{
cout <<"Guess The Number";
cin >> guess;
cin.ignore();
if (guess == randomNumber)
{
cout << "Congratulations, you got it!";
cout << "You guessed the number in" <<i<<"tries";
system("pause");
return;
}
elseif (guess > randomNumber)
{
cout <<"Your guess is too high Try again!";
}
elseif (guess < randomNumber)
{
cout<<" Your guess is too low Try again!";
}
elseif (i=3)
cout<<" Sorry, you have no more guesses.";
system("pause");
main();
}
}
bool quitGame()
{
bool quit;
char y='y';
cout<<" Are you sure you want to quit? (y/n)";
cin >> quit;
if (quit==y)
{
quit = false;
return quit;
}
else
{
quit = true;
return main();
}
}
C++ is case sensitive. You wrote "quitGame" instead of "QuitGame" (or vice versa).
You should learn how to fix extremely simple errors like these ;)
Also, never call the main function as you are doing on line 108.
The entire "quitGame" function you have can be written like this instead:
1 2 3 4 5 6 7
bool quiteGame()
{
char yn = 'y';
cout << "Are you sure you want to quit? y/n: ";
cin >> yn;
return yn == 'y' || yn == 'Y';
}
And in main, you should have everything in a loop and exit the loop if quitGame returns false.
In your original code, the 'quit' boolean was completely useless, because you either assigned to it and returned it immediately, or assigned to it and returned something else after.
char y;
cout << "Are you sure you want to quit? (y/n) ";
cin >> y;
if(y == 'y'){
quit = true;
return quit;
}
else{
quit = false;
returntrue; // or change the return type of main to bool
}
@Asif Hirai: it is illegal to have main return anything other than a signed int. And, you should never call main ;) though I have to admit I didn't catch that input-to-bool thing.
It is against the C++ definition. The C++ language is very clearly defined. Anything that violates those rules is termed "illegal". Any compiler that accepts such violations is not a C++ compiler. It's probably very similar to a C++ compiler, but it isn't one.
// or change the return type of main to bool
That would violate the C++ language definition.
First off thank you everyone for the help! I have fixed the errors and now I am not getting any errors but my guesses are not running through properly and overall the program just is not doing what its suppose to do. I have been working on this for hours now and I just can't get it to work right.
#include <cstdlib>
#include <iostream>
#include <process.h>
#include <time.h>
#include <windows.h>
void showMenu();
void explainGame();
void playGame ();
bool exit();
usingnamespace std;
int main()
{
system ("cls");
int choice=0;
cout <<"Welcome to my guessing game\n";
cout <<"Please enter choice\n";
cout <<"1. Explain Game\n";
cout <<"2. Play Game\n";
cout <<"3. Exit\n";
cin>>choice;
switch (choice)
{
case 1: explainGame();
break;
case 2:
playGame();
break;
case 3:
exit();
break;
default:
cout << "Choose another number";
}
system ("cls");
return 0;
}
void explainGame()
{
system ("cls");
cout <<"Explain Game\n\n";
cout<<"Guess a number between 1 and 20, you have three guesses";
system ("pause");
main();
}
void playGame()
{
int guess;
int randomNumber = rand()%20 +1;
system ("cls");
for (int i=1; i<=3; i++)
{
cout <<"Guess The Number";
cin >> guess;
cin.ignore();
if (guess == randomNumber)
{
cout << "Congratulations, you got it!";
cout << "You guessed the number in" <<i<<"tries";
system("pause");
return;
}
elseif (guess > randomNumber)
{
cout <<"Your guess is too high Try again!";
}
elseif (guess < randomNumber)
{
cout<<" Your guess is too low Try again!";
}
elseif (i==3)
cout<<" Sorry, you have no more guesses.";
system("pause");
main();
}
}
bool exit()
{
char yn = 'y';
cout << "Are you sure you want to quit? y/n: ";
cin >> yn;
return yn == 'y' || yn == 'Y';
}