Thread: Swap Function - How to

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    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
    Last edited by Khabz; 03-14-2013 at 10:46 AM.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The swap function is easy:

    Code:
    int arr[9] = { /* Initialize it... */ };
    std::swap(arr[0], arr[8]);
    swap is in <algorithm>.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    That was helpful, tyvm

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    FYI in C++ 11 swap is in <utility>
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    std::swap has always been in utility. Any improvement C++11 added was in r-value references.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    According to swap - C++ Reference, it was in <algorithm> pre-C++11.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I guess my implementation of utility always included algorithm then. That's kind of ........ty.

  8. #8
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Also, don't use void main(). In C++, the main() function must return int.

  9. #9
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    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. #10
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by Khabz View Post
    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. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Khabz View Post
    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. #12
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Okay thanks for the info I will change it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'd like some help with making a swap function.
    By sw881118 in forum C Programming
    Replies: 9
    Last Post: 02-02-2011, 09:00 AM
  2. swap function
    By c_lady in forum C Programming
    Replies: 4
    Last Post: 06-09-2010, 02:27 PM
  3. Good old 'Swap' function
    By starcatcher in forum C Programming
    Replies: 29
    Last Post: 02-03-2008, 10:38 AM
  4. swap function
    By SpEkTrE in forum C Programming
    Replies: 8
    Last Post: 02-18-2005, 01:38 PM
  5. Swap Function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-09-2002, 11:11 AM