Thread: Sorting hand of cards in poker

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Sorting hand of cards in poker

    I am working on an assignment that involves creating a simplified poker game. I am new to C++, but this is for class, so I am here to learn. Anyways...

    After I deal each players hand vector, how would I sort the vector before it prints? They are char vectors called hand1 for player 1, and hand2 for player 2. The deck vector is a char as well, but the rank array is a string (so that the number 10 prints), and the suit array is a char (so that the actual symbols print). Instead of printing: 8<heart>, 3<club>, A<spade>, 4<spade>, 9 <diamond>, 8<club>, 2<club>, it would print: 2<club>, 3<club>, 4<spade>, 8<heart>, 8<club>, 9 <diamond>, A<spade>.

    I hope this was enough information. I can send my code if you'd like to look at it. No pointers, no classes. I'm not at that level yet (unless there is no other way to do what I am asking). I can improve upon this project in my next class.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sort it before you print?
    std::sort would work, but that would require iterators or pointers.
    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
    Oct 2011
    Posts
    2
    Quote Originally Posted by Elysia View Post
    Sort it before you print?
    std::sort would work, but that would require iterators or pointers.
    How would I use sort with a vector? I have tried something similar with lists (which I have not ever used before this), but it definitely did not sort in any way.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    If you have a vector v, then

    std::sort(v.begin(), v.end());
    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.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    How exactly do you want the output to look like?

    I simply replaced your loop with

    std::sort(p1Hand.begin(), p1Hand.end());
    std::sort(p2Hand.begin(), p2Hand.end());

    to sort. But I don't know if the output is what you expect.
    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.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well over here you complain about how long you've been working on it, and how nice it would be to have as many people as possible to look at it.

    But if you're just going to delete the code as soon as only ONE person has read and replied to it, well -

    On the one hand, I normally revive posts deleted because "you got an answer".
    But somehow, I think it's more fitting that you should continue to suffer in silence knowing that no-one else can now contribute to your question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Undelete the code!
    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.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No - the OP can post it again themselves, if they're sincere about actually wanting some help.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I wasn't thinking about the OP, but rather other people who would see this thread.
    That is why editing out or removing the code is a bad idea.
    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.

  10. #10
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This sounds like an interview question. I will add to it with how would you sort the deck making sure you touched every card and that the deck was fully shuffled in one pass?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. manually sorting cards
    By cyberfish in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 05-16-2008, 09:09 AM
  2. need a hand
    By eurikau in forum C Programming
    Replies: 0
    Last Post: 07-17-2007, 08:50 AM
  3. what are some other 'life skills' that can go hand in hand with programming
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-17-2003, 02:34 PM
  4. lend me a hand please...
    By khaisunizam in forum C Programming
    Replies: 5
    Last Post: 09-05-2002, 07:24 PM
  5. Hand Up
    By Rolf in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2001, 08:44 AM