Thread: switching value of variable?

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    10

    Angry switching value of variable?

    I'm trying to ru this code however it's not working for me. I need to switch the value of "x" with the value of "y" if (x<y) however it's not working the way I wrote it. Does anyone have any suggestions?

    Code:
    
    { 
    
    
        int x=0;
        int y=0;
        int temp=0;
        int remainder=0;
    
        // read in the two integers
    
        cout << endl ;
         cout << "Enter the first number (integer) : " ; 
        cin >> x ;
         cout << "Enter the second number (integer) : " ; 
        cin >> y ;
     
        //echo inputs
     
        cout << "Input numbers are: " << x << " , " << y << endl;
    
        if ( x < y) 
    
    		
        
           { // exchange values of x and y 
    
    		 temp=x;
    		 x = y;
    		 y==temp;
          
    
     }
    Last edited by genesis531501; 07-25-2011 at 09:01 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So when you compile the code and you get an warning saying something like "line 35: statement has no effect" you should look a little closer at line 35:
    Code:
    y==temp;
    If you're not getting that warning, now is a good time to take a little time-out and read up on how to use your compiler.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    For starters, you have an "equal to" operator instead of an assignment operator in this block.

    Code:
    { // exchange values of x and y 
    
    		 temp=x;
    		 x = y;
    		 y==temp;
    	}

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    When I try to simply put y=temp it doesn't work. No values switch at all. When it tires to compile it states that == has no effect however when it is there temp and x actually change values.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    I am fairly new to C ++ and that is the error I am getting. I just want to know how to fix it.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by genesis531501 View Post
    When I try to simply put y=temp it doesn't work. No values switch at all.
    This statement is incorrect.

    (ETA: Perhaps the confusion comes from the fact that the Euclidean algorithm does not care one whit whether the numbers start out in the right order, so you'll get the same answers as you got before even once you got the swap working.)
    Last edited by tabstop; 07-25-2011 at 08:30 PM.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Have you tried sprinkling some "couts" through your code to see current values and where the program goes wrong? (You could run a debugger, too, but this might be easier to start with.)
    Last edited by Matticus; 07-25-2011 at 08:34 PM. Reason: correct a word

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    maybe a beter question would be if anyone is familair with switching variable values using a temp variable?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Yes, we all are. What you have for swapping variables is correct (once you remove the extra equals sign). You should probably try telling us why you think it doesn't.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    I run the program and I input two numbers. program stops and it says what the input numbers are. It should however give me the GCD for the two numbers. In the compiler it shows no values being swapped. for example when x and y should swap later in the program I have x%y however the compiler shows both of them at zero or intx, int y.

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
        if ( x < y) 
        { // exchange values of x and y 
            temp=x;
            x = y;
            y=temp;
         }
     
        cout << "x = " << x << " , y = " << y << endl;
    Try this and tell us what you get.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Make sure you didn't move the line; the y = temp line needs to be last. Just put another copy of
    Code:
    cout << "Input numbers are: " << x << " , " << y << endl;
    right after the swap and you'll see them change as appropriate.

    Also at the end of your loop, you are supposed to have zero for the remainder; that's the point of the Euclidean algorithm. The GCD should be left in y.

  13. #13
    Registered User
    Join Date
    Jul 2011
    Posts
    10
    I appreciate that they did switch. thank you, The GCD should be left in y however the end of my code say to out put the GCD. no GCD is out putted.

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You edited out the loop bit. I'm guessing you're going one loop too far (i.e. checking the wrong value to be zero), but I don't remember for sure now what you had there.

  15. #15
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Can you share the algorithm again?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switching to RGB from GRB
    By HQSneaker in forum Game Programming
    Replies: 20
    Last Post: 09-28-2004, 07:48 AM
  2. Switching hardrives
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-01-2003, 09:42 PM
  3. About variable type switching..
    By Dragonlord in forum C Programming
    Replies: 6
    Last Post: 08-31-2002, 05:22 PM
  4. Switching arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-18-2002, 02:55 PM
  5. switching
    By iain in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-11-2001, 08:29 AM