Simple game...not so simple anymore

I've exhausted my personal knowledge and reading of (D.S. Malik: C++ Programming, 6th ed) as to what I am doing wrong/ needs to be done. I'd really appreciate any help with this. What I am trying to do is:
1) verify user name
2) if name is same, start game
3) if name is not same, get new name, then start game
4) provide 5 hints to user, allow input of answer as "guess" variable
5) correct animal is: lion (however, when entering lion, I get "incorrect answer"

Any help would be appreciated...I'm really at max stress level and at wits end with C++...

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
 //Header includes...//
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

//Usings...//
using namespace std;

//Prototype functions...//
void animalGuess();
void WinGame();
string loseGame();

//Named constants...//
const string lion;
const string animal = lion;

//Start of main program...//
int main()
{
	//Declared variables...//
	string playerName;
	char menuChoice;
	string guess;
	ifstream inFile;
	ofstream outFile;
	bool gameContinue = true;

	//Start of Main Menu display//
	cout << "Welcome to the Animal Guessing Game!\n" << endl;
	cout << endl;
	cout << "You will have a total of 5 attempts to guess the animal.\n "
		<< "Enter your guess after each hint." << endl;
	cout << endl;
	cout << "For the duration of the game, please ensure\n "
		<< "your 'CAPS LOCK' is off and you use all lower case letters." << endl;
	cout << endl;
	inFile.open("player.txt");
	inFile >> playerName;
	cout << "Are you player, " << playerName << "? Enter: y if so,\n "
		<< "otherwise, enter: n ...to register a new name." << endl;
	cout << endl;
	inFile.close();
	cin >> menuChoice;
	cout << endl;

			//Nested Menu Choice for invalid menu entry//
			while (menuChoice != 'y' && menuChoice != 'n')
			{
				cout << "Invalid choice. Please enter either a 'y' or an 'n'." << endl;
				cin >> menuChoice;
			}//endWhile//

		//Sub Menu Choice: Returning player//
		if (menuChoice == 'y')
		{
			cout << "Welcome back, " << playerName << "!"
				<< "I hope your memory is good! Game starts now!" << endl;
			animalGuess();
		}//endIf
	
			//Sub Menu Choice: Get new player's name//
			else if (menuChoice == 'n')
				{
					cout << "Please type your name, press ENTER when done." << endl;
					cin >> playerName;
					outFile.open("player.txt");
					outFile << playerName;
					outFile.close();
					cout << "Welcome, " << playerName << ", the game will now begin!" << endl;
					animalGuess();
			}//endElseIf//

bool gameContinue();

system ("pause");

return 0;

}//endof main() function//

//Function that displays hints and allows player guess...//
void animalGuess()
{
	//Function variables...//
	string guess;
	string hint1 = "I have four legs, what am I?";
	string hint2 = "Incorrect. I have fur, what am I?";
	string hint3 = "Incorrect. I have a tail, what am I?";
	string hint4 = "Incorrect. I have whiskers, what am I?";
	string hint5 = "Incorrect, last guess! I'm \"King of the Jungle\"\n What am I?";
	int count = 0;
	
	//Display hints, allow input of guess, increase count by one (after each guess input), and display count//
	
	while (count < 5 && guess != animal)
	{
		cout << hint1 << endl;
		cin >> guess;
		count++;
		cout << count << endl;
		cout << hint2 << endl;
		cin >> guess;
		count++;
		cout << count << endl;
		cout << hint3 << endl;
		cin >> guess;
		count++;
		cout << count << endl;
		cout << hint4 << endl;
		cin >> guess;
		count++;
		cout << count << endl;
		cout << hint5 << endl;
		cin >> guess;
		count++;
		cout << count << endl;
		cout << "No more guesses remain. Sorry, you lose!" << endl;
		cout << endl;
		
		if (guess == animal)
		{
			void WinGame();

		}//endIF//

		else if (count == 5)
			bool gameContinue();

	}//endWhile//

}//end of animalGuess() function//


//Function that reports a winning game to player//
void WinGame()
{
	string playerName;
	cout << "Congratulations, " << playerName << ", you guessed correct!"
		<< "The animal was a \"Lion\" " << endl;

}//end of WinGame() function//

//Function that returns continue value for another game; yes = true, no = false and quit//
bool gameContinue()
{
	char contGame;
	
	cout << "Play again? Enter: yes, to play again, otherwise, enter: no, to quit" << endl;
	cin >> contGame;

	if(contGame == 'yes')
	{
		return true;
	}
	else
	{
		return false;
	}
}
By the way this code is wrong

1
2
3
4
5
6
7
8
9
10
11
12
13
	char contGame;
	
	cout << "Play again? Enter: yes, to play again, otherwise, enter: no, to quit" << endl;
	cin >> contGame;

	if(contGame == 'yes')
	{
		return true;
	}
	else
	{
		return false;
	}


The result of condition if(contGame == 'yes') will be always equal to false.
Well, you have quite a few things that aren't okay here.

Actually this code - if it's yours - suggests that you don't understand what you are doing. You may want to check out a C++ tutorial you can find on this site.

For example, you are doing lots of bad things with variables:

1
2
3
//Named constants...//
const string lion; //line 16
const string animal = lion;


What you are doing here, is declaring 2 constant strings. Now, since these are constant, you can define them only once. Now tell me - what does lion have in it? Since it's constant, you won't be able to change it. Lion probably contains ""(empty string), and so does animal.
And by the way - if you have 2 constant strings that are the same, you don't need second one. Lion itself would be sufficient.

Next thing would be:

1
2
3
4
5
6
7
8
//Function that reports a winning game to player//
void WinGame()
{
	string playerName;
	cout << "Congratulations, " << playerName << ", you guessed correct!"
		<< "The animal was a \"Lion\" " << endl;

}//end of WinGame() function// 

You create local variable playerName. What value does it have? Again, since you didn't define it, it's probably ""(empty string). You probably wanted to do something like:

void WinGame(string playerName)

And another thing - the big while loop for guessing won't work. Why? Go on, try to run it by yourself(in your head - step by step - imagine you are the computer that runs the program :) ). If you still have some problems, ask.
I was able to get a lot of bugs worked out, and thanks for the help folks.

I'm down to my last bug...it involves a bool value. Below is my current, bugged, code. I still am unable to get the input of:
lion
... to successfully end the game. Currently, I think, after the final hint, it doesn't matter I type, I win. (that's cheating, can't have it!) hahah. Thanks again for any and all help. (I think some of the problem may be from using a 'void' function and not getting a return type?...

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
void animalGuess()
{
	//Function variables...//
	string guess;
	string hint1 = "Hint 1: I have four legs, what am I?";
	string hint2 = "Hint 2: Incorrect. I have fur, what am I?";
	string hint3 = "Hint 3: Incorrect. I have a tail, what am I?";
	string hint4 = "Hint 4: Incorrect. I'm a carnivore, what am I?";
	string hint5 = "Hint 5: Incorrect, I have whiskers, what am I?";
	int guesses = 5;
	bool animal = false;

	for (int i = 0; i < guesses; i++)
	{	
		cout << hint1 << endl;
		cin >> guess;
		cout << hint2 << endl;
		cin >> guess;
		cout << hint3 << endl;
		cin >> guess;
		cout << hint4 << endl;
		cin >> guess;
		cout << hint5 << endl;
		cin >> guess;

		if (guess == "lion")
		{
			cout << "Congratulations! You guessed correct!" << endl;
			animal = true;
			return;
		}

		else 
		{
			cout << "Sorry, no guess remain, you lose." << endl;
			return;
		}
	
	}

	return;
}//end of animalGuess() function// 
Last edited on
a simple thing i would like to point out is, when your for loop starts and you say cout<<hint1, and then you do cin>>guess, what if the person guesses right(i.e it is lion), then what is the use for doing cout<<hint2.
I see what you're saying, I thought that perhaps since the
if(guess == "lion") was nested within the 'for' loop, it would execute to do just that :/
Your for loop isn't a loop. There are returns in every path of execution in the body of the loop. You would achieve the same effect by removing the for machinery completely.
Hey, dessoul, remember what I said?

And another thing - the big while loop for guessing won't work. Why? Go on, try to run it by yourself(in your head - step by step - imagine you are the computer that runs the program


I was talking about thing you posted after my reply(so your animalGuess() ).
First off, it really looks like you didn't try to debug it at all. Programming isn't a job for lazy people. If you do some large thing, you are the one who made it, and you should debug it. If you have much code and it's hard to find an error, you shouldn't rely on internet guys to do it for you. Use errors you have now and try to learn by yourself. You will learn a lot more if you experiment by yourself then if we point out all the bugs.

I also said "try to run program in your mind". Which means - go step by step, line after line, instruction after instruction, and try to imagine what's going on in your program. If you did, you would see "oh, wait! I'm asking for guess so many times, and I still didn't check if any of these guesses is correct!"

If something doesn't work, you can always make a copy of it somewhere else(just in case), and just play with it. Try to find error and try to fix it. If you don't know how something in your code works - you should check it in internet. It may take some time, but trust me - you will learn a lot, and eventually, debugging may become easier.

Cheers!
Matthew, I understand what you mean with your examples; steps to take to try and resolve the problem. However, I am under the impression that this forum, being the "beginner" section, is for people like me: beginning to use/ learn C++. If I have exhausted my knowledge on troubleshooting a problem, is it not a logical option for my next step to be to ask for help?

Cire, it is my understanding, according to read material, that 'for' is a loop... Am I wrong in deducing author's text as being factual? (confused)

That said, maybe my limited understanding of the software is getting in the way of me being able to describe my problems in sufficient and clear way? (It's certainly the factor behind my asking questions). Maybe the problem is within these forums...

I'm not asking anyone here to solve my problems for me; don't want anyone to. Offering helpful suggestions or pointers about my code (or providing sample code), I have found, does help me to learn better; everyone learns differently, right? But, pouring over a textbook, while helpful, does have it's limits, though. (I'm not the kind of person who can look at something over and again (that is wrong) and be able figure out what is wrong if I don't know where to look (in the first place) or what to look for.)

I really do appreciate the helpful suggestions, folks... just, not so much the implied accusations that I'm not making an attempt to learn or want others to do my work for me; am lazy. If I put off that impression, forgive me, not my intention! I know that having others do something for you accounts to you not learning a damn thing :)

Final note, I was able to get my code working. Thanks, dhruv, for pointing that out. (Sometimes, it takes people looking in, from outside the box, to point out the obvious; which is not always obvious when someone is hung up on an issue for a tremendous amount of time)

Thanks, everyone :)
Cire, it is my understanding, according to read material, that 'for' is a loop... Am I wrong in deducing author's text as being factual? (confused)


for is a loop construct, yes. However, if I do:

1
2
3
4
5
int somefunc()
{
    for ( int i=0; i<10; ++i )
        return i ;
}


I don't return from the function 10 times. I return during the first iteration of the loop and so that code is functionally equivalent to this code:
1
2
3
4
int somefunc()
{
    return 0;
}


This is why your for loop isn't really a loop. It never completes an iteration (in other words, it never loops.)
Last edited on
closed account (Dy7SLyTq)
you would want to make a for loop that calles a function 10 times and in the function you return a static variable that is upped with ++
Ah, I see...makes perfect sense now! Thanks you two ^
Something else that hasn't been mentioned is that you are overwriting the guess several times before you check if it is correct. The simplest way of storing the hints properly is to use an array.

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
// the code you last posted
void animalGuess()
{
	//Function variables...//
	string guess;
	string hint1 = "Hint 1: I have four legs, what am I?";
	string hint2 = "Hint 2: Incorrect. I have fur, what am I?";
	string hint3 = "Hint 3: Incorrect. I have a tail, what am I?";
	string hint4 = "Hint 4: Incorrect. I'm a carnivore, what am I?";
	string hint5 = "Hint 5: Incorrect, I have whiskers, what am I?";
	int guesses = 5;
	bool animal = false;

	for (int i = 0; i < guesses; i++)
	{	
		cout << hint1 << endl;
		cin >> guess;
		cout << hint2 << endl;
		cin >> guess;
		cout << hint3 << endl;
		cin >> guess;
		cout << hint4 << endl;
		cin >> guess;
		cout << hint5 << endl;
		cin >> guess;

		if (guess == "lion") // this only checks the guess after hint 5
		{
			cout << "Congratulations! You guessed correct!" << endl;
			animal = true;
			return;
		}

		else 
		{
			cout << "Sorry, no guess remain, you lose." << endl;
			return;
		}
	
	}

	return;
}//end of animalGuess() function//  


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
// modified code
void animalGuess()
{
	//Function variables...//
	string guess;
        string hint[5]= {
                                 "Hint 1: I have four legs, what am I?",
                                 "Hint 2: Incorrect. I have fur, what am I?",
                                 "Hint 3: Incorrect. I have a tail, what am I?",
                                 "Hint 4: Incorrect. I'm a carnivore, what am I?",
                                 "Hint 5: Incorrect, I have whiskers, what am I?"
                        }
	int guesses = 5;
	bool animal = false;

	for (int i = 0; i < guesses; i++)
	{	
		cout << hint[i] << endl; // now you only receive one hint per guess
		cin >> guess;

		if (guess == "lion")
		{
			cout << "Congratulations! You guessed correct!" << endl;
			animal = true;
			return;
		}
	}
	cout << "Sorry, no guess remain, you lose." << endl; // as explained in another post, 
//you don't want to always return after first guess
	return;

}//end of animalGuess() function//  
Last edited on
Well, I didn't want to sound rude or anything - just wanted to make sure that you did check your code, because, well... For me it's obvious, and if I run your program in my mind I could clearly see what's wrong - hoped that you could achieve the same - but if you don't it's okay.

manudude3 provided you with good code, that will check for guess. If you still are checking this thread, you may see the differences now and see what you did wrong. It isn't that bad, but shows that you still have a problems with following your program(and few other things). But because you're learning, it's completly okay. Good luck, and hope you will improve quickly!:)
I really wanted to make use of array and 'for' loop in my function, but I was having a heck-of-a-time with what little code I did create, so I didn't bother. That said, I cannot express my thanks enough for providing an example of code that does both... thank you so much for taking the time from your day and do so. Thanks, all of you, words cannot describe my gratitude for your time, patience, and understanding!
Topic archived. No new replies allowed.