Dear the gurus;
I am a developing a small program for voting of about 5 candidates. The program should compute and return the candidates and their total tallied votes. If a voter enters a number outside 1-5 it should count it as faulty vote. I have the following program but returns an error. Kindly help
#include
#include
using namespace std;
const int vs=5;
int votesA=0, votesB=0, votesC=0, spoiltvotes=0;//TOTALS ALREADY INITIALISED-GLOBAL
char vote;
void main()
{
// loop over the voting stations
int i;
for (i=1;i<=vs ;i++ )
// loop over voters
cout<<"\n Enter your vote:\t";
cin>>vote;
while( vote<=5)
{
switch(vote)
{
case 1: votesA++;
break;
case 2:votesB++;
break;
case 3: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:"<
Check this code out.
#include<iostream>
using namespace std;
const int vs=5;
int votesA=0, votesB=0, votesC=0, spoiltvotes=0;//TOTALS ALREADY INITIALISED-GLOBAL
int vote;
int main()
{
// loop over the voting stations
int i;
for (i=1;i<=vs ;i++ )
// loop over voters
cout<<"\n Enter your vote:\t";
cin>>vote;
while( vote<=5)
{
switch(vote)
{
case 1: votesA++;
break;
case 2:votesB++;
break;
case 3: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:"<<votesB;
cout<<"\n\tNumber of votes for candidateC:"<<votesC;
cout<<"\n\nNumber of spoilt votes:\t"<<spoiltvotes;
return 0;
}
I've changed these codes:
1. removed #include<conio> (conio.h is not a standard header file. so try to avoid it.)
2. made char vote; to int vote; . cause when you are declaring vote as char it will not work as a integer value it will work as a character.
3. made void main() to int main() . because you are returning 0 from main().
4. removed getch(); . Cause it has no functionality in this program or I can't understand why you use this.