the question that i have to solve says:
fuction floor can be used to round a number to a specific decimal place. the statement y=floor(x*10+0.5)/10; rounds x to the tenths position. write a program that defines four functions to round a number x in various ways:
1- roundToInteger(number)
2- roundToTenths(number)
3-roundToHundredths(number)
4- roundToThousandths(number)
for each value read, your program should print the original value, the number rounded to nearest tenths,hundredths,thousandths.
this is what i have tried to write..but ofcourse..and like usual..lol..i have lots of mistakes..i would really appreciate the help
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
# include<cmath>
//declaration of functions
void roundToInteger(double);
void roundToTenths(double);
void roundToHundredths(double);
void roundToThousandths(double);
int main()
{
unsigned short choice;
do
{
float x;
cout<<”-1- round decimal to the nearest integer”<<endl;
cout<<”-2- round decimal to the nearest tenth”<<endl;
cout<<”-3- round decimal to the nearest hundredth”<<endl;
cout<<”-4- round decimal to the nearest thousandth”<<endl;
cout<<”please enter a decimal”<<endl;
cin>>x;
cout <<"You chose Menu Option #"<<choice<<endl ;
}
while(selection<1 || selection>4);
switch(choice)
{
case 1 :
roundToInteger(number);
break ;
case 2 :
roundToTenths(number);
break;
case 3 :
roundToHundredths(number);
break;
case 4:
roundToThousandths(number);
break;
default:
cout<<”you should never get here\a\a\a\a!!!”;
}
}while(choice != 5);
cout<<”you choose to quit\a\a\a"<<endl;
return 0;
}
//definition of functions
void roundToInteger(double number)
{
y= floor(x+0.5);
cout<<”the decimal ”<<x<<” rounded to the nearest integer = “<<number<<endl;
}
void roundToTenths( double number)
{
y=floor(x*10+0.5)/10;
cout<<”the decimal ”<<x<<” rounded to the nearest tenths = “<<number<<endl;
}
void roundToHundredths(double number)
{
y=floor(x*100+0.5)/100;
cout<<”the decimal ”<<x<<” rounded to the nearest hundredths = “<<number<<endl;
}
void roundToThousandths(double number)
{
y=floor(x*1000+0.5)/1000;
cout<<”the decimal ”<<x<<” rounded to the nearest thousandths = “<<number<<endl;
}