# Swap Function - How to

• 03-14-2013
Khabz
Swap Function - How to
Hello everyone, I'm trying to make a simple C++ game but I came across with some problems which are giving me a hard time.

I'll post a portion of code so you can understand my 'problem' better.

Code:

```void main() {     char cord1('1');     char cord2('2');     char cord3('3');     char cord4('4');     char cord5('5');     char cord6('6');     char cord7('7');     char cord8('8');     char cord9(' ');     bool WinGame = true;                 cout<<"  " <<cord1 <<"  |  " <<cord2 <<"  |  " <<cord3 <<endl;         cout<<"-----+-----+-----" <<endl;         cout<<"  " <<cord4 <<"  |  " <<cord5 <<"  |  " <<cord6 <<endl;         cout<<"-----+-----+-----" <<endl;         cout<<"  " <<cord7 <<"  |  " <<cord8 <<"  |  " <<cord9 <<endl <<endl;         int Play;         cout<<"Wich piece do you wish to move?: ";         cin>>Play;         cin.ignore();```
When the user inserts an integer from 1 to 8 (which are part of the game board), THAT integer is supposed to swap with the empty square.
Example:

1 | 2 | 3
4 | 5 | 6
7 | 8 |

Wich piece do you wish to move?: 1

| 2 | 3
4 | 5 | 6
7 | 8 | 1

I made some research and I know there is a "swap" function somewhat, I just don't know how to implement it (yet).

Thanks in advance, hope you can give me a hand.

PS. The code is NOT complete. It is just an example as to how I defined the slots of the game board
• 03-14-2013
Elysia
The swap function is easy:

Code:

```int arr[9] = { /* Initialize it... */ }; std::swap(arr[0], arr[8]);```
swap is in <algorithm>.
• 03-14-2013
Khabz
That was helpful, tyvm
• 03-14-2013
rogster001
FYI in C++ 11 swap is in <utility>
• 03-14-2013
whiteflags
std::swap has always been in utility. Any improvement C++11 added was in r-value references.
• 03-15-2013
Elysia
According to swap - C++ Reference, it was in <algorithm> pre-C++11.
• 03-15-2013
whiteflags
I guess my implementation of utility always included algorithm then. That's kind of ........ty.
• 03-15-2013
antred
Also, don't use void main(). In C++, the main() function must return int.
• 03-15-2013
Khabz
I've used void main() for a couple of games I made before and it worked just fine just like it is working now.
• 03-15-2013
antred
Quote:

Originally Posted by Khabz
I've used void main() for a couple of games I made before and it worked just fine just like it is working now.

Well yes, your computer isn't going to explode because of it, but per the C++ standard, void main() is not valid C++. It's not a biggie, but then again it's not that much extra work to write int main instead of void main, especially considering that the effect is that your program would then be standard-compliant (at least in that particular respect).
• 03-15-2013
Matticus
Quote:

Originally Posted by Khabz
I've used void main() for a couple of games I made before and it worked just fine just like it is working now.

void main(void) - the Wrong Thing
• 03-15-2013
Khabz
Okay thanks for the info :) I will change it