need help coverting float to integer

This is a discussion on need help coverting float to integer within the C++ Programming forums, part of the General Programming Boards category; Im writing a program for a course in school. We need to round our years of service. The user inputs ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    3

    need help coverting float to integer

    Im writing a program for a course in school. We need to round our years of service. The user inputs a value that is float, so they can enter 28.3 or 28.6 etc... We need to round the value so we can output it as an integer value. so 28.3 needs to be 28 and 28.6 needs to be 29. Can anyone tell me how to do this conversion???

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this round function.
    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    double round( double num );
    
    int main(void)
    {
       double a = 28.3;
       double b = 28.6;
    
       cout << round( a ) << endl;
       cout << round( b ) << endl;
    
       return 0;
    }
    
    double round( double num )
    {
       return floor(num + 0.5);
    }

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    3
    Thank you so much swoopy!

  4. #4
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    It's worth pointing out, though, that the whole point of learning a programming language is so that you can use the building blocks provided by that language to create your own solutions to problems. The question you posed was trivial to solve, even for a beginner, with just a little bit of effort, as long as you understand what 'rounding' means. If you're unable to even attempt to solve such a trivial problem you may need to rethink learning to program at all.

    Unfortunately, you missed an opportunity to develop some useful problem solving skills this time round.

    ;-)

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    3
    you know i dont plan on programming for a career. i missed a couple classes already because i play for the college soccer team. i needed a little help, is that too much to ask?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 04:25 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 10:42 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 11:53 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Repetition in do{}while
    By Beast() in forum C Programming
    Replies: 25
    Last Post: 06-16-2004, 11:47 PM

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