Hi again!
It's about function in C++... I'm just not familiar with it!
Can you tell me why my code below doesn't hit my purpose <to swap two integers> :
[cpp]#include <stdio.h>
You should pass parameters by pointer/reference and return nothing (aka void) if you want to have some effect on them. Now it just copies a and b to stack, swaps, and after return from function stack is cleared, so a, b remain the same.
Your return statement is incorrect - you've probably meant returning both changed values, but it is not possible in C or C++ - you can return only one parameter (your statement works as comma operator takes action - but nothing happens)
Correct function should look like this (warning: not tested)
[cpp]
void swap(int& a, int& b)
{
a ^= b ^= a ^= b;
}
void main()
{
int i,j;
i=1; j=2;
swap (i,j);
// now i == 2 && j == 2
}
[/cpp]
Try reading B. Stroustrup's Annotated C++ Ref. Manual, B. Eckel's Thinking in C++, and, of course, Effective and More Effective series by S. Meyers.
consists of 2 inputs: integers in the first line and in the second line.
How can we separate it without taking it as 2 different string inputs <with 'gets'>?