Answer to Question #13582 in C++ for mark

Question #13582
write a c++ program for presidential election,the user will enter his vote based in 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
1
Expert's answer
2012-08-30T09:31:02-0400
#include <iostream>

#define N 4
using namespace std;

void ShowMenu(char candidates[][20])
{
cout<<"\tList of candidates:\n";
for (int i=0;i<N;i++)
{
& cout<<i+1<<". "<<candidates[i]<<endl;
}
cout<<"\tAvaliable inputs:\n";
cout<<"V - vote"<<endl;
cout<<"R - view results"<<endl;
cout<<"Q - quit"<<endl;
}

void ShowResult(char candidates[][20], int *results)
{
cout<<"\nResults:\n";
for (int i=0;i<N;i++)
{
& cout<<i+1<<". "<<candidates[i]<<"\t"<<results[i]<<endl;
}
}
int Max(int *arr)
{
int max=0;
for (int i=1;i<N;i++)
{
& if (arr[i]>arr[max])
max = i;
}
return max;
}

int main()
{
char candidates[N][20] = {"Candidate1","Candidate2","Candidate3","Candidate4"};
int results[N] = {0};
while(true)
{
& ShowMenu(candidates);
& cout<<"Your choice: ";
& char choice;
& cin>>choice;
& switch(tolower(choice))
& {
case 'q':
cout<<"The winner is "<<candidates[Max(results)]<<"!\n";
return 0;
break;
case 'r':
cout<<"\nTemporary results:\n";
ShowResult(candidates,results);
break;
case 'v':
cout<<"Enter candidate number: ";
int n;
cin>>n;
results[n-1]++;
break;
& }
& cout<<endl;
}
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS