Help with this do/while loop

This is a program that determines what animal you are if you respond correctly to a set of questions related to animals.

However, the code is not functioning 100% accurately.

Once a player answers a question to try to guess the animal, the code gives you the option of playing again.

However, when the player types in "Y" or "y", the program reads the "Y" "y" as wanting to play again, and it also reads the "Y/y" as an answer to the next round of game questions.

I tried inserting a break, but it doesn't compile correctly.

What could this minor solution be? Thank you.

Here is the code (I suspect the problem is the while loop):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	if(animalName!=animal[i]->Name())
		{
			cout<<endl<<"You lose"<<endl;
			cout<<"I am not a "<<animalName<<endl;
			cout<<"I am a "<<animal[i]->Name()<<endl;
		}
		else
		{
			cout<<"You win!"<<endl;
			cout<<"I am a "<<animal[i]->Name()<<endl;
		}
		cout<<"Do you want to play again: ";
		cin>>ch;
		ch=toupper(ch[0]);
	}
	while(ch=="Y" || ch=="y");
	
	system("pause");
	return 0;
	
}


Last edited on
That's only one half of the loop. Post the whole thing.
Here you go:

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
do{
		 int lb=0;
		 int ub=7;
		int i=random.next(lb, ub);
		if(animal[i]->IsMeatEater())
			cout<<endl<<"I am a meat eater"<<endl;
		else
			cout<<"I am not a meat eater"<<endl;
		if(animal[i]->HasFourLegs())
			cout<<"I have four legs"<<endl;
		else
			cout<<"I do not have four legs"<<endl;
		if(animal[i]->IsMammal())
			cout<<"I am a mammal"<<endl;
		else 
			cout<<"I am not a mammal"<<endl;

		cout<<"Which animal do you think I am? "<<endl;
		cout<<"Am I a Cow?"<<endl;
		cout<<"Am I a Green Iguana?"<<endl;
		cout<<"Am I a Komodo Dragon?"<<endl;
		cout<<"Am I a Manatee?"<<endl;
		cout<<"Am I a Panther?"<<endl;
		cout<<"Am I a Rattle Snake?"<<endl;
		cout<<"Am I a Sparrow?"<<endl;
		cout<<"Am I a Sperm Whale?"<<endl;
		getline(cin,animalName);
		if(animalName!=animal[i]->Name())
		{
			cout<<endl<<"You lose"<<endl;
			cout<<"I am not a "<<animalName<<endl;
			cout<<"I am a "<<animal[i]->Name()<<endl;
		}
		else
		{
			cout<<"You win!"<<endl;
			cout<<"I am a "<<animal[i]->Name()<<endl;
		}
		cout<<"Do you want to play again: ";
		cin>>ch;
		ch=toupper(ch[0]);
	}
	while(ch=="Y" || ch=="y");
	
	system("pause");
	return 0;
	
}




thanks for replying.
Does it not function correctly or does it not even compile?
It compiles, but it does not function correctly.

Add cin.clear(); before line 27 and 40.
Also, you could declare ch directly as type char, remove line 41, and replace the double quotes in line 43 with single quotes.
Hope this helps.
Topic archived. No new replies allowed.