Logical error with exchanging values. How do I fix this?

closed account (Siz8AqkS)
I am trying to exchange values of two variables throughout the code and I can't seem to fix it. Here is a samples code. What am I doing wrong? Thanks a bunch!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <stdlib.h>

using namespace std;

int main ()
{
    int xx, yy, newxx, newyy;
    xx=11;
    yy=22;
    
    xx=newxx;
    yy=newyy;
    xx=newyy;
    yy=newxx;
    
    cout<<xx<<endl;
    cout<<yy<<endl;
    system ("PAUSE");
    return 0;
}


This code outputs to:

2
39
Last edited on
1
2
3
4
5
6
int xx;
int newxx;

xx=11;

xx = newxx;
value of xx is not defined
value of newxx is not defined

xx is now 11

xx is now same as newxx, i.e. not defined



See http://www.cplusplus.com/reference/utility/swap/
Topic archived. No new replies allowed.