+ 1

C++ Percentage Error help

void main() { int sub1,sub2,sub3,sub4,sub5,total_percentage; clrscr(); cout<<"enter sonething\n"; total_percentage=(sub1+sub2+sub3+sub4+sub5)/500; cout<<"the total\n"<<total_percentage; getch(); } it is showing 0 always.

11th Mar 2018, 5:46 PM
Robin
Robin - avatar
2 Réponses
+ 3
(int + int + …) is int. 500 is int. int / int gives the expected result rounded off. 2/3 = 0 5/3 = 1 25/2 = 12 You must do a static_cast<double>(one_of_your_division_operands) It converts the argument to a double precision floating point number.
11th Mar 2018, 6:16 PM
Timon Paßlick
+ 1
thanks sir
12th Mar 2018, 4:17 PM
Robin
Robin - avatar