Answer to Question #13980 in C++ for mac eleazar

Question #13980
write a C++ program for Presidential Elections, the user will enter his votes 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 in accepting votes if the user enterde letter 'Q', 'V' to vote 'R' to view the result.
1
Expert's answer
2012-08-31T08:06:18-0400
#include <iostream>
using namespace std;

void Menu();
void Result(char candidates[][20], int *results);
int SelectMaxVotes(int *arrcandidates);

int main()
{
char candidates[4][20] = {"Candidate1","Candidate2","Candidate3","Candidate4"};
int results[4] = {0};
while(true)
{
Menu();
cout<<"Select one item: ";
char choice;
cin>>choice;
switch(tolower(choice))
{
case 'q':
cout<<"The winner is "<<candidates[SelectMaxVotes(results)]<<"!
";
return 0;
break;
case 'r':
cout<<"
Temporary results:
";
Result(candidates,results);
break;
case 'v':
cout<<"Enter candidate number: ";
int n;
cin>>n;
results[n-1]++;
break;
}
cout<<endl;
}
return 0;
}

void Menu()
{

cout<<"V - vote"<<endl;
cout<<"R - view results"<<endl;
cout<<"Q - quit"<<endl;
}

void Result(char candidates[][20], int
*results)
{
cout<<"
Results:
";
for (int i=0;i<4;i++)
{
cout<<i+1<<". "<<candidates[i]<<" "<<results[i]<<endl;
}
}
int SelectMaxVotes(int *arrcandidates)
{
int max=0;
for (int i=1;i<4;i++)
{
if (arrcandidates[i]>arrcandidates[max])
max = i;
}
return max;
}

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
APPROVED BY CLIENTS