0

Hi so im creating a program to have the user input 5 candidates and votes then the program spits back out the winner and percent of each candidate but i cant seem to figure out why the double varible dont show as a number like 40.5 or something just 0 and 100. I am still learning how this works

string candidate[5];
int votes[5];
int total;
double percent[5];
string winner;

cout << "Enter the first candidate: ";
cin >> candidate[0];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[0];

cout << endl << "Enter the second candidate: ";
cin >> candidate[1];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[1];

cout << endl << "Enter the third candidate: ";
cin >> candidate[2];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[2];

cout << endl <<"Enter the fourth candidate: ";
cin >> candidate[3];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[3];

cout << endl << "Enter the fifth candidate: ";
cin >> candidate[4];
cout << "Enter the amount of votes he/she recieved: ";
cin >> votes[4];

total = votes [0] + votes[1] + votes[2] + votes[3] + votes[4];

percent[0] = (votes[0] / total * 100);

percent[1] = (votes[1] / total * 100);

percent[2] = (votes[2] / total * 100);

percent[3] = (votes[3] / total * 100);

percent[4] = (votes[4] / total * 100);

if (votes[0] > votes[1] && votes[2] && votes[3] && votes[4])
    winner = candidate[0];
    
if (votes[1] > votes[0] && votes[2] && votes[3] && votes[4])
    winner = candidate[1];
    
if (votes[2] > votes[1] && votes[0] && votes[3] && votes[4])
    winner = candidate[2];
    
if (votes[3] > votes[1] && votes[2] && votes[0] && votes[4])
    winner = candidate[3];
    
if (votes[4] > votes[1] && votes[2] && votes[3] && votes[0])
    winner = candidate[4];
    
cout << "Candidate" << setw(5) << "Votes" << setw(5) << "Percent" << endl;
cout << "------------------------------------------------" << endl;
cout << candidate[0] << setw(5) << votes[0] << setw(5) << percent[0] << endl;
cout << candidate[1] << setw(5) << votes[1] << setw(5) << percent[1] << endl;
cout << candidate[2] << setw(5) << votes[2] << setw(5) << percent[2] << endl;
cout << candidate[3] << setw(5) << votes[3] << setw(5) << percent[3] << endl;
cout << candidate[4] << setw(5) << votes[4] << setw(5) << percent[4] << endl;
cout << "Winner: " << winner << endl;
cout << "Total Votes: " << total;
return 0;}
Ducksfan
  • 1
  • 2
  • 2
    You are using integer math instead of floating-point math when calculating the percentages. Also, statements like `if (votes[0] > votes[1] && votes[2] && votes[3] && votes[4])` don't do what you think they do, you need to write them more like this instead: `if (votes[0] > votes[1] && votes[0] > votes[2] && votes[0] > votes[3] && votes[0] > votes[4])`. Or better, use [`std::max_element()`](https://en.cppreference.com/w/cpp/algorithm/max_element) instead. Also, you are not taking into account if there is a tie between winning candidates. – Remy Lebeau Mar 03 '21 at 22:59

1 Answers1

0

All of the variables in the calculation on the right hand side of your assignment are ints, so the result is an int.

You would have to convert the operands to doubles to get a double result.

percent[0] = (double(votes[0]) / double(total) * 100);
eemz
  • 1,183
  • 6
  • 10