I\'m doing a problem and the code is not working for me. Here\'s the problem: Wr
ID: 3652932 • Letter: I
Question
I'm doing a problem and the code is not working for me.
Here's the problem:
Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate's name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election.
Here's my code:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int votes [20];
int findMax (int votes[]);
int main(int argc, char *argv[])
{
string name[10];
int total = 0;
double percent[10];
int m;
cout << "Enter the names of candidates in local election and number of votes respectively" << endl;
for (int i = 0; i < 5; i++)
{
cin >> name[i];
cin >> votes[i];
}
for (int i = 0; i < 5; i++)
{
total = total + votes[i];
}
for (int i = 0; i < 5; i++)
{
percent[i] = (double) ((votes[i]/total)*100);
}
cout << "Candidate" <<' ' <<"Votes Received" << ' ' <<"% of TotalVotes" << endl;
cout << setprecision(2);
for (int i = 0; i < 5; i++)
{
cout << name[i] <<"' ' << "' ' << "' ' << votes[i] << ' ' << percent[i] << endl;
}
cout << "Total-----" << total << endl;
m = findMax(votes);
cout << "Winner of the election is " << name[m] << endl;
system("pause");
}
int findMax(int votes[])
{
int i, max;
max = votes[0];
for (i = 0; i < 5; i++)
{
if (votes[i] > max)
max = votes[i];
}
return max;
}
Everything else looks okay to me, but it's this one line of code that's giving me the most trouble:
cout << name[i] <<"' ' << "' ' << "' ' << votes[i] << ' ' << percent[i] << endl;
What I'm I doing wrong? I'm a beginner of C++ so please go easy on me.
Explanation / Answer
this line is just suppose to tab right? Did you intend to add the ' per tab? coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.