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<iostream>
#include<conio>
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:"<<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();
return 0;

}

use

int vote;

in place of

char vote;

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<iostream>
#include<conio>
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:"<<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();
return 0;

}

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.

a question from a learner:
1. What's the purpose of the 'for' loop, does that not just print "Enter your vote" 5 times?

How about this?

#include<iostream> using namespace std; const int vs=5;int votesA=0, votesB=0, votesC=0, spoiltvotes=0;//TOTALS ALREADY INITIALISED-GLOBALint vote;int main(){// loop over the voting stationsint i;for (i=1;i<=vs ;i++ ) // loop over voterscout<<"\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 formatcout<<"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; }#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;

      switch(vote)
      {
         case 1: votesA++;
         break;

         case 2:votesB++;
         break;

         case 3:votesC++;
         break;

         default:spoiltvotes++;
      }

    }


// 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;

}

All right. As the description of the program given above, I think you can use this. It is modification of radiat's code. In my previous code I just gave you a way to avoid error but this is the code , I think you want.

#include <iostream>

using namespace std;

int main() {
    
// loop over the voting stations
   for (int i=1;i<=vs ;i++ )
   {
      cout<<"\n Enter your vote:\t";
      cin>>vote;
 
      switch(vote)
      {
         case 1: votesA++;
         break;
 
         case 2:votesB++;
         break;
 
         case 3:votesC++;
         break;
 
         default:spoiltvotes++;
      }
 
    } 
 
// display the results in a neat presentable format
cout<<endl<<"Number of votes for candidate A:\t"<<votesA<<endl;
cout<<"Number of votes for candidate B:\t"<<votesB<<endl;
cout<<"Number of votes for candidate C:\t"<<votesC<<endl;
cout<<"Number of spoilt votes :\t\t"<<spoiltvotes;
    
    return 0;
}

Here I use endl for formatted output.

Hello I am a c++ beginer and has been consulted to write a program by group members that enables them choose whom to be contributed for every time they meet, assist me on how to go about this please!

commented: "Bury it in the desert." "Wear gloves." https://xkcd.com/2030/ +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.