Splitting a deck

Pages: 12
Hey there, im trying just for fun to make a small program that is basically a deck of cards split into 4 randomly. 52 cards, no colors, just 4 suits. Im only 2 weeks into coding for C++ so dont be too harsh please, and im only 16. Now back on track, ive set all the strings, added #include <string> and <iostream> in my .h file. So now that i have my 4 strings set with 13 cards each, string set with my 4 types of cards. I dont know how to set it to randomly pick 4 decks with 13 cards each. With all suits included. Im not looking dead on for the answer, im looking for something that'll lead me on track. Im hear to learn, not for give-aways. Any help would be very appreciated. Thanks! :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "main.h"
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;


int main()
{
	string cards [52];
	srand(clock());
	random_shuffle(&cards[0],&cards[51]);
	system("PAUSE");
	return 0;

}
Last edited on
The hearts, diamonds, clubs, and spades arrays do not need to be strings if they are only going to contain numbers.

Also, I don't quite understand what it is you want to do?
im trying just for fun to make a small program that is basically a deck of cards split into 4 randomly.
What do you mean by this? Could you elaborate on this and/or give an example?
Last edited on
I had a conversation with my friend who is in college now for coding/programming. He said when he was in hs his teacher gave him a assignment like this. The requirements were to make a deck of 52 cards, with the normal 4 suits. Then to be able to run a program that would randomly devide the 52 cards into 4 decks of 13. With random numbers and suits in each deck, if that makes since.

And so I dont have to have them? I #defined deck 52. then added int d = deck. gonna try and branch off with that. Should I define each group (spades, hearts, etc.)?

Basically what im trying to do is up my knowledge. :)

I added a function, and added a thing that makes it random.. still trying how to make it work.. Going to define each suit.
Last edited on
i just cant understand how can you use numbers...how will you differentiate it from numbers of otherdeck????????
my advise is just make an array of 52 characters in which first 13 represent some symbol (say 'H' for heart) next 13 represent some other symbol(say 'S' for spade) and so on.....
now shuffle this array using
random_shuffle(&arr[0],&arr[51]);
where arr is the name of the array......

I guess you got my point...
Thats why I marked as solved, because I was like.. Im doing this wrong, let me restart. And started to do what you stated. Then read this, thanks. I almost gave up.. I was like, I did this wrong.. Haha, Highly appreciated. :)
Don't forget to #include <ctime> and then seed the random function to the time:
srand(time(NULL));
Last edited on
already did, one thing. All my numbers in the new array which is.

string cards [52] = {h1, h2, h3, h4, etc...}

Say they need to be defined.. o.O
Last edited on
You can use a for loop to go from 0 to 51 in the array and define the numbers. If you know how to use stringstreams it will be even easier for you.
Last edited on
Looking up how to use it, since itd be easier.
@LB
hey how will seeding the random data to time help....mean what is this for?i am new to this concept and haven't learnt it yet.....
Well, if you don't seed the random function to the time or some other value that changes with each run of your program, then every time you run your program the random function will return the same values in the same order. By seeding it to the time, you change its position in this sequence and then you get different results every time as long as your system time is changing.
random_shuffle is unidentified.. whats the #include for it? And this is what I have..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "main.h"
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
	string cards;
	srand(clock());
	random_shuffle(&cards[0],&cards[51]);
	system("PAUSE");
	return 0;

}
@L B
thanx.....
random_shuffle() is defined in the algorithm header:
http://www.cplusplus.com/reference/algorithm/
Run thiss, theres a major error. Why?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "main.h"
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	string cards;
	srand(clock());
	random_shuffle(&cards[0],&cards[51]);
	system("PAUSE");
	return 0;

}
Because the string cards has a size of 0? You are trying to access stuff that doesn't exist, basically.
I fixed, that its constantly popping with errors nomatter what number I put though.
By the way, clock() is a bad way to initialize the PRNG, as it will be 0 most of the time.
Use time(0) instead: http://www.cplusplus.com/reference/clibrary/ctime/time/
How did you 'fix' that? The string has to contain 52 characters for your code to work, and currently it just contains nothing.
nvm. I feel dumb, anyways. i gave it a value of 52. Now when I run it it just says. "Press any key to continue"
Pages: 12