用一维数组相关知识进行选举
#include
#include
#include
using namespace std;
void input(string str[], int votes[], int* total, int* max)
{
*total = 0;
*max = 0;
cout << "Please Enter the Five Candidates' Last name" << endl;
for (int i = 0; i > str[i] >> votes[i];
if (i != 0 && votes[*max] < votes[i]) *max = i;
*total += votes[i];
}
}
void output(string str[], int votes[], int* total, int* max)
{
cout << "Candidate Votes Received % of Total Votes " << endl;
for (int i = 0; i < 5; ++i)
{
cout << str[i] << " " << votes[i] << " " << setiosflags(ios::fixed) << setprecision(2) << 100 * votes[i] * 1.0 / (*total) << endl;
}
cout << "Total " << *total << endl << endl;
cout << "The Winner of the Election is " << str[*max] << "." << endl;
}
int main()
{
//返回0
string lastName[5];
int votes[5];
double per[5];
int total;
int max;
input(lastName, votes, &total, &max);
output(lastName, votes, &total, &max);
return 0;
}