# Thread: Swap Function - How to

1. ## 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

2. The swap function is easy:

Code:
```int arr[9] = { /* Initialize it... */ };
std::swap(arr[0], arr[8]);```
swap is in <algorithm>.

4. FYI in C++ 11 swap is in <utility>

5. std::swap has always been in utility. Any improvement C++11 added was in r-value references.

6. According to swap - C++ Reference, it was in <algorithm> pre-C++11.

7. I guess my implementation of utility always included algorithm then. That's kind of ........ty.

8. Also, don't use void main(). In C++, the main() function must return int.

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

10. 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).

11. 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

12. Okay thanks for the info I will change it