Y/N while loops not working

Why wont my code work? The program asks to start counting tickets then no matter what the input is, the program executes:

There were 0 attendees. 0 had blue tickets and 0 had green tickets.


Please help me.



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
 int main()
{

	char ans, ticket;
	int blue = 0;
	int green = 0;

	cout << "Start counting tickets? (y/n): ";
	cin >> ans;

	while (toupper(ans) == 'y')
	{
		cout << "Enter b for blue or g for green: ";
		cin >> ticket;

		while (toupper(ticket) == 'b')
		{
			blue++;
		}

		while (toupper(ticket) == 'g')
		{
			green++;
		}

		cout << "Count another ticket ? (y / n) : ";
		cin >> ans;
	}

	cout << "There were " << blue + green << " attendees. " << blue << " had blue tickets and " << green << " had green tickets." << endl;

	return 0;

}
What does toupper() do? Now think about what 'y' is.
Last edited on
Thank you YFGNG ! I didnt think that it would matter, learned something new. Although when I do this for 'g' and 'b' the program will stop once I enter B ,b ,g , or G.

edit: Actually I got it after switching them to if else
Last edited on
You should use an if, else if, else. I assume your conditions go something like:

1
2
if (B) ++blue;
else   ++green;


But what if someone enters a character other than 'b' or 'g'?
Last edited on
Thank you c: I got the program to work
Topic archived. No new replies allowed.