c++ program

can i ask how to put a total number of voters in my code...


#include <iostream>

#define N 3
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] = {"FPJ","ROCO","GMA"};
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;
}
Code tags would make commenting easier.

"total voters" as in "total number of votes given" as in "sum of the elements of the array results"? Read the documentation of std::accumulate.

Now, a question for you: I do enter the number of my favourite candidate: 42. What do I win?
Topic archived. No new replies allowed.