By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 817 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Compiling problems

P: 22
Hello, i am getting numerous error messages when trying to compile my program. The program is to create a function to reverse digits. I have a feeling i am close but i can't figure out the problem. I'm getting a lot of errors regarding istreams and ostreams. I have been trying to figure this out for hours.

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int reverseDigit(int rev);
  6.  
  7. int _tmain(int argc, _TCHAR* argv[])
  8. {
  9.     int num;
  10.  
  11.     cout >> "Please enter an integer to be reversed: ";
  12.     cin << num;
  13.     cout >> endl;
  14.  
  15.     cout >> "The reverse order is: " >> reverseDigit(num) >> endl;
  16.  
  17.     system("PAUSE");
  18.     return 0;
  19. }
  20.  
  21. int reverseDigit(int rev)
  22. {
  23.     int num = rev, rnum = 0, digit;
  24.  
  25.     while (num>0)
  26.     {    
  27.         rnum *= 10;
  28.         digit = num % 10;
  29.         rnum += digit;
  30.         num /= 10;
  31.     }
  32.  
  33.     return rnum;
  34.  
  35.     while (num<0)
  36.     {
  37.         rnum *= 10;
  38.         digit = num % 10;
  39.         rnum += digit;
  40.         num /= 10;
  41.     }
  42.     rnum *= -1;
  43.  
  44.     return rnum;
  45. }
Feb 20 '07 #1
Share this Question
Share on Google+
24 Replies


Ganon11
Expert 2.5K+
P: 3,652
Big reason #1 is that your <<s and >>s are coupled with the wrong statements. You use cout >> when you should use cout <<, and you use cin << when you should use cin >>. Switching these should remove a lot of the errors.
Feb 20 '07 #2

P: 22
Wow, i feel like a complete idiot now haha. The good news is it did compile but the bad news is that every time i try to reverse a negative number i get 0.
Feb 20 '07 #3

P: 22
Wow, i feel like a complete idiot now haha. The good news is it did compile but the bad news is that every time i try to reverse a negative number i get 0.
I know that in order to get the output to be negative and reverse i would do the same coding only multiply rnum and the end by -1 to make the reversed integer negative but i cant figure out how to do that.
Feb 20 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. int reverseDigit(int rev)
  2. {
  3.    int num = rev, rnum = 0, digit;
  4.    while (num>0)
  5.    {    
  6.       rnum *= 10;
  7.       digit = num % 10;
  8.       rnum += digit;
  9.       num /= 10;
  10.    }
  11.    return rnum;
  12.  
  13.    while (num<0)
  14.    {
  15.       rnum *= 10;
  16.       digit = num % 10;
  17.       rnum += digit;
  18.       num /= 10;
  19.    }
  20.    rnum *= -1;    
  21.    return rnum;
  22. }
Looking at this function, can you tell me when the second half will execute?
Feb 20 '07 #5

P: 22
Looking at this function, can you tell me when the second half will execute?[/quote]

Doesn't the second half execute only if the number is negative? If it doesn't, i don't understand why.
Feb 21 '07 #6

sicarie
Expert Mod 2.5K+
P: 4,677
Looking at this function, can you tell me when the second half will execute?
Doesn't the second half execute only if the number is negative? If it doesn't, i don't understand why.[/quote]

Do a line by line test.

Pick an arbitrary value, and go line-by-line through your peogram with it (make sure you hit every line, and you will see what Ganon was talking about).
Feb 21 '07 #7

P: 17
#include <iostream>
Please try this one: #include <iostream.h>
Feb 21 '07 #8

sicarie
Expert Mod 2.5K+
P: 4,677
Please try this one: #include <iostream.h>
Pretty sure that's not it.
Feb 21 '07 #9

DeMan
100+
P: 1,806
I think what Ganon11 is suggesting is that return is terminal!! that is you execute a "while" then "return" then expect more processing......How does the program flow beyond the return, irrespective of what you pass in ??

(and since rnum is set to 0, that will always be your return)
Feb 21 '07 #10

sicarie
Expert Mod 2.5K+
P: 4,677
I think what Ganon11 is suggesting is that return is terminal!! that is you execute a "while" then "return" then expect more processing......How does the program flow beyond the return, irrespective of what you pass in ??

(and since rnum is set to 0, that will always be your return)
That was my thought as well (though I was hoping the OP would catch it themself).
Feb 21 '07 #11

P: 22
thanks for the help guys. Is a while statement what i should be using or should I be using an if statement.
Feb 24 '07 #12

Ganon11
Expert 2.5K+
P: 3,652
You should be using both an if statement and a while statement. I can see what you were trying to do by creating two loops, but the return statement after the first loop executes no matter what. You should use an if...else statement to separate the function into two possibilities - a positive (or zero) number, or a negative number. From there, you can use your loops and they will work as planned.
Feb 24 '07 #13

P: 22
You should be using both an if statement and a while statement. I can see what you were trying to do by creating two loops, but the return statement after the first loop executes no matter what. You should use an if...else statement to separate the function into two possibilities - a positive (or zero) number, or a negative number. From there, you can use your loops and they will work as planned.
I've done this but i am having a little problem somewhere in the code. It compiles, it just doesn't output anything after i put in the integer to be reversed.

int reverseDigit(int rev)
{
int num = rev, rnum = 0, digit;


while (num>=0)
{
if (true)
{
rnum *= 10;
digit = num % 10;
rnum += digit;
num /= 10;
}
else
{
rnum *= 10;
digit = num % 10;
rnum += digit;
num /= 10;
rnum *= -1;
}
}
return rnum;
}
Feb 24 '07 #14

DeMan
100+
P: 1,806
Is the if(true) just a debug thing.....?
Feb 24 '07 #15

DeMan
100+
P: 1,806
When you say doesn't output anything, do you mean it just stops doing anything, or the program stops without printing your last line?
Feb 24 '07 #16

P: 22
When you say doesn't output anything, do you mean it just stops doing anything, or the program stops without printing your last line?
The program itself doesn't stop, but it just goes to the next line without printing anything
Feb 24 '07 #17

Expert 100+
P: 1,510
for a start try changing
Expand|Select|Wrap|Line Numbers
  1. while (num>=0)
  2.  
which causes your program to loop forever when num = 0
to
Expand|Select|Wrap|Line Numbers
  1. while (num>0)
  2.  
also
Expand|Select|Wrap|Line Numbers
  1. if (true)
  2. {
  3.                       will always execute this code 
  4. }
  5.  
Feb 24 '07 #18

P: 22
Expand|Select|Wrap|Line Numbers
  1. if (true)
  2. {
  3.                       will always execute this code 
  4. }
  5.  
what if the int typed in is negative? wont it execute the else?
Feb 25 '07 #19

Ganon11
Expert 2.5K+
P: 3,652
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:

Expand|Select|Wrap|Line Numbers
  1. if (num is positive) {
  2.    // Reverse using positive method
  3. } else { // num isn't positive, so it must be negative
  4.    // Reverse using negative method
  5. }
with your loops being the Reverse portion.
Feb 25 '07 #20

P: 22
ahhh, 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
Feb 25 '07 #21

Ganon11
Expert 2.5K+
P: 3,652
ahhh, 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
Correct. However, your test is redundant. Let's break it down:

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.
Feb 25 '07 #22

P: 22
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(int 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;
}
Feb 25 '07 #23

P: 22
i might be wrong on this but is it because there is no way for the else to ever work because its in the while which is greater than 0 to begin with.

If so, is it possible to just get rid of the while statement and just use the if statement?
Feb 25 '07 #24

P: 22
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(num) << endl;

system("PAUSE");
return 0;

i have used #include <climits> as well. So i know thats not the problem.
Feb 25 '07 #25

Post your reply

Sign in to post your reply or Sign up for a free account.