Originally Posted by
mikeprogram
I made the following changes:
I am not getting any error messages, however, its taking forever to display an answer and it never give an answer.
What is wrong now?
Thank you for any suggestions.
Code:
#include<iostream>
using namespace std;
#include<cmath>
double roundtohundreths (double);
int main()
{
int t = 'Y';
double x;
while( t != 'N' && t != 'n' )
{
if ( t == 'Y' || t == 'y' )
{
cout << "Enter number: ";
cin >> x;
while( char() != '\n' ); // changed from getchar() to char()
cout << "Original value is: " << x << endl;
cout << "Rounded number is: " << roundtohundreths( x ) << endl;
}
cout << "Type Y for entering a number ( N to end ): " << endl;
t = char(); // changed from getchar() to char()
}
return 0;
}
double roundtohundreths (double a)
{
double roundto = floor( a * 100 + .5 ) / 100;
return roundto;
}
Ok, Ill show you what I mean
Code:
#include<iostream>
using namespace std;
#include<cmath>
double roundtohundreths (double);
int main()
{
char t = 'Y'; // we don't use ints in C++ for characters, as we can
// use other functions to check for things like end of file and such
double x;
while( t != 'N' && t != 'n' )
{
if ( t == 'Y' || t == 'y' )
{
cout << "Enter number: ";
cin >> x;
// Get rid of the while not newline business, no need for it
cout << "Original value is: " << x << endl;
cout << "Rounded number is: " << roundtohundreths( x ) << endl;
}
cout << "Type Y for entering a number ( N to end ): " << endl;
cin >> t; // use the >> operator, this is whats known as C++ style IO
}
return 0;
}
double roundtohundreths (double a)
{
double roundto = floor( a * 100 + .5 ) / 100;
return roundto;
}
EDIT: fixed, I missed the fact you had that silly newline check, that isn't nessacary when using C++ IO, as the stream deals with all of that. Now in this current
Oh, and some sample output
Code:
Enter number: 6.523
Original value is: 6.523
Rounded number is: 6.52
Type Y for entering a number ( N to end ):
Y
Enter number: 6.549
Original value is: 6.549
Rounded number is: 6.55
Type Y for entering a number ( N to end ):
n
BTW, I didn't add ANY checks to make sure the users input was valid, which I would suggest adding (and should be a trivial addition really)