Answer to Question #13548 in C++ for jef
2012-08-23T08:30:15-04:00
write c++ program for presidential election, the user will enter his vote 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.
1
2012-08-28T10:27:58-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 !
Learn more about our help with Assignments:
C++
Comments
Leave a comment