I'm New at this and can't figure this out

Feb 18, 2012 at 8:18pm
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#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;

		}
		else if (guess > randomNumber)
		{
			cout <<"Your guess is too high  Try again!";
		}
		else if (guess < randomNumber)
		{
			cout<<" Your guess is too low  Try again!";
		}
		else if (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
Last edited on Feb 18, 2012 at 9:08pm
Feb 18, 2012 at 8:21pm
Why don't you start by putting your code into code format (the "code" button) and then telling us what kind of errors you're getting.
Feb 18, 2012 at 8:55pm
@aas09f: He tried to do that but accidentally put his code after the code tags instead of inside of them.

@Tompoly: Where do you ask the user to input choice? Did you forget 'using namespace std;'? Did you mean 'gameInstructions' instead of 'explainGame'?
Feb 18, 2012 at 9:19pm
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <cstdlib>
#include <iostream>
#include <process.h>
#include <time.h>
#include <windows.h>

void showMenu();
void explainGame();
void playGame ();
bool QuitGame();

using namespace 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;

		}
		else if (guess > randomNumber)
		{
			cout <<"Your guess is too high  Try again!";
		}
		else if (guess < randomNumber)
		{
			cout<<" Your guess is too low  Try again!";
		}
		else if (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();
	}
}
Feb 18, 2012 at 9:27pm
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.
Last edited on Feb 18, 2012 at 9:28pm
Feb 18, 2012 at 9:29pm
change
cin >> quit;

to
1
2
3
4
5
6
7
8
9
10
11
12
char y;
cout << "Are you sure you want to quit? (y/n) ";
cin >> y;
if(y == 'y'){
   quit = true;
   return quit;
}
else{
   quit = false;
   return true; // or change the return type of main to bool
}
Feb 18, 2012 at 9:32pm
@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.
Feb 18, 2012 at 9:37pm
How is it "illegal"?
Feb 18, 2012 at 9:39pm
How is it "illegal"?


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.
Last edited on Feb 18, 2012 at 9:41pm
Feb 18, 2012 at 9:49pm
lol, illegal as far as the C++ language definition. =) I kinda just have "law" on the brain atm due to a couple criminal justice powerpoints

so moschops, on my thread, i got my program to open My Documents. thx for the help
Feb 20, 2012 at 6:58pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cstdlib>
#include <iostream>
#include <process.h>
#include <time.h>
#include <windows.h>


void showMenu();
void explainGame();
void playGame ();
bool exit();

using namespace 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;

		}
		else if (guess > randomNumber)
		{
			cout <<"Your guess is too high  Try again!";
		}
		else if (guess < randomNumber)
		{
			cout<<" Your guess is too low  Try again!";
		}
		else if (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';
}
Feb 20, 2012 at 7:01pm
Do not do this:

main();

Caliing main() is not something you should be doing.
Topic archived. No new replies allowed.