Programming Rounding Numbers

This is a discussion on Programming Rounding Numbers within the C++ Programming forums, part of the General Programming Boards category; Hello, Everyone: I am having trouble with programming a program which rounds numbers to the hundreths place. I can show ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    30

    Post 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>

    Please help me figure out my mistake. Thank you very much in advance for helping me figure out my mistake.

    Thank you,
    Mike

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    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;
    }

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    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
    Last edited by mikeprogram; 10-29-2005 at 10:57 PM.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    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 ):
    Last edited by SlyMaelstrom; 10-29-2005 at 11:06 PM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    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.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    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

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    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.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    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.

  9. #9
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    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?
    Sent from my iPadŽ

  10. #10
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    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.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    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.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    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.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  13. #13
    Registered User
    Join Date
    Oct 2005
    Posts
    30
    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;
    }

  14. #14
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote 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)
    Last edited by Xipher; 10-30-2005 at 12:03 AM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    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;
    }
    Sent from my iPadŽ

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Newbie question: rounding numbers
    By cantore in forum C Programming
    Replies: 10
    Last Post: 02-04-2006, 02:24 PM
  3. (C++) How would you go about rounding numbers?
    By jeffcoulter in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2005, 07:47 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. Rounding Numbers accurately
    By crystaldawn68 in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2001, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21