Compiling problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Trev17
    New Member
    • Feb 2007
    • 22

    Compiling problems

    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.

    Code:
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    int reverseDigit(int rev);
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int num;
    	
    	cout >> "Please enter an integer to be reversed: ";
    	cin << num;
    	cout >> endl;
    
    	cout >> "The reverse order is: " >> reverseDigit(num) >> endl;
    
    	system("PAUSE");
    	return 0;
    }
    
    int reverseDigit(int rev)
    {
        int num = rev, rnum = 0, digit;
    
    	while (num>0)
    	{	
    		rnum *= 10;
    	    digit = num % 10;
    	    rnum += digit;
    	    num /= 10;
    	}
    		 
    	return rnum;
    
    	while (num<0)
    	{
    		rnum *= 10;
    	    digit = num % 10;
    	    rnum += digit;
    	    num /= 10;
    	}
    	rnum *= -1;
    	
    	return rnum;
    }
    Last edited by sicarie; Feb 20 '07, 02:37 AM. Reason: Added code tags.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    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.

    Comment

    • Trev17
      New Member
      • Feb 2007
      • 22

      #3
      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.

      Comment

      • Trev17
        New Member
        • Feb 2007
        • 22

        #4
        Originally posted by Trev17
        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.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Code:
          int reverseDigit(int rev)
          {
             int num = rev, rnum = 0, digit;
             while (num>0)
             {	
                rnum *= 10;
                digit = num % 10;
                rnum += digit;
                num /= 10;
             }
             return rnum;
          
             while (num<0)
             {
                rnum *= 10;
                digit = num % 10;
                rnum += digit;
                num /= 10;
             }
             rnum *= -1;	
             return rnum;
          }
          Looking at this function, can you tell me when the second half will execute?

          Comment

          • Trev17
            New Member
            • Feb 2007
            • 22

            #6
            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.

            Comment

            • sicarie
              Recognized Expert Specialist
              • Nov 2006
              • 4677

              #7
              Originally posted by Trev17
              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).

              Comment

              • estillore
                New Member
                • Jan 2007
                • 17

                #8
                Originally posted by Trev17
                #include <iostream>
                Please try this one: #include <iostream.h>

                Comment

                • sicarie
                  Recognized Expert Specialist
                  • Nov 2006
                  • 4677

                  #9
                  Originally posted by estillore
                  Please try this one: #include <iostream.h>
                  Pretty sure that's not it.

                  Comment

                  • DeMan
                    Top Contributor
                    • Nov 2006
                    • 1799

                    #10
                    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)

                    Comment

                    • sicarie
                      Recognized Expert Specialist
                      • Nov 2006
                      • 4677

                      #11
                      Originally posted by DeMan
                      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).

                      Comment

                      • Trev17
                        New Member
                        • Feb 2007
                        • 22

                        #12
                        thanks for the help guys. Is a while statement what i should be using or should I be using an if statement.

                        Comment

                        • Ganon11
                          Recognized Expert Specialist
                          • Oct 2006
                          • 3651

                          #13
                          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.

                          Comment

                          • Trev17
                            New Member
                            • Feb 2007
                            • 22

                            #14
                            Originally posted by Ganon11
                            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(in t 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;
                            }

                            Comment

                            • DeMan
                              Top Contributor
                              • Nov 2006
                              • 1799

                              #15
                              Is the if(true) just a debug thing.....?

                              Comment

                              Working...