Not displaying anything but zeros as outputs.
I'm only getting zeros as output and don't know why.
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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include<iostream>
#include<cmath>
using std::cout;
using std::cin;
using std::endl;
//declaration of functions
void roundToInteger(double);
void roundToTenths(double);
void roundToHundredths(double);
void roundToThousandths(double);
double x;
double y;
int main()
{
unsigned short choice;
do
{
float x;
cout<<"Enter a decimal"<<endl;
cin>>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<<" 5 - Quit"<<endl;
cin>> choice;
cout <<"You chose Menu Option #"<<choice<<endl;
}
while(choice=='1'||choice=='4');
switch(choice)
{
case 1 :
roundToInteger(x);
break;
case 2 :
roundToTenths(x);
break;
case 3 :
roundToHundredths(x);
break;
case 4:
roundToThousandths(x);
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 x)
{
y = floor(x+0.5);
cout<<"The number "<<x<<" rounded to the nearest integer = "<<y<<endl;
}
void roundToTenths( double x)
{
y=floor(x*10+0.5)/10;
cout<<"the decimal "<<x<<" rounded to the nearest tenths = "<<y<<endl;
}
void roundToHundredths(double x)
{
y=floor(x*100+0.5)/100;
cout<<"the decimal "<<x<<" rounded to the nearest hundredths = "<<y<<endl;
}
void roundToThousandths(double x)
{
y=floor(x*1000+0.5)/1000;
cout<<"the decimal "<<x<<" rounded to the nearest thousandths = "<<y<<endl;
}
|
Last edited on
Line 24 is causing your issue. You take the user input into the local float x but the functions are called with the global double x.
I would recommend not using the global x and y.
Thank you. Was driving me crazy. All fixed and working good.
Topic archived. No new replies allowed.