When you say doesn't output anything, do you mean it just stops doing anything, or the program stops without printing your last line?
Compiling problems
Collapse
X
-
No - you are not performing any test on the number.
An if statement (and loops) require boolean logic to be executed. Usually this consists of a check such as num > 0, which evaluates the true when num is greater than 0. But what you have is if (true) - true always evaluates to true. True is never false. You should try something like this:
Code:if (num is positive) { // Reverse using positive method } else { // num isn't positive, so it must be negative // Reverse using negative method }
Comment
-
Originally posted by Trev17ahhh, that makes sense. so if i put the if statement like this:
if ((num>0) == true)
the else would make it false and the if statement would not come out true if i did input a negative. right?
With the above code in it still returns 0 for negative numbers
First, the inside parentheses will be evaluated. Is num > 0? Let's assume num is positive. Then num > 0 evaluates to true, so your if statement is essentially if (true == true). Well, true == true, so true == true evaluates to true. This final, single true determines if the statement will execute.
But you can cut this short by just having num > 0 - if num is positive, this statement evaluates to true, and the if statement is satisfies.
Again, if you keep it as is, it will work, but it takes more room for no gained productivity.Comment
-
theres still a problem somewhere in my reverseDigits function. I think it has to do with what i'm declaring in the while statement but it keeps giving me 0 as the output for a negative reverse function.
int reverseDigit(in t rev)
{
int num = rev, rnum = 0, digit;
while (num>0)
{
if (num>0)
{
rnum *= 10;
digit = num % 10;
rnum += digit;
num /= 10;
}
else
{
rnum *= 10;
digit = num % 10;
rnum += digit;
num /= 10;
rnum *= -1;
}
}
return rnum;
}Comment
-
I fixed it, i changed while(num>0) to while(num) and i took out the rnum*= -1 at then end of the else statement. It seems to be outputting everything ok now.
I do have one more question, im suppost to have a slip of code in where it checks if you are putting in an integer amount. I have added code for this but it doesn't seem to be working properly, I have used this code for trying this:
int _tmain(int argc, _TCHAR* argv[])
{
int num;
cout << "Please enter an integer to be reversed: ";
cin >> num;
cout << endl;
if (num>INT_MAX || num<INT_MIN)
{
cout << "Error: Please use different integer" << endl;
cout << "Please enter an integer to be reversed: ";
cin >> num;
cout << endl;
}
cout << "The reverse order is: " << reverseDigit(nu m) << endl;
system("PAUSE") ;
return 0;
i have used #include <climits> as well. So i know thats not the problem.Comment
Comment