Hello all,

I am just practicing on class. From what I have learned thus far (from the internet) I made a simple program that resembles picking a deck of cards. But my problem is that although it works. I cannot get it to generate different cards for each object. To understand better here is my code :

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

class typecard {

public:
	int displayCards();
};


//function display which cards was randomly choosen;
int typecard::displayCards()
{
	srand(time(NULL));//random number seeding;
	int card1;
	int ranCards(rand()%13+1);
	int suit(rand()%4+1);

	for(int i=0;i<5;i++)
	{
		suit = rand()%4+1;
		ranCards = rand()%13+1;
	}
		switch(ranCards)
	{	
	
		case 1:	if(suit == 1)//represents hearts.
				{
					cout<<"Ace (of hearts) ";
					card1 = 11;
				}
				if(suit == 2)//represents clubs.
				{
					cout<<"Ace (of clubs) ";
					card1 = 11;
				}
				if(suit == 3)//represents diamonds.
					{
						cout<<"Ace (of diamonds) ";
						card1 = 11;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"Ace (of spades) ";
						card1 = 11;
					}
					break;

		case 2: if(suit == 1)//represents hearts.
					{
						cout<<"2 (of hearts) ";
						card1 = 2;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"2 (of clubs) ";
						card1 = 2;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"2 (of diamonds) ";
						card1 = 2;
					}
				if(suit == 4)//represents spades.
				{
					cout<<"2 (of spades) ";
					card1 = 2;
				}
					break;

		case 3: if(suit == 1)//represents hearts.
					{
						cout<<"3 (of hearts) ";
						card1 = 3;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"3 (of clubs) ";
						card1 = 3;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"3 (of diamonds) ";
						card1 = 3;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"3 (of spades) ";
						card1 = 3;
					}
					break;

		case 4: if(suit == 1)//represents hearts.
					{
						cout<<"4 (of hearts) ";
						card1 = 4;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"4 (of clubs) ";
						card1 = 4;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"4 (of diamonds) ";
						card1 = 4;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"4 (of spades) ";
						card1 = 4;
					}
					break;
		case 5: if(suit == 1)//represents hearts.
					{
						cout<<"5 (of hearts) ";
						card1 = 5;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"5 (of clubs) ";
						card1 = 5;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"5 (of diamonds) ";
						card1 = 5;
					}
				if(suit == 4)//represents spades.
				{
					cout<<"5 (of spades) ";
					card1 = 5;
				}	
					break;

		case 6: if(suit == 1)//represents hearts.
					{
						cout<<"6 (of hearts) ";
						card1 = 6;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"6 (of clubs) ";
						card1 = 6;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"6 (of diamonds) ";
						card1 = 6;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"6 (of spades) ";
						card1 = 6;
					}
					break;

		case 7: if(suit == 1)//represents hearts.
					{
						cout<<"7 (of hearts) ";
						card1 = 7;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"7 (of clubs) ";
						card1 = 7;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"7 (of diamonds) ";
						card1 = 7;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"7 (of spades) ";
						card1 = 7;
					}
					break;

		case 8: if(suit == 1)//represents hearts.
					{
						cout<<"8 (of hearts) ";
						card1 = 8;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"8 (of clubs) ";
						card1 = 8;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"8 (of diamonds) ";
						card1 = 8;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"8 (of spades) ";
						card1 = 8;
					}
					break;

		case 9: if(suit == 1)//represents hearts.
					{
						cout<<"9 (of hearts) ";
						card1 = 9;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"9 (of clubs) ";
						card1 = 9;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"9 (of diamonds) ";
						card1 = 9;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"9 (of spades) ";
						card1 = 9;
					}
					break;

		case 10: if(suit == 1)//represents hearts.
				{
					cout<<"10 (of hearts) ";
					card1 = 10;
				}	
				if(suit == 2)//represents clubs.
					{
						cout<<"10 (of clubs) ";
						card1 = 10;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"10 (of diamonds) ";
						card1 = 10;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"10 (of spades) ";
						card1 = 10;
					}
					break;

		case 11: if(suit == 1)//represents hearts.
					{
						cout<<"J (of hearts) ";
						card1 = 10;
					}
				if(suit == 2)//represents clubs.
				{
					cout<<"J (of clubs) ";
					card1 = 10;
				}	
				if(suit == 3)//represents diamonds.
					{
						cout<<"J (of diamonds) ";
						card1 = 10;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"J (of spades) ";
						card1 = 10;
					}
					break;

		case 12: if(suit == 1)//represents hearts.
					{
						cout<<"Q (of hearts) ";
						card1 = 10;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"Q (of clubs) ";
						card1 = 10;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"Q (of diamonds) ";
						card1 = 10;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"Q (of spades) ";
						card1 = 10;
					}
					break;

		case 13: if(suit == 1)//represents hearts.
					{
						cout<<"K (of hearts) ";
						card1=2;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"K (of clubs) ";
						card1=2;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"k (of diamonds) ";
						card1=2;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"K (of spades) ";
						card1=2;
					}
					break;


	
	
	}//end of switch.

return(card1);

}


int main(){

	typecard p1;
	typecard p2;
	typecard p3;
	typecard p4;

//The problem resides here. How could I get p1-p4 to generate different results.

	p1.displayCards();
	p2.displayCards();
	p3.displayCards();
	p4.displayCards();
	
}
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<string>
using namespace std;

class typecard {

public:
	int displayCards();
};


//function display which cards was randomly choosen;
int typecard::displayCards()
{
	int card1;
	int ranCards(rand()%13+1);
	int suit(rand()%4+1);

	for(int i=0;i<5;i++)
	{
		suit = rand()%4+1;
		ranCards = rand()%13+1;
	}
		switch(ranCards)
	{

		case 1:	if(suit == 1)//represents hearts.
				{
					cout<<"Ace (of hearts) ";
					card1 = 11;
				}
				if(suit == 2)//represents clubs.
				{
					cout<<"Ace (of clubs) ";
					card1 = 11;
				}
				if(suit == 3)//represents diamonds.
					{
						cout<<"Ace (of diamonds) ";
						card1 = 11;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"Ace (of spades) ";
						card1 = 11;
					}
					break;

		case 2: if(suit == 1)//represents hearts.
					{
						cout<<"2 (of hearts) ";
						card1 = 2;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"2 (of clubs) ";
						card1 = 2;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"2 (of diamonds) ";
						card1 = 2;
					}
				if(suit == 4)//represents spades.
				{
					cout<<"2 (of spades) ";
					card1 = 2;
				}
					break;

		case 3: if(suit == 1)//represents hearts.
					{
						cout<<"3 (of hearts) ";
						card1 = 3;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"3 (of clubs) ";
						card1 = 3;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"3 (of diamonds) ";
						card1 = 3;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"3 (of spades) ";
						card1 = 3;
					}
					break;

		case 4: if(suit == 1)//represents hearts.
					{
						cout<<"4 (of hearts) ";
						card1 = 4;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"4 (of clubs) ";
						card1 = 4;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"4 (of diamonds) ";
						card1 = 4;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"4 (of spades) ";
						card1 = 4;
					}
					break;
		case 5: if(suit == 1)//represents hearts.
					{
						cout<<"5 (of hearts) ";
						card1 = 5;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"5 (of clubs) ";
						card1 = 5;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"5 (of diamonds) ";
						card1 = 5;
					}
				if(suit == 4)//represents spades.
				{
					cout<<"5 (of spades) ";
					card1 = 5;
				}
					break;

		case 6: if(suit == 1)//represents hearts.
					{
						cout<<"6 (of hearts) ";
						card1 = 6;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"6 (of clubs) ";
						card1 = 6;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"6 (of diamonds) ";
						card1 = 6;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"6 (of spades) ";
						card1 = 6;
					}
					break;

		case 7: if(suit == 1)//represents hearts.
					{
						cout<<"7 (of hearts) ";
						card1 = 7;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"7 (of clubs) ";
						card1 = 7;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"7 (of diamonds) ";
						card1 = 7;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"7 (of spades) ";
						card1 = 7;
					}
					break;

		case 8: if(suit == 1)//represents hearts.
					{
						cout<<"8 (of hearts) ";
						card1 = 8;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"8 (of clubs) ";
						card1 = 8;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"8 (of diamonds) ";
						card1 = 8;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"8 (of spades) ";
						card1 = 8;
					}
					break;

		case 9: if(suit == 1)//represents hearts.
					{
						cout<<"9 (of hearts) ";
						card1 = 9;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"9 (of clubs) ";
						card1 = 9;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"9 (of diamonds) ";
						card1 = 9;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"9 (of spades) ";
						card1 = 9;
					}
					break;

		case 10: if(suit == 1)//represents hearts.
				{
					cout<<"10 (of hearts) ";
					card1 = 10;
				}
				if(suit == 2)//represents clubs.
					{
						cout<<"10 (of clubs) ";
						card1 = 10;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"10 (of diamonds) ";
						card1 = 10;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"10 (of spades) ";
						card1 = 10;
					}
					break;

		case 11: if(suit == 1)//represents hearts.
					{
						cout<<"J (of hearts) ";
						card1 = 10;
					}
				if(suit == 2)//represents clubs.
				{
					cout<<"J (of clubs) ";
					card1 = 10;
				}
				if(suit == 3)//represents diamonds.
					{
						cout<<"J (of diamonds) ";
						card1 = 10;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"J (of spades) ";
						card1 = 10;
					}
					break;

		case 12: if(suit == 1)//represents hearts.
					{
						cout<<"Q (of hearts) ";
						card1 = 10;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"Q (of clubs) ";
						card1 = 10;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"Q (of diamonds) ";
						card1 = 10;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"Q (of spades) ";
						card1 = 10;
					}
					break;

		case 13: if(suit == 1)//represents hearts.
					{
						cout<<"K (of hearts) ";
						card1=2;
					}
				if(suit == 2)//represents clubs.
					{
						cout<<"K (of clubs) ";
						card1=2;
					}
				if(suit == 3)//represents diamonds.
					{
						cout<<"k (of diamonds) ";
						card1=2;
					}
				if(suit == 4)//represents spades.
					{
						cout<<"K (of spades) ";
						card1=2;
					}
					break;




	}//end of switch.

return(card1);

}


int main(){

    srand(time(NULL)); //random number seeding;
	typecard p1;
	typecard p2;
	typecard p3;
	typecard p4;

//The problem resides here. How could I get p1-p4 to generate different results.

	p1.displayCards();
	p2.displayCards();
	p3.displayCards();
	p4.displayCards();

}

Changed the code, well more like placed 1 line elsewhere... The call to srand, only needs to be done once before it's used, it's abit like an on/off switch & that is why you were getting all the same cards.

So I put it in main before the object creation... Now try compiling that & running it! ;)

SWEET. I guess I just overlooked that. Thanks man :)

If there is anyone viewing this code , i also have one more question. do you think this code is alright. i mean if probably could be done better. what's your constructive critisicm on this?

SWEET. I guess I just overlooked that. Thanks man :)

You're welcome, feel free to set as solved... Also to elaborate cause I feel I didn't really describe the reason why very well before...

What I meant were, if a second call to srand is made with the same seed value as before, it resets the succession so future calls to rand() basically start again hence being done 4 times meant it started at the same value every time... Which is also why the solution is just putting it at the start & not calling it multiple times.

Thanks I under stand.

You're using a switch/case as an array subscript.

Consider something like

char *suits[] = {
  "Hearts", "Clubs", "Diamonds", "Spades"
};
char *ranks[] = {
  "Ace", "Deuce", "Three", // you get the idea
}

suit = rand()%4;
ranCards = rand()%13;
cout << ranks[ranCards] << " (of " << suits[suit] << ") ";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.