Rounding Decimals to Whole Numbers for Percent Output

Hello,

I'm having a problem, I don't know how to get the output from my equation to come out as a rounded whole number.

im sending whole integers into the void function below and then i want to plug them into the equations and end up with a whole percent of number correct.
How should i do this? Am i using the wrong data types? or is there something else i neded to do?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void printReport(int probsPerSet, int numCorrect1, int numCorrect2, int numCorrect3)
{
    int percCorrct1, percCorrct2, percCorrct3, TotalPercCorr;
    int TotalCorrect, TotalProbs;
    cout.precision(0);
 
    percCorrct1 = (float)numCorrect1/probsPerSet*100;
    percCorrct2 = (float)numCorrect2/probsPerSet*100;
    percCorrct3 = (float)numCorrect3/probsPerSet*100;
    TotalCorrect = numCorrect1 + numCorrect2 + numCorrect3;
    TotalProbs = 3*probsPerSet;
    TotalPercCorr = (float)TotalCorrect/TotalProbs*100;
 
    cout <<"Set#1:  You got " <<numCorrect1<<" correct out of " <<probsPerSet<< " for " << percCorrct1<<endl;
    cout <<"Set#2:  You got " <<numCorrect2<<" correct out of " <<probsPerSet<< " for " <<percCorrct2<<endl;
    cout <<"Set#3:  You got " <<numCorrect3<<" correct out of " <<probsPerSet<< " for " << percCorrct3<<endl;
    cout <<"Overall you got " <<TotalCorrect<<" correct out of " <<TotalProbs<< " for " <<TotalPercCorr <<endl;
}
You can caste the final decimal percentage as an int which will truncate it to the nearest whole number below its approximate value or use the floor() or ceil() functions included in the <cmath>
header. All are dealt with in the above ref by Bazzy.
Topic archived. No new replies allowed.