Help with c++ homework?

Hello, so when working on this problem, I should be able to enter 'q' or 'Q' to finish entering characters, but it only stops after I enter 'Q'. I thought I did the uppercase function correctly but for some reason it doesn't seem to work. Is there something I'm doing wrong? Thanks!

[Edit] : To make matters weirder 'A' and 'a' seem to work just fine when I enter them, but its only 'q' and 'b' that don't seem to register...

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
#include <iostream>
using namespace std;

int main( )
{
	char vote = ' ';
	int numAndy = 0;
	int numBeth = 0;

	cout << "Please enter the first member's vote [A for Andy/B for Beth/Q to quit]: ";
	cin >> vote;

    vote = toupper(vote);
	
	while (vote != 'Q')
	{
		if (vote == 'A')
		{	
			numAndy = numAndy + 1;
		}
		else if (vote == 'B')
		{
			numBeth = numBeth + 1;
		}
		cout << "Please enter the next member's vote [A for Andy/B for Beth/Q to quit]: ";
		cin >> vote;
	}

	if (numAndy > numBeth)
	{
		cout << "Number of votes Andy received: " << numAndy << endl;
		cout << "Number of votes Beth received: " << numBeth << endl;
		cout << "Andy won the election." << endl;
	} 
	else if (numBeth > numAndy)
	{
		cout << "Number of votes Andy received: " << numAndy << endl;
		cout << "Number of votes Beth received: " << numBeth << endl;
		cout << "Beth won the election." << endl;
	} 
	else if (numAndy == numBeth)
	{
		cout << "Number of votes Andy received: " << numAndy << endl;
		cout << "Number of votes Beth received: " << numBeth << endl;
		cout << "Both candidates received the same number of votes." << endl;
	} 

	system("pause");
	return 0;
}
Last edited on
closed account (3CXz8vqX)
line 15.

C++ is very strict about uppercase and lower case. Q is different from q for instance. Perhaps you might want to adjust that line ever so slightly ^-^
Thank you so much for your reply... I don't mean to be dense, but I thought
vote = toupper(vote); would take care of the uppercase/lowercase problem, I changed line 15 to while (vote != 'Q' || vote != 'q') in hopes that that would be the change I needed, but it still kept asking me to continue inputting. Is there something I'm just not getting? Thank you again!
closed account (3CXz8vqX)
vote = toupper(vote); is not inside the while loop, so it won't make a difference in the slightest.

vote != 'Q' || vote !='q' ....shouldn't that be &&?

The problem is because you're using !=, which reverses logic. Try it, I could quite easily be wrong >.<;
You're missing that fact that you also have a cin of vote on line 26 for which you're not doing a toupper().
Ooooh, ok so I moved the toupper() inside the loop and changed vote != 'Q' || vote !='q' to vote != 'Q' && vote !='q' and it seems to work fine now. I apologize, I was under the impression that placing the toupper() where I did in the first place affected the entire code!

Thank you both so much!
closed account (3CXz8vqX)
Take a look into

http://www.cplusplus.com/doc/tutorial/variables/

Specifically the part about scope! (I'm actually going through it myself riveting stuff... like if I read more of this, I'll rivet myself to the wall...jk jk, it's actually really interesting. ).
Topic archived. No new replies allowed.