having trouble with a function

was wondering how do I output the numbers i just swapped in my function to the screen, its only outputting the first number imputed by the user for example if the user enters in 3,4 it outputs 3 for the numbers swapped.

can someone tell me what im doing wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  #include "stdafx.h"
using namespace std;
int one;
int two;
int x;
int swap(int one, int two);


int main()
{
	cout << "Enter two integer values ";
	cin >> one >> two;
	
	cout << "the numbers entered = " << one << two << "/n";

	cout << "the numbers swapped = " << swap(one,two);

	return 0;


}

int swap(int one, int two)
{
	x = two;     // x = 0 one = 2 two = 5,       // x = 5 two = 2 one = 5
	two = one;
	one = x;
	return one,two;
}
For a swap function like that you would have to give the function the ability to edit the original values like this:

1
2
3
4
5
void swap(int& first, int& second){
    int temp = first;
    first = second;
    second = temp;
}


The & gives the function access to the original variables you made- when you don't include them the function gets a copy of the originals.

Edit: grammar
Last edited on
Ah i see, thanks mate
Topic archived. No new replies allowed.