Help! My average variable is not converting from an integrer into a double. I think there is smth wrong with the static cast.
#include %26lt;iostream%26gt;
#include %26lt;cmath%26gt;
#include %26lt;iomanip%26gt;
using namespace std;
int main()
{
int num;
int count=0;
int total=0;
do{
cout %26lt;%26lt; "Enter positive number press -1 to exit\n";
cin %26gt;%26gt; num;
if (num %26gt; 0){
total+=num;
count++;}
}while (num != -1);
double average = double(total/count); // is there smth wrong?
cout %26lt;%26lt; "Total is " %26lt;%26lt; total %26lt;%26lt; endl;
cout %26lt;%26lt; "Count is " %26lt;%26lt; count %26lt;%26lt; endl;
cout %26lt;%26lt; "Average of non negative numbers is " %26lt;%26lt; fixed %26lt;%26lt; showpoint %26lt;%26lt; setprecision(2) %26lt;%26lt; average %26lt;%26lt;endl;
return 0;
}
C++ programming help?
Integer operated with integer is integer. change it to double(total)/count
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment