Card Shuffler

For my programming class we are supposed to make a program, that so far all it has to do is first display an array of cards, and then display the array of cards shuffled, no repeats, and differently shuffled each time. "cards.cpp" to run the file, and then make a class where everything happens in "cards.h". I have tried using srand(), but then I get an error, so I am using rand(). I don't have the exact error at the moment because I am currently using a compiler-less computer. It was something like

"In cards.h, line (whatever the for loop of int shuffle is): void function is not ignored as it ought to be."

If someone can help me that'd be cool. The code for both files is below.
Note: I'm not quite sure if this is correct because I had to rewrite the code just now due to the fact that in FreeBSD (where I originally made it), I did not know how to mount a flash drive (I know I'm dumb haha), but it should work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
//Name: Eric Wells
//Program: cards.cpp
//Purpose: Reference files from cards.h to display an array of
//cards and shuffle them, more additions later
//Errors: <logical> When program is run, cards are first displayed
//in order correctly.  However, when the cards are attempted to be
//displayed the second time as shuffled, some cards are repeated.
//The problem lies in cards.h, read cards.h errors for details.
//Note: Code has been fixed, will update fixed code soon.
*/

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;
#include "cards.h"

int main()
{
  cards stuff;
  
  stuff.deck();
  stuff.shuffle();
  
  return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
//Name: Eric Wells
//Program: cards.cpp
//Purpose: Display an array of cards and shuffle
//them, more additions later (run by cards.cpp)
//Errors: <logical> When cards.cpp is run, cards are first displayed
//in order correctly.  However, when the cards are attempted to be
//displayed the second time as shuffled, some cards are repeated.
//The problem lies in the for loop of "int shuffle()"
//Note: Code has been fixed, will update fixed code soon.
*/

#include <iostream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;

class cards()
{
  public:
  int deck()
  {
    string aDeck[52] = {"Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs", "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds", "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"};
    
    for (int i=0; i<52; i++)
    {
      cout << aDeck[i] << "\n";
    }
    
    cout << "\n\n";
    
    return 0;
  }
  
  int shuffle()
  {
    string aDeck[52] = {"Ace of Spades", "Two of Spades", "Three of Spades", "Four of Spades", "Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades", "Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades", "King of Spades", "Ace of Clubs", "Two of Clubs", "Three of Clubs", "Four of Clubs", "Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs", "Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs", "King of Clubs", "Ace of Diamonds", "Two of Diamonds", "Three of Diamonds", "Four of Diamonds", "Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds", "Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds", "King of Diamonds", "Ace of Hearts", "Two of Hearts", "Three of Hearts", "Four of Hearts", "Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts", "Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts", "King of Hearts"};
    
    for (int i=0; i<52; i++)
    {
      int shuff = 1+rand()%6;
      cout << aDeck[shuff] << "\n";
    }

    cout << "\n\n";

    return 0;
  }
};
Last edited on
Well, I would look at the shuffle(), since that is where they imply the problem is...logically go through it and you should see what's wrong.
srand() is another function as rand(). Look at the reference part of this site to see how it works. You should use it like this: first use srand(time(0)) once in your program, then use rand() when you want to generate a random number.

The above code runs fine. However, you shouldnt use line 12 till 16 in your .h file. I would also consider to declare your cards using two string-arrays, one to hold the color ("spades", "clubs", etc.) and one to hold the numbers ("Ace","Two", etc.), and use two for loops to store the 'sum' of those arrays into a third array.
Oh, and make a membervariable of aDeck[52], to avoid you have to declare it twice.

Hope this helps, if you got any questions, please ask.

Ps. I would consider to download a free compiler as dev-cpp on your home computer

EDIT: firedraco is right, your shuffle function doesnt work as it should. I didnt notice
Last edited on
I agree you should use srand(time(0)) in your code so you get better random numbers.

Yes you display the same cards more then once because you keep choosing random numbers in your array without ever removing the ones your have already used.

Some logic changes to be made to your program:

1)change shuffle to void because it has no real return value.

2)your array should be a private member.

3)In your class cards you should have a constructor that initializes your array
4) I would use vectors instead of arrays for the ease of removing an element but you want arrays. some quick code might be some errors but its basically what needs to be done

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void shuffle()
{
     string array;
     int lastIndex=51;
    string temp;   
    for(int i=0; i<52; i++, lastIndex--)
     {
          randomNumber=rand()%lastIndex;
          array[i]=aDeck[randomNumber];
          temp=aDeck[randomNumber];
          aDeck[randomNumber]=aDeck[lastIndex];
          aDeck[lastIndex]=temp;    
     }

     for(int i=0; i<52; i++)
     {
        aDeck[i]=array[i];
     }
}
Last edited on
Thanks for the help, I got the code working and now I am currently building off of that.

Edit: I will update the code if I have any more problems.
Last edited on
By the way, can anyone tell me how to make a flashdrive show up in FreeBSD? I was trying to copy my code off of there, but when I plug my flashdrive in nothing happens.
Can't you just open the .cpp/.hpp and copy the text through notepad++ or another text editor?
Well I mean we don't have an internet connection on our FreeBSD computers, and I wanted to transfer the data off of my FreeBSD computer, and the only way I can think of doing that is using a flash drive but it won't work and I don't know how to make it work.
ewells
BabyFace!!!
Alright so I made a new function called deal, and I figured out (which was pretty easy) how to deal however number of cards to however number of hands the user wants (shuffled differently every time). But what I am now trying to do is to order the shuffled cards that are dealt to you, like if I got a 3, 7, and a 2, and the other player got a 6, queen, and a jack, I want it to look like:

Hand number 1:
7
3
2

Hand number 2:
12
11
6
But I get:
12
11
7
6
3
2



Problem is, I can get it to organize the cards, but I don't get how to get them in separate hands, without making the first hand have the top three numbers and the second have the lower three. I am not asking for the code, I am just asking for a logical explanation.
_________________________________________________________________________________________

Edit: Nevermind I got it, although I had to edit it so that you can't choose hands, it's preset to two (each has it's own function in the class to make it simpler).
Last edited on
Topic archived. No new replies allowed.