Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

BoxerRobban472

macrumors member
Original poster
Sep 12, 2013
66
1
Gothenburg, Sweden
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!:)
 
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.
 
Last edited:
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?:)
 
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.
 
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.
 
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.

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?:)

----------

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.

I actually didn't (and I don't really know why)... But hey, now I've learned two things today, thanks!:p
 
But thank you so much for your answer
No worries.

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?:)

Yep, that is correct.

I actually didn't (and I don't really know why)... But hey, now I've learned two things today, thanks!:p
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.
 
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;
}
 
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.
 
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.
 
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;
}

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?

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.

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?:)
 
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?:)

The IDE has nothing to do with it, it's the same.
 
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.
 
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.

Alright, that clears it up!

Have a nice weekend, and thanks for the help everyone!:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.