Confused: Negative #'s and If statements

Started by
4 comments, last by Sc4Freak 17 years, 6 months ago

void AFunction(int ivar)
{
	if(ivar <= -1)
	{
		cout<< "Invalid Answer! Enter a Valid One!";
	}
	else
	{
		//other code
	}
}



Basically this functions takes the input that a user enter and makes sure that he/she doesn't enter a negative number (or possibly the answer becomming negative for some reason or another). Thing is, this code runs even when the answer is not a negative number. It gets set off with 1 (possibly other numbers higher than 1). So it screws up the way I want my program to run. Sense I have not had to work with negative numbers in programming yet, I don't know if the part: ivar <= -1 is valid or not. I have not read, at least not to my recollection, about using negative numbers in this way is invalid. I've done a google search, and I searched these forums, and I haven't come up with anything usefull. If anyone has any insight on this problem of mine, please say something (if you feel like it of course). NOTE: Code is in C++ Thanks for reading.
Cheers,Ken(Koolchamp)_____________________________"Choose a job you love, and you will never have to work a day in your life." - Confucius"If you don't have a game industry job and you want to go to E3, do what everyone else does. Launch a game review website and call yourself press." - Mike McShaffry(This is true for me) “…..I'm a geek and jocks are my natural enemy.” – Li C. Kuo
Advertisement
That code is perfectly valid, and should do as you expect. However, perhaps for some reason, ival is not what you think it is. Try sending it to cout along with your error message.

Or better, use the debugger to step through your code and check the values of variables at different points in time. The debugger is a very valuable tool. Learning to use it early on will be very useful.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Surprisingly I did use the debugger. It says that the variable that is taken in is equal to 1. Thats what got me really confused, because it should skip over it.

EDIT: I just added the code to show what ivar equals. It equals 1.

EDIT #2: I kinda lied in the first post of mine. The int argument is an unsigned int. Could that be the problem? Sense unsigned variables can't hold negative numbers?

[Edited by - Koolchamp on October 6, 2006 10:16:22 PM]
Cheers,Ken(Koolchamp)_____________________________"Choose a job you love, and you will never have to work a day in your life." - Confucius"If you don't have a game industry job and you want to go to E3, do what everyone else does. Launch a game review website and call yourself press." - Mike McShaffry(This is true for me) “…..I'm a geek and jocks are my natural enemy.” – Li C. Kuo
Unsigned int? That'll do it. Switch to a signed one.
It is quite possible that since ivar is unsigned, it is converting all numbers that are compared to it to an unsigned int as well. The bit-pattern for -1, when interpretted as an unsigned int instead of a signed int, is +4294967295 (on a 32-bit machine, using the standard 2's complement form), number that is most definitely greater than ivar, thus the if-statement would evaluate to true.

Most compilers will give you a warning about doing a signed/unsigned comparison, and the warning is because of problems just like this. Something may look fine superficially, but things are happening behind the scenes that might make it do something other than you expected.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Quote:Original post by Koolchamp
EDIT #2: I kinda lied in the first post of mine. The int argument is an unsigned int. Could that be the problem? Sense unsigned variables can't hold negative numbers?

Yep, that's exactly it. "Unsigned" means that it doesn't have a sign (positive or negative) so unsigned numbers are always positive.
NextWar: The Quest for Earth available now for Windows Phone 7.

This topic is closed to new replies.

Advertisement