Click here to Skip to main content
14,735,522 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am having a problem with finding percentage. When displaying output from math problems i need: # complete, Right, Wrong, and percentage right. All but percentage comes out correct. If all problems are correct it shows 100% anything else is 0%. What am I doing wrong.

cout << setw(17) << "Addition: " << setw(16) << TotalAddProb << setw(17) << AddCountRight << setw(11) << AddCountWrong << setw(11) << setprecision(2) << fixed << ((AddCountRight) / (TotalAddProb) *100) << "\n";

1 solution

Try changing this:
((AddCountRight) / (TotalAddProb) *100)

To this:
(AddCountRight * 100.0 / TotalAddProb)

In your version, you are performing integer division, so the result gets truncated to 0 then multiplied by 100 (the result of which is 0). In my version, I convert the integer to a floating point number, then perform the rest of the math (which avoids the integer division).
Alecroy 24-Sep-10 21:58pm
Reason for my vote of 5
Automatic vote of 5 for accepting answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900