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();
}