Voting program

Hi good friends. I am designing a program that accepts users choice from the listed candidates. The candidates are tallied and listed according to the votes casted against them. The program then returns the winner and the number of faulty votes. When I run the below code, I get 1 error of which I dont know where its coming from. Please help.


#include<iostream>
#include<conio>
using namespace std;
const int vs=4;
int votesA, votesB, votesC, spoiltvotes;//TOTALS ALREADY INITIALISED-GLOBAL
char vote;
void main()
{
// loop over the voting stations
for ( int i=1;i<=vs ;i++ )

// loop over voters
cout<<"\n Enter your vote:\t";
cin>>vote;
while( vote!='X')
{
switch(vote)

case A: votesA++;
break;
case B:votesB++;
break;
case 'C:votesC++;
break;
default:spoiltvotes++;

cout<<"\n Enter your vote:\t";
cin>>vote;
}


// display the results in a neat presentable format
cout<<"Number of votes for candidate\n\tA:"<<votesA;
cout<<"Number of votes for candidate B:";
cout<<votesB<<"\n\tNumber of votes for candidateC:"<<votesC;
cout<<"\n\nNumber of spoilt votes:\t"<<spoiltvotes;

getch()

}

Change void main() to int main() and put return 0 at the end of your code.

You need to use brackets in your for loop and switch statement, i.e.

for (...
{

// code here

switch(vote)
{
// cases here
}

}

Need to put ' ' around your A,B,C cases so it sees it's a character.

Need a ; at the end of getch();

I think conio should be conio.h should it not?

You haven't initialized votesA, votesB, votesC or spoiltvotes to anything so set them equal to zero.



And use tags, makes it easier to read.
Last edited on
Oh yeah just noticed the cin problem, read this.

http://www.cplusplus.com/forum/articles/6046/
Topic archived. No new replies allowed.