# Programming Rounding Numbers

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-29-2005
mikeprogram
Programming Rounding Numbers
Hello, Everyone:

I am having trouble with programming a program which rounds numbers to the hundreths place. I can show you my job but it does not compile.

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 >> "%lf", &x;       while( getchar() != '\n' );       cout << "Original value is: " << " x " << endl;       cout << "Rounded number is: " << " roundtohundreths( x ) " << endl;     }     cout << "Type Y for entering a number or N to end ): " << endl;     t = getchar();   }   return 0; } double roundtohundreths (double a) {   double roundto = floor( a * 100 + .5 ) / 100;   return roundto; }```
The following error is:
<pegasus> g++ numerical.cpp
numerical.cpp: In function `int main()':
numerical.cpp:17: implicit declaration of function `int getchar(...)'
<pegasus>

Thank you,
Mike
• 10-29-2005
Enahs
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; // changed       while( getchar() != '\n' );       cout << "Original value is: " <<  x  << endl; // no quotes around variable here       cout << "Rounded number is: " <<  roundtohundreths( x )  << endl; //again not quotes around this function call now     }     cout << "Type Y for entering a number or N to end ): " << endl;     t = getchar();   }   return 0; } double roundtohundreths (double a) {   double roundto = floor( a * 100 + .5 ) / 100;   return roundto; }```
• 10-29-2005
mikeprogram
Enahs,

Thank you for your help. However, I am still getting the same message as before which says that:
Code:

```<pegasus> g++ numerical.cpp numerical.cpp: In function `int main()': numerical.cpp:17: implicit declaration of function `int getchar(...)' <pegasus>```
I don't understand what is happening even though, I corrected the mistakes you said I have.

Thank you once again,
Mike
• 10-29-2005
SlyMaelstrom
I'm not gonna fix this for you, but I left some suggestions on how to do it.

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' )    // This if statement is pointless unless you     {                              // want to satisfy a condition that isn't Y or N                                   // Yes it does make sure they press Y or N, but do you                                   // know what would happen if they don't?       cout << "Enter number: ";       cin >> x;       while( getchar() != '\n' );  // This does nothing but gets a character from the input buffer                                   // which by the way, will always be a \n after your input.       cout << "Original value is: " <<  x  << endl;       cout << "Rounded number is: " <<  roundtohundreths( x )  << endl;     }     cout << "Type Y for entering a number or N to end ): " << endl;     t = getchar();    // There is a better way of doing this.   }   return 0; } double roundtohundreths (double a) {   double roundto = floor( a * 100 + .5 ) / 100;  //This doesn't work   return roundto;                                //check your logic. }```
...by the way, here is output of your code compiling. Not what you wanted, is it?
Code:

```Enter number: 5 Original value is: 5 Rounded number is: 5 Type Y for entering a number or N to end ): y Enter number: 6.5 Original value is: 6.5 Rounded number is: 6.5 Type Y for entering a number or N to end ): y Enter number: 8.2 Original value is: 8.2 Rounded number is: 8.2 Type Y for entering a number or N to end ):```
• 10-29-2005
Xipher
I agree, their is a better way of doing that. It also only takes including one line to fix it in its current state, but the other way I am thinking takes a small edit and an added variable, and I think the code would be easier to understand and read.
• 10-29-2005
mikeprogram
SlyMaelstrom,

I appreciate your pointers. I want numbers which rounds to the hundredth place. For example, a person enters 6.23569 and the result in the hundredth place is 6.24. However, I still don't understand what you want me to change. Please explain me what do you really mean?

Code:

`t = getchar()`
I think I would change to

Code:

`t = getchar(x)`
Thank you
• 10-29-2005
Xipher
nope, thats not it.

first, I don't think you realize where the definition of getchar is.
second, I see no reason to use C input function, when C++ input streams would work just fine. the fact that your using them already makes me wonder why your using getchar at all, and where you got the idea to use it here.
• 10-29-2005
mikeprogram
Xipher,

I am trying to translate a program from C to C++ language.

Its my old program... that is where i got the getchar from.

I dont have much experience using C++ language. That is why I am asking you to help me.
• 10-29-2005
SlyMaelstrom
Ooo, I see what you want it to do. In that case your logic fine. :) Sorry.

Nah, nah, that's not what you want to change it to. Especially considering variable x is a double and wouldn't accept a Y or N.

The reason why you don't want getchar() is because it works on a buffer. You type in a char, you press enter, it returns the character. ...but you still have another character in your input stream, do you know what it is? What form of input do you know that ignores that last character?
• 10-29-2005
Xipher
ok, drop the getchar line, change the type of t to char, and add a line to get a character from standard input into t, the same thing your using to get the doubles, the >> operator.
• 10-29-2005
mikeprogram
SlyMaelstrom,

Once again, thank you for your commentaries, I am still stuck with getchar(),

My problem is that im translating from C to C++ language programming which I am not sure if getchar() would be written in C++ language as:

Code:

`int x, getchar`
and then change
Code:

` while( getchar() != '\n' );`
to the following:
Code:

`getchar = roundtohundreths( x )`
Im really not sure, but once again thank you for your time. :)
• 10-29-2005
Xipher
your use of getchar was just fine, its simply that the headers your including do not define it at all, Im guessing your expecting it to be in iostream, but its not.

Im guessing you haven't gotten around to reading my post yet, so Ill wait for a response.
• 10-29-2005
mikeprogram

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; }```
• 10-29-2005
Xipher
Quote:

Originally Posted by mikeprogram

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)
• 10-29-2005
SlyMaelstrom
Try this:

Code:

```#include<iostream> using namespace std; #include<cmath> double roundtohundreths (double); int main() {   char t = 'Y';   double x;   while( t != 'N' && t != 'n' )   {     if ( t == 'Y' || t == 'y' )     {                                  cout << "Enter number: ";       cin >> x;       cout << "Original value is: " <<  x  << endl;       cout << "Rounded number is: " <<  roundtohundreths( x )  << endl;     }     cout << "Type Y for entering a number or N to end ): " << endl;     cin >> t;   }   return 0; } double roundtohundreths (double a) {   double roundto = floor( a * 100 + .5 ) / 100;    return roundto; }```
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last