need help at a voting program can somebody help?

so i have 4 errors and i dont know how to fix it...
after pressing v and when i'm about to choose who to vote i cant type anyhing

#include <iostream>
using namespace std;

int Roco, FPJ, GMA;
int desicion;
int V, R, Q, A;
int vote;

int main()
{
cout<<"PRESIDENTIAL ELECTIONS \n";
cout<<"Candidates: \n";
cout<<"A)FPJ\tB)ROCO\tC)GMA\n";
cout<<"[Enter`V`-vote, `R`-result, `Q'-quit]: \n";
cin>>desicion;
if(desicion == V)
cout<<"Enter yout vote:";
do{
cin>>vote;
}while (vote == A);
++FPJ;
cout<<FPJ;

return 0;
}
Last edited on
What are your errors?

Just for starters, you haven't declared FPJ, so the value will be initialised some random data. I'll assume you want it to start at 0, so you should declare it that way. Also, try not to use global variables (variables outside of main()), because it means that you might create problems within functions later on.

Also, you are comparing vote (a char) with "A" (a string literal). Try comparing it with 'A' instead (notice the single quotes instead of the double quotes).

In line 16, you are comparing desicion with V, which is interpreted as a variable, which doesn't exist. I assume this is one of your errors. Try making desicion a char, and again, compare it to 'V' (inside single quotes).

Another thing you probably havent realised, is that it will ask for their vote regardless of whether they press 'V'. Instead, try putting your loop for voting into its own section, using curly braces.

Finally, your while statement. You can't use a while loop like a for loop, you can only do comparisons inside. Try moving FPJ++ inside the loop, based on whether 'A' has been pressed.
Last edited on
next time use indentation to make your code cleaner and more readable

I have re-wrote your code and explain your error by comments:
NT3 already said and explained your errors, i just want you to understand it more:

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


int main()
{
	int Roco = 0, FPJ = 0, GMA = 0;
	char decision;
	int V, R, Q, A; // i don't how you will use this
	char vote;

	cout<<"PRESIDENTIAL ELECTIONS \n";
	cout<<"Candidates: \n";
	cout<<"A)FPJ\tB)ROCO\tC)GMA\n";
	
	cout<<"[Enter`V`-vote, `R`-result, `Q'-quit]: \n";
	cin>>decision;
	
	if ( decision == 'V' ) { /* you should enclose this if with
							  * braces if there are more than one
							  * commands you want to perform under the
							  * condition.
							  */
		cout<<"Enter yout vote:";
		do{
			
			cin>>vote;
			if ( vote == 'a' )
				++FPJ;
			else if ( vote == 'b' )
				++Roco;
				/* I ASSUME you know what you will add here ... */
		
		} while (vote == 'E'); /* I will assume you want to exit this loop
                                                 * until the user presses inputs  'E' */
	} /* <---The closing brace of if (decision == 'V') indicating the end of the commands
	   *     you want to perform after the condition ( decision == 'V' )
	   */
	
	cout<<FPJ;

	return 0;
}


Just read this and hopefully you will know your errors.
I hope this helped you
Last edited on
I think this is the Problem :D



Write a C++ program for Presidential Elections, the user will enter his votes based on the code for each candidate. The program will add the total votes for each candidate and proclaim the winner whoever got the most number of votes. The user will stop in accepting votes if the user entered letter ‘Q’, ‘V’ to vote and ‘R’ to view the result.

Sample Output:
PRESIDENTIAL ELECTIONS

Candidates:

A) FPJ B) ROCO C) GMA

[Enter ‘V’-vote, ‘R’-result and ‘Q’-quit]: V 

Enter your vote: A <enter>


PRESIDENTIAL ELECTIONS

Candidates: Results:
FPJ 100
ROCO 25
GMA 5

TOTAL VOTES: 130

And the winner is: FPJ
Last edited on
sure bro...
so i have been trying it out...
so i came up with this...
#include<iostream>
#include<string>
using namespace std;
void main()
{
char choice,choice2;
int ca=0,cb=0,cc=0,tv=0;
string sa="FPJ",sb="Roco",sc="GMA";

do
{
cout<<"\t Presidential Elections\n\n";
cout<<"Candidates \n\n";
cout<<"<A>FPJ \n";
cout<<"<B>Roco \n";
cout<<"<C>GMA \n\n";
cout<<"Enter <v>vote <r> result and <q> quit : ";
cin>>choice2;
}
if(choice2=='V' || choice2=='v');
else(choice2=='R' || choice2=='r')

cout<<"-----------------------\n";

}
while(choice2=='V' || choice2=='v');
if (choice2=='R' || choice2=='r')
{
cout<<"\t Presidential Elections\n\n";
cout<<"Candidates:"<<"\t results:"<<"\n\n";
cout<<"<A>FPJ : "<<ca<<"\n";
cout<<"<B>Roco : "<<cb<<"\n";
cout<<"<C>GMA : "<<cc<<"\n\n";
tv=ca+cb+cc;
cout<<"Total Votes : "<<tv<<"\n";
//place "and the winner is : <name> here//
}
}
but i have 10 errors can anybody help?
sorry wrong program
#include<iostream>
#include<string>
using namespace std;
void main()
{
char choice,choice2;
int ca=0,cb=0,cc=0,tv=0;
string sa="FPJ",sb="Roco",sc="GMA";

do
{
cout<<"\t Presidential Elections\n\n";
cout<<"Candidates \n\n";
cout<<"<A>FPJ \n";
cout<<"<B>Roco \n";
cout<<"<C>GMA \n\n";
cout<<"Enter <v>vote <r> result and <q> quit : ";
cin>>choice2;
}
if(choice2=='V' || choice2=='v');
else(choice2=='R' || choice2=='r')

cout<<"-----------------------\n";
cout<<"Enter your vote : ";

cin>>choice;
switch(choice)
{
case 'A':
case 'a':
ca++;
break;
case 'B':
case 'b':
cb++;
break;
case 'C':
case 'c':
cc++;
break;
}



{
cout<<"\t Presidential Elections\n\n";
cout<<"Candidates:"<<"\t results:"<<"\n\n";
cout<<"<A>FPJ : "<<ca<<"\n";
cout<<"<B>Roco : "<<cb<<"\n";
cout<<"<C>GMA : "<<cc<<"\n\n";
tv=ca+cb+cc;
cout<<"Total Votes : "<<tv<<"\n";
//place "and the winner is : <name> here//
}
}
but i have 10 errors can anybody help?


You've already had helpful feedback on syntax and indentation. Why is it you don't want to read it?
let me give you a presentation on how to solve your problem:
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

int main ()
{
    /* First, think of the variables you need:
    * ~ a variable for the user's choice ( (v)vote, (r)result, (q)quit ) ( char )
    * ~ 3 variables to store the votes for each candidates ( int )
    * ~ a variable to store the total number of votes ( int )
    *
    * @print the welcome screen@ */
    do {
        /*
        *     Presidential elections
        *
        * Candidates:
        * <A> FPJ
        * <B> ROCO
        * <C> GMA
        *
        * Enter <v> vote <r> result and <q> quit :
        * @prompt the input */
        if ( input == 'v' )
        {
            /* what do you want to do when the user enterd 'v' ?, put it here */
        }
        else if ( input == 'r' )
        {
            /* You want this area a code to print the results */
        }
    } while ( input != 'q' ) // while the user input is not equal to q
    
    return 0; /* quit the program */
}
I am not really proud of this -code- but may help, i had a little time.
Compile,run and look what happens!

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
//Democracy.cpp
//Voting program

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <string>
using std::string;

void vote();
void results(string candidate_1,string candidate_2,string candidate_3);
int winner(int array_results[],int size);
int
	candidate_counter_1=0,
        candidate_counter_2=0,
        candidate_counter_3=0; //global variables cuz these mother f.k.rs.. are everywhere!

int main(){


bool isVoting=true;
char option;
string
	candidate_1="Pikachu",
	candidate_2="Son Goku",
	candidate_3="Homer Simpson"; //local variables to simulate democracy..

while(isVoting){
cout<<"PRESIDENTIAL ELECTIONS\n\n";
cout<<"Candidates:\n";
cout<<"1.-"<<candidate_1
	<<"\n2.-"<<candidate_2
	<<"\n3.-"<<candidate_3<<endl;
cout<<"Enter <v> vote, <r> results, <q> quit\n";
cin>>option;

while(option!='v'&&option!='V'&&option!='r'&&option!='R'&&option!='Q'&&option!='q'){
cout<<"\nEnter <v> vote, <r> results, <q> quit\n";
cin>>option;
}//end inner while

if(option=='v'||option=='V'){
	vote();
}else if(option=='r'||option=='R'){
	results(candidate_1,candidate_2,candidate_3);
}else if(option=='q'||option=='Q'){
	isVoting=false;
}//end if..else..else

}//end while

return 0; //indicates success
}//end main

void vote(){

int election;

cout<<"\nPlease enter your election: ";
cin>>election;
while(election<1||election>3){
cout<<"\nPlease enter your election (1,2,3): ";
cin>>election;
}//end while

switch(election){
	case 1:
		candidate_counter_1++;
	break;
	case 2:
		candidate_counter_2++;
	break;
	case 3:
		candidate_counter_3++;
	break;
	default:
	break;
}//end switch


}//end function vote


void results(string candidate_1,string candidate_2,string candidate_3){
cout<<'\n'<<candidate_1<<' '<<candidate_counter_1<<" votes\n"
	<<candidate_2<<' '<<candidate_counter_2<<" votes\n"
	<<candidate_3<<' '<<candidate_counter_3<<" votes"
	<<endl;
cout<<"And the winner is :";

int stored_results[3]={candidate_counter_1,candidate_counter_2,candidate_counter_3};

if(winner(stored_results,3)==1){
	cout<<candidate_1<<'\n'<<endl;
}else if(winner(stored_results,3)==2){
	cout<<candidate_2<<'\n'<<endl;
}else if(winner(stored_results,3)==3){
	cout<<candidate_3<<'\n'<<endl;
}else if(winner(stored_results,3)==4){
int highest=0;
	for(int i=0;i<3;i++)
		if(stored_results[i]>highest) highest=stored_results[i];	
	cout<<"\nTie:\n";
		for(int i=0;i<3;i++){
			if(stored_results[i]==highest){
				if((i+1)==1){
					cout<<candidate_1<<endl;
				}else if((i+1)==2){
					cout<<candidate_2<<endl;
				}else{
					cout<<candidate_3<<endl;
				}//end if..else..else
			}//end if
	}//end for
}else{
	cout<<"\nTie:\n";
	cout<<candidate_1<<' '<<candidate_2<<' '<<candidate_3<<endl;
}//end if..else..else


}//end function results

int winner(int array[],int size){
int highest=0;
int theWinnerIs;
int sameScore=0;

	for(int i=0;i<size;i++)
		if(array[i]>highest) highest=array[i]; //store the highest scored

	for(int i=0;i<size;i++){
		if(array[i]==highest){
			theWinnerIs=i+1;
		}//end if
	}//end for

	for(int i=0;i<size;i++)
		if(highest==array[i])sameScore++;

	if(sameScore>1){
		if(sameScore==2)
			theWinnerIs=4;
		else
			theWinnerIs=5;
	}//end if

return theWinnerIs;
}//end function winner 



Eyenrique-MacBook-Pro:Study Eyenrique$ ./Democracy 
PRESIDENTIAL ELECTIONS

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
v

Please enter your election: 1
PRESIDENTIAL ELECTIONS

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
v

Please enter your election: 1
PRESIDENTIAL ELECTIONS

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
v

Please enter your election: 1
PRESIDENTIAL ELECTIONS

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
v

Please enter your election: 2
PRESIDENTIAL ELECTIONS

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
v

Please enter your election: 2

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
v

Please enter your election: 2
PRESIDENTIAL ELECTIONS

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
v

Please enter your election: 3
PRESIDENTIAL ELECTIONS

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
v

Please enter your election: 3
PRESIDENTIAL ELECTIONS

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
r

Pikachu 3 votes
Son Goku 3 votes
Homer Simpson 2 votes
And the winner is :
Tie:
Pikachu
Son Goku
PRESIDENTIAL ELECTIONS

Candidates:
1.-Pikachu
2.-Son Goku
3.-Homer Simpson
Enter <v> vote, <r> results, <q> quit
q


To be honest i support Pikachu ;D
Last edited on
so i tried nevermore28's advice and it made it much easier to understand but the problem now is that it doesn't register the votes i made and doesn't quit...
#include<iostream>
#include<string>
using namespace std;
void main()
{
char choice,choice2;
int ca=0,cb=0,cc=0,tv=0;
string sa="FPJ",sb="Roco",sc="GMA";

do
{
cout<<"\t PRESIDENTIAL ELECTIONS\n\n";
cout<<"Candidates \n\n";
cout<<"<A>FPJ\t<B>Roco\t<C>GMA \n";
cout<<"-----------------------\n";
cout<<"Enter <v>vote <r> result and <q> quit : ";
cin>>choice2;
if (choice2=='v')
{switch(choice)
{cout<<"Enter your vote:";
cin>>choice;
case 'A':
case 'a':
ca++;
break;
case 'B':
case 'b':
cb++;
break;
case 'C':
case 'c':
cc++;
break;
}
}
else if (choice2=='r')
{
cout<<"\t Presidential Elections\n\n";
cout<<"Candidates:"<<"\t results:"<<"\n\n";
cout<<"<A>FPJ : "<<ca<<"\n";
cout<<"<B>Roco : "<<cb<<"\n";
cout<<"<C>GMA : "<<cc<<"\n\n";
tv=ca+cb+cc;
cout<<"Total Votes : "<<tv<<"\n";
//place "and the winner is : <name> here//
}
} while (choice !='q');
}
you can try mine now... ._____________.
i tried your source code and it has 3 errors
and this are the 3 errors
Z:\voting program\election.cpp(106) : error C2374: 'i' : redefinition; multiple initialization
Z:\voting program\election.cpp(103) : see declaration of 'i'
Z:\voting program\election.cpp(133) : error C2374: 'i' : redefinition; multiple initialization
Z:\voting program\election.cpp(130) : see declaration of 'i'
Z:\voting program\election.cpp(139) : error C2374: 'i' : redefinition; multiple initialization
Z:\voting program\election.cpp(130) : see declaration of 'i'
Error executing cl.exe.
If you see,
are counter variables from for loops
i don't have clue why you obtain these errors,
but my code compile without problems,
are you using a c++ compiler? or c compiler?
are you using correct indentation?
Last edited on
i am using a c++ compiler
and i just copy + paste...
..never mind
i fixed the voting problem now my problem is the quit part
if i use choice !='q' it wont stop
but when i use choice =='q' it will stop after any given command
#include<iostream>
#include<string>
using namespace std;
int main()
{
char choice,choice2;
int ca=0,cb=0,cc=0,tv=0;
string sa="FPJ",sb="Roco",sc="GMA";

do
{
cout<<"\t PRESIDENTIAL ELECTIONS\n\n";
cout<<"Candidates \n\n";
cout<<"<A>FPJ\t<B>Roco\t<C>GMA \n";
cout<<"-----------------------\n";
cout<<"Enter <v>vote <r> result and <q> quit : ";
cin>>choice2;
if (choice2=='v')
{ cout<<"Enter your vote:";
cin>>choice;
switch(choice)
{
case 'A':
case 'a':
ca++;
break;
case 'B':
case 'b':
cb++;
break;
case 'C':
case 'c':
cc++;
break;
}
}
else if (choice2=='r')
{
cout<<"\t Presidential Elections\n\n";
cout<<"Candidates:"<<"\t results:"<<"\n\n";
cout<<"<A>FPJ : "<<ca<<"\n";
cout<<"<B>Roco : "<<cb<<"\n";
cout<<"<C>GMA : "<<cc<<"\n\n";
tv=ca+cb+cc;
cout<<"Total Votes : "<<tv<<"\n";
//place "and the winner is : <name> here//
}
}while (choice !='q');

return 0;
}
simple: because you compare choice not choice2
Topic archived. No new replies allowed.