rounding
Oct 4, 2013 at 11:28pm UTC
this program is supposed to output rounded numbers for a whole number,
and decimal places for tenth, hundredth, and thousandth, but my output
only outputs whole numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int whole(double );
int tenth(double );
int hund(double );
int thous(double );
int main()
{
int a, b, c, d;
double num;
cout << "Input a number to be rounded to the nearest whole number, tenth, hundredth, and thousandth place." << endl;
cin >> num;
a = whole(num);
cout << a << endl;
b = tenth(num);
cout << setprecision(1) << fixed << b << endl;
c = hund(num);
cout << setprecision(2) << fixed << c << endl;
d = thous(num);
cout << setprecision(3) << fixed << d << endl;
return 0;
}
int whole(double x)
{
double y = floor (x + .5);
return y;
}
int tenth(double x)
{
double y = floor (x * 10 + .5) / 10;
return y;
}
int hund(double x)
{
double y = floor (x * 100 + .5);
y = y / 100;
return y;
}
int thous(double x)
{
double y = floor (x * 1000 + .5) / 1000;
return y;
}
Oct 4, 2013 at 11:57pm UTC
Take a look at your return types.
Oct 5, 2013 at 1:36am UTC
oh wow, thank you.
Topic archived. No new replies allowed.