implementing classes

hey i hav an assignment and i can't get the first part of it to work, we have been given a header file that we can't change

class Card
{
public:
friend class Hand;
void set(int newSuit, int newFace); // set card suit and face
void display(); // display the card value in plain english, see examples for details
private:
int suit; // 0 is Clubs, 1 is Spades, 2 is Hearts, 3 is Diamonds
int face; // 2 is 2, ... 10 is 10, 11 is Jack, 12 is Queen, 13 is King, 14 is Ace
};

class Hand
{
public:
void set(Card newCard1, Card newCard2, Card newCard3, Card newCard4, Card newCard5); // set cards in hand
void displayHand(); // display the cards in the hand, see examples for details
void quickAnalysis(); // analyse the hand and determine if it contains
// a four of a kind, flush, or full house
// update the string member combination accordingly
void displayCombination(); // use member combination to display the name of the combination in case it is
// a four of a kind, flush, or full house
// otherwise output a message that hand is not recognised
// see examples for details
private:
Card card1; // first card in hand
Card card2;
Card card3;
Card card4;
Card card5; // last card in hand
string combination; // hold information regarding the combination of the hand
// the value of this variable is set by quickAnalysis()
};


and when a user inputs 10 integers it should list what they are, but i cant seem to get displayHand() to work


int main()
{
int suit1, suit2, suit3, suit4, suit5;
int face1, face2, face3, face4, face5;

cout << "Enter a hand (10 integers): " << flush;
cin >> suit1 >> face1 >> suit2 >> face2 >> suit3 >> face3 >> suit4 >> face4 >> suit5 >> face5;

Card card1, card2, card3, card4, card5;

card1.set(suit1,face1);
card2.set(suit2,face2);
card3.set(suit3,face3);
card4.set(suit4,face4);
card5.set(suit5,face5);

card1.display();
card2.display();
card3.display();
card4.display();
card5.display();

Hand hand;
hand.set(card1, card2, card3, card4, card5);

hand.displayHand();
system("pause");
return 0;
}


void Card::set(int newSuit, int newFace)
{
suit = newSuit;
face = newFace;
}


void Card::display()
{
string faceStr;
switch (face)
{
case 2: faceStr = "2";
break;
case 3: faceStr = "3";
break;
case 4: faceStr = "4";
break;
case 5: faceStr = "5";
break;
case 6: faceStr = "6";
break;
case 7: faceStr = "7";
break;
case 8: faceStr = "8";
break;
case 9: faceStr = "9";
break;
case 10: faceStr = "10";
break;
case 11: faceStr = "Jack";
break;
case 12: faceStr = "Queen";
break;
case 13: faceStr = "King";
break;
case 14: faceStr = "Ace";
break;
}
string suitStr = "";
switch (suit)
{
case 0: suitStr = "Clubs";
break;
case 1: suitStr = "Spades";
break;
case 2: suitStr = "Hearts";
break;
case 3: suitStr = "Diamonds";
break;
}
cout << faceStr << " of " << suitStr << endl;
}

void Hand::set(Card newCard1, Card newCard2, Card newCard3, Card newCard4, Card newCard5)
{
card1 = newCard1;
card2 = newCard2;
card3 = newCard3;
card4 = newCard4;
card5 = newCard5;
}

void Hand::displayHand()
{
cout << "*********************" << endl;
card1.display();
card2.display();
card3.display();
card4.display();
card5.display();
cout << "*********************" << endl;

}


im not sure about which order and the syntax of everything, any help would be appreciated, i need to get this part done so i can do the main part of the assignment
You need to put the code into code tags so line numbers are shown. (Click on <> icon and paste code).

The input requires the user to put in ten integers. You dont give a range. There are no if statements to catch numbers outside any designated range. Also the switch statements have no defaults put in, so Im guessing but when numbers are entered the result will be ambiguous. ie; suit only needs a range of 4.
Also I would have thought a random allocation of cards would be better. Put srand (time(NULL)); at start of main, and header #include <ctime> or <time.h> whatever works and then you can use
(rand()% enternumber+1); whenever you need a random selection. ie; range of 13 for number of cards in suit = (rand()% 13+1); (the +1 is needed to cater for offset-0).

Also, you appear to be instantiating card objects inside the class Hand and again, I think so anyway, in int main with this line of code; Card card1, card2, card3, card4, card5;. Perhaps delete both these lines and write a global instantiation before int main() and below the class definitions.
Last edited on
okay, this is the header file i've been given that i can't change
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
class Card
{
  public:
    friend class Hand;     
    void set(int newSuit, int newFace); // set card suit and face
    void display();                     // display the card value in plain english, see examples for details
  private:
    int suit; // 0 is Clubs, 1 is Spades, 2 is Hearts, 3 is Diamonds
    int face; // 2 is 2, ... 10 is 10, 11 is Jack, 12 is Queen, 13 is King, 14 is Ace
};

class Hand
{
  public:  
  void set(Card newCard1, Card newCard2, Card newCard3, Card newCard4, Card newCard5); // set cards in hand
  void displayHand();     // display the cards in the hand, see examples for details 
  void quickAnalysis();   // analyse the hand and determine if it contains 
                          // a four of a kind, flush, or full house  
                          // update the string member combination accordingly
  void displayCombination(); // use member combination to display the name of the combination in case it is 
                             // a four of a kind, flush, or full house 
                             // otherwise output a message that hand is not recognised
                             // see examples for details
  private:
    Card card1;  // first card in hand   
    Card card2;   
    Card card3;           
    Card card4;   
    Card card5;  // last card in hand   
    string combination; // hold information regarding the combination of the hand
                        // the value of this variable is set by quickAnalysis() 
};



and this is what i have done, it is going to be run on an automarker so it will only be testing relevant numbers so that isn't the problem

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
int main()
{
  int suit1, suit2, suit3, suit4, suit5;
  int face1, face2, face3, face4, face5;  

  cout << "Enter a hand (10 integers): " << flush;
  cin >> suit1 >> face1 >> suit2 >> face2 >> suit3 >> face3 >> suit4 >> face4 >> suit5 >> face5;
 Card card1, card2, card3, card4, card5;
  
  card1.set(suit1,face1);
  card2.set(suit2,face2);
  card3.set(suit3,face3);
  card4.set(suit4,face4);
  card5.set(suit5,face5);
    
  card1.display();
  card2.display();
  card3.display();
  card4.display();
  card5.display();
  
  Hand hand;
  hand.set(card1, card2, card3, card4, card5);

  hand.displayHand();
  
system("pause");
  return 0;
}

void Card::set(int newSuit, int newFace)
{
  suit = newSuit;
  face = newFace;
}


void Card::display()
{
  string faceStr;
  switch (face)
  {
    case  2: faceStr = "2";
             break;
    case  3: faceStr = "3";
             break;
    case  4: faceStr = "4";
             break;
    case  5: faceStr = "5";
             break;
    case  6: faceStr = "6";
             break;
    case  7: faceStr = "7";
             break;                                                                           
    case  8: faceStr = "8";
             break;
    case  9: faceStr = "9";
             break;
    case 10: faceStr = "10";
             break;
    case 11: faceStr = "Jack";
             break;  
    case 12: faceStr = "Queen";
             break;                
    case 13: faceStr = "King";
             break;  
    case 14: faceStr = "Ace";
             break;                  
  }
  string suitStr = "";
  switch (suit)
  {
    case 0: suitStr = "Clubs";            
            break;
    case 1: suitStr = "Spades";            
            break;
    case 2: suitStr = "Hearts";            
            break;                
    case 3: suitStr = "Diamonds";            
            break;
  }                   
  cout << faceStr << " of " << suitStr << endl;   
}

void Hand::set(Card newCard1, Card newCard2, Card newCard3, Card newCard4, Card newCard5)
{
 card1 = newCard1;
 card2 = newCard2;
 card3 = newCard3;
 card4 = newCard4;
 card5 = newCard5;
}

void Hand::displayHand()     
{
  cout << "*********************" << endl;      
  card1.display();
  card2.display();
  card3.display();
  card4.display();
  card5.display();
  cout << "*********************" << endl;
  
}


i just can't seem to get

cout << "*********************" << endl;
card1.display();
card2.display();
card3.display();
card4.display();
card5.display();
cout << "*********************" << endl;

to come up after the person inputs the numbers, i need to be able to do this then after i think i can do the rest of the assignment, thanks in advance
Can I ask this, regarding the following lines of code

1
2
cout << "Enter a hand (10 integers): " << flush;
  cin >> suit1 >> face1 >> suit2 >> face2 >> suit3 >> face3 >> suit4 >> face4 >> suit5 >> face5;



If I entered '21' as my first integer. Then I entered '2018' as my second, then '1'as my third, '290' as my fourth, '3' as my fifth, '4' as my sixth, 28 as my seventh, 48 as my eighth, 2 as my ninth, 2 as my tenth.
What will happen??
There appears to be no facility in your code to deal with the range of numbers entered by the user. What happens when my input as per above is entered into the program?

For instance the card suit only needs 1-4 numbers, the face require a range of 1-13. As a result I can see an immediate problem when numbers are entered on line 7 of your code.
i understand what you are saying but that part of code was given to us as part of the assignment, the numbers used by the automarker will only be relevant to the number of suits and faces, that is not the challenge of this assignment
You said you cant change the header file.
What can you change?
And how can you fix something without changing code?
i can change everything except the header, i'm just saying that i don't need to do anything about the numbers they input, i just need to have displayHeading() come out after the person inputs 10 valid integers using the suit1 face1 etc so it looks like

*********************
Ace of Spades
10 of Spades
3 of Clubs
King of Spades
5 of Spades
*********************
Topic archived. No new replies allowed.