randomizing card decks

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
srand (time (0));
int counter=1;
string w[4]={"Hearts","Spades","Clubs","Diamonds"};
AAA:
int a=rand()%13+1;
int b=rand()%4;

counter=counter+1;

cout<<a<<" "<<w[b]<<endl;



if (counter<=52)
{

goto AAA;
}
else
{

goto BBB;
}
BBB:
return 0;
}
//the program does all i want but full of mistakes. there should be only 13 spades, 13 diamaonds, 13 clubs and 13 Hearts. because i can not limit the number of cards ( like there should be only 13 hearts && it should not repeat itself. here in output i ve many of the same card.
i m learning and now just began to read about classes. i need some simple help, not using pointers and structures. can it be done?
Last edited on
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
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
   int a, b;
   srand( time( 0 ) );
   string w[4] = { "Hearts", "Spades", "Clubs", "Diamonds" };

   bool taken[13][4];        // keep an array to determine which cards have been picked (initialise as false, i.e. not picked)
   for ( a = 0; a < 13; a++ )
   {
      for ( b = 0; b < 4; b++ ) taken[a][b] = false;
   }

   int numCards;
   cout << "How many cards do you want? ";
   cin >> numCards;
   for ( int n = 1; n <= numCards; n++ )
   {
      bool ok = false;
      while ( !ok )
      { 
         a = rand() % 13;    // correct this below
         b = rand() % 4;
         ok = !taken[a][b];
      }
      taken[a][b] = true;
      a++;                   // correction to start from 1, not 0
      cout << setw( 2 ) << a << " of " << w[b] << endl;
   }

   return 0;
}

A second array simplifies it:
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
#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
srand (time (0));

string w[4]={"Hearts","Spades","Clubs","Diamonds"};
int remaining[]={13,13,13,13};

for(int counter=1;  counter<=52; counter=counter+1)
{
int b;
do
{
  b=rand()%4;
}
while(0 == remaining[b]);

--(remaining[b]);

cout<<(13-remaining[b])<<" "<<w[b]<<endl;
}

return 0;
}
If you do have a full deck and want to pull cards in random order from it, it would be intuitive to shuffle the deck.
http://www.cplusplus.com/reference/algorithm/shuffle/

No pointers, no structures ... but you do use array, so arrays are allowed?
Lets say that numbers 1..13 are hearts, 101..113 are spades, 201..213 are clubs, ...

You have array int deck[52];
It has those 52 unique numbers.
You have shuffled the deck.

1
2
3
int card = deck[i];
int suit = card / 100;
int rank = card % 100;

What can you tell about the suit and rank?


What kind of numbers should the array contain, if we use them like this?
1
2
3
int card = deck[i];
int suit = card / 13;
int rank = card % 13 + 1;

thank you lastchance, coder777 and keskiverto for sharing time for my problem. lastchance really helped. i think coder777's program has a part missing which i cant correct. because it begins to shuffle from 1 then 2 then 3.............. its not really randomizingthe numbers i think. keskiverto ; this a nice viewpoint for mei thanks for the reply.
its not really randomizingthe numbers
Yes, it does not shuffle numbers. Why should it?
hmm ok. ıt is decreasing as (13-reamining[b]. now i got it. another way to do this. ıIt doesnt have to shuffle numbers , you are right. but in real a deck of cards usually do it. anyway thanks for the help, your program gave me some ideas as well. thank you coder777
Topic archived. No new replies allowed.