Can someone help me out?

This voting program isn't working.
What am I doing wrong?


#include<iostream>

using namespace std;

int main()
{


int DJ = 0, Casey = 0;

int vote;

cout << "Pick your favorite Jaws brother\n Type D to vote for DJ. \n Type C to vote for Casey \n Type an E When Voting is Complete \n";

cin >> vote;

if (vote == D)
DJ++;

else if (vote == C)

Casey++;

else if (vote == E)



if (DJ > Casey)

cout << "The Winner is DJ!!";

else if (Casey > DJ)

cout << "The Winner is Casey!!";

system("pause");

return(0);
}
Please use the code format button ( <> ).

Now, what exactly is not working? What error is being produced?
What am I doing wrong?


1. You don't include using namespace std; so "cout" and "cin" are not recognized.
2. You use the wrong data type for your "vote" variable; it should be a 'char'.
3. You need to use single quotes in your if statements when comparing characters.

Also your program will only record 1 vote; I'm not sure if that's what you want.

Here's the code with all the corrections:
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
// Example program
#include <iostream>
#include <string>
using namespace std;

int main()
{
int DJ = 0;
int Casey = 0;
char vote;

cout << "Pick your favorite Jaws brother\n Type D to vote for DJ. \n Type C to vote for Casey \n Type an E When Voting is Complete \n";
cin >> vote;

if (vote == 'D')
DJ++;

else if (vote == 'C')
Casey++;

else if (vote == 'E')
cout << "Voting is complete!" << endl;

if (DJ > Casey)
cout << "The Winner is DJ!!";

else if (Casey > DJ)
cout << "The Winner is Casey!!";

system("pause");

return 0;
}
Last edited on
Momothegreat wrote:
1. You don't include using namespace std; so "cout" and "cin" are not recognized.
The OP certainly did, however it is advised to avoid including entire namespaces and instead to locally scope to them via the :: operator e.g. std::cout.


@OP I believe you are going to want a loop to iterate until 'd' is entered. Please look into using while loops or even a do/while. http://www.cplusplus.com/doc/tutorial/control/

Something like
1
2
3
4
do
{
    //stuff to repeat (get the input)
} while(input != EscapeSequence);



1
2
3
4
5
if (DJ > Casey)
    cout << "The Winner is DJ!!";

else if (Casey > DJ)
    cout << "The Winner is Casey!!";
What happens when DJ is equal to Casey? (Hint: nothing, so you may want to add in that case)


One last thing, you should avoid using system("anything");


Oh, and @OP please put in more descriptive information when posting questions here. "This voting program isn't working. What am I doing wrong?" is very vague and you don't even mention what your end goal is or what the assignment is. If you would please post that next time or even update yours that would be more helpful for us and for you. Keep in mind this isn't a website to do homework for you but to assist you.
Last edited on
All right. Just 1 more thing: I need to make it say something else if it's a tie


#include <iostream>
#include <string>
using namespace std;

int main()
{
int DJ = 0;
int Casey = 0;
char vote;

cout << "Pick your favorite Jaws brother\n Type D to vote for DJ. \n Type C to vote for Casey \n Type an E When Voting is Complete \n";
cin >> vote;

if (vote == 'D')
DJ++;

else if (vote == 'C')
Casey++;

else if (vote == 'E')
cout << "Voting is complete!" << endl;

else
cout << "Invalid Vote!";

do
{
(cin >> vote);
}

while (vote != 'E');
{

if (DJ > Casey)
cout << "The Winner is DJ!!";



else if (Casey > DJ)
cout << "The Winner is Casey!!";


else if (DJ = Casey)
cout << "It's a tie!!!";

}

system("pause");

return 0;
}




Everything I tried, it always said DJ wins when it was a tie. Can you help me out?
change this
1
2
3
4
5
do
{
(cin >> vote);
}


with this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
do
{


cin >> vote;

if (vote == 'D')
DJ++;

else if (vote == 'C')
Casey++;

else if (vote == 'E')
cout << "Voting is complete!" << endl;

else
cout << "Invalid Vote!";

}


Your code simply didn't checked the if else statement after the first vote. Try giving the first vote to Casey and you will find out

Remember how do while works. It just executes the statement inside the {} and nothing else

Last edited on
All right! It works now!

Thanks for your help!
Welcome :D
Topic archived. No new replies allowed.