Problem with rounding off numbers

Discussion in 'Mac Programming' started by BoxerRobban472, Mar 18, 2015.

  1. BoxerRobban472 macrumors member

    Joined:
    Sep 12, 2013
    Location:
    Gothenburg, Sweden
    #1
    Hello!

    I'm currently learning C++ and I have kind of gone stuck on this task:

    "Write a program that, with help of type conversion, converts a entered decimal number to the closest integer. For example, the number 35.8 would be rounded off to 36."

    This is what I have written:
    Code:
    int main(int argc, const char * argv[]) {
    	float number;
    	cout << "enter a number: ";
    	cin >> number;
    	cout << "The number is: " << (int)number << endl;
    	return 0;
    The problem is, that if I enter for example 35.8, it prints out 35. How do I make it round off to the closest number? I'm using Xcode 6.1, if that's to any help.

    Thanks in advance!:)
     
  2. robvas, Mar 18, 2015
    Last edited: Mar 18, 2015

    robvas macrumors 68020

    Joined:
    Mar 29, 2009
    Location:
    USA
    #2
    You could do something like

    x = 36.2
    y = (int)x // would be 36

    x - y == .2

    since .2 is less than .5, you can just round using (int)x
    If it was >= .5, you could round using (int)x + 1

    You're just checking the decimal point.
     
  3. BoxerRobban472 thread starter macrumors member

    Joined:
    Sep 12, 2013
    Location:
    Gothenburg, Sweden
    #3
    Thank you very much for the fast answer, robvas! It worked like a charm! I have a few questions though, I used an if-statement when implementing the solution you suggested. Unfortunately, the book hasn't gotten to if-statements just yet, so I think they had an idea that you were supposed to do this without an if-statement. Do you have any idea how that is possible?:)
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    Did you try searching?

    When I google c++ round to integer it's easy to find lots of examples.

    Learning to search effectively is an important programming skill. Really.
     
  5. theSeb macrumors 604

    theSeb

    Joined:
    Aug 10, 2010
    Location:
    Poole, England
    #5
    This will work, but not for negative numbers.

    Code:
    
    cout << "The number is: " << (int)(number+0.5) << endl;
    
    This is basically a "hack" and to do it properly you should be using math.h, but perhaps the book does not expect you to yet. If you don't understand why the above code works, just shout.
     
  6. BoxerRobban472 thread starter macrumors member

    Joined:
    Sep 12, 2013
    Location:
    Gothenburg, Sweden
    #6
    Oh! That's clever! I saw your post a while ago but didn't really understand it, but I thought about it a little bit and I just got it! That's really cool!
    I must admit, I am a little curious about how this could be done in a way that would work with negative numbers so I did a quick Google search but all solutions I found used some kind of function... I guess it's hard to do that without using functions/if-statements. But thank you so much for your answer!:)

    Off-topic: The book haven't mentioned math.h yet, but I think I read about it somewhere and as I understand it is some kind of library of functions related to math, is that correct?:)

    ----------

    I actually didn't (and I don't really know why)... But hey, now I've learned two things today, thanks!:p
     
  7. theSeb macrumors 604

    theSeb

    Joined:
    Aug 10, 2010
    Location:
    Poole, England
    #7
    No worries.

    Yep, that is correct.

    Yeah, this is something really important to be able to do. There is a wealth of information out there and plenty of people willing to give it away. You will find that people often ask the same question. The trick is to find that information, but it becomes easier with experience, just like everything else.
     
  8. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #8
    It's frequently implemented as int i = double + 0.5, *BUT* this might not work the way you want with negative numbers.

    To be sure, you might want to do something like this:

    Code:
    int roundRealNumber(double realNumber)  //Changed to work with negative numbers - *ROUNDS UP*:
    {                                                 // 1.25 = 1
    	double integerPart;                           // 1.75 = 2
    	double fractionPart;                          // -1.25 = -1
    	int roundedInteger;                           // -1.75 = -2
    
    	fractionPart = modf(realNumber, &integerPart);
    	roundedInteger = integerPart;
    	
    	if(realNumber > 0 && fractionPart >= 0.5)
    		roundedInteger += 1;
    	if(realNumber < 0 && fractionPart <= -0.5)
    		roundedInteger -= 1;
    	
    	return roundedInteger;
    }
     
  9. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #9
    Assigning floating point values to integers truncates the result. You can use round() from cmath or math.h to get rounding of your floating point number.
     
  10. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #10
    round(1.25) = 1
    round(1.75) = 2
    round(-1.25) -1
    round(-1.75) -2

    be sure this is what you want;

    round() probably does something like the above code.
     
  11. BoxerRobban472 thread starter macrumors member

    Joined:
    Sep 12, 2013
    Location:
    Gothenburg, Sweden
    #11
    Thank you very much for your answer! I just got to ask one question (which I guess is a little off-topic); What does the 'return roundedInteger;'-line do? Or, to be more precise, what does the 'return'-statement do? I have tried googling several times before, but I don't think I have ever fully understood it. I think it does something in style with terminating all processes that includes the variable roundedInteger, and stores the value roundedInteger got from the last process/operation/etc. it was included in. Is that correct?

    Is that something that applies to all C++-code, no matter what IDE it's written on? In the book I'm reading, they're using C++ Visual Express but since I'm on Mac I'm using Xcode and I thought maybe that could have something to do with it, but maybe it doesn't matter?:)
     
  12. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    The IDE has nothing to do with it, it's the same.
     
  13. BoxerRobban472 thread starter macrumors member

    Joined:
    Sep 12, 2013
    Location:
    Gothenburg, Sweden
    #13
    Great, cheers!
     
  14. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #14
    The function prototype specifies that it returns an int to the calling environment. if you didn't return the rounded int, it wouldn't and your IDE would complain. When a return() statement is encountered the function returns a variable (if the function isn't declared as void), terminates execution of the function and deallocates memory for the local variables declared in the function.
     
  15. BoxerRobban472 thread starter macrumors member

    Joined:
    Sep 12, 2013
    Location:
    Gothenburg, Sweden
    #15
    Alright, that clears it up!

    Have a nice weekend, and thanks for the help everyone!:)
     

Share This Page