Hello! I am trying to make a fraction calculator for an assignment. In summary, one of my variables (totalPercent) is set to equal a value (correct) divided by a total. However, despite any alterations I have done, the variable is always equal to 0. For instance, if I get 2 out 3 problems correct I will be given 0.00% However, if I make the program display the value for each individual variable only totalPercent gives the incorrect output that I am expecting.
My directions for this particular area of the program is:
The program must be able to display the number of correct answers and percentage with 2 digits after the decimal point.
Are lines 1 - 8 the final lines of your program? Because you set correct equal to 0 on line 4, so when you calculate total percent on line 7 the result would be 0/3, which gives you 0.
Ah! I see it now. Line 7 of what you originally posted: totalPercent = static_cast<double> (correct/total) * 100;
It isn't being cast correctly. correct/total will evaluate first before it is cast a double. That means it will follow the rules of integer division, so 2/3 will evaluate to 0 as it has been doing. Try something like casting one of them as a double: totalPercent = static_cast<double>correct / total * 100;
(Also, when you use the increment operator, you only need to use it by itself. You don't need count = count++. count++ will do the same exact thing in fewer keystrokes).