Problem with Classes and Class calls

Hi! I'm making a program whose purpose is to create an object that represents a single playing card. Then I need to perform several different operations on said card. I've done most of the classes, but I'm having issues with the setcard function. It's supposed to take the given argument and set the playing card to said argument, but it doesn't seem to be working properly. I'd appreciate the help. Thank you!

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  #include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <time.h>

using namespace std;

class cardType
{
public:
	void setCard(int& rank, int& suit);
	//Function to set the card.
	//The time is set according to the parameters.
	//Postcondition: rnk = rank; sut = suit; 
	//               
	//               The function checks whether the 
	//               values of rank and sut.
	//               If a value is invalid, the 
	//               default value 1 is assigned.

	void randCard(int& rank, int& suit);
	//Function to set the rank and suit of the card randomly.

	void cheatCard(int& rank, int& suit);
	//Functoin to set the card to the Ace of Spades.

	void printCard() const;
	//Function to print the card
	//Postcondition: The time is printed in the form
	//"The _____ of ______"


	cardType(int rank, int suit);
	//Constructor with parameters
	//The card is set according to the parameters.
	//Postcondition: rnk = rank; sut = suit; 
	//               The constructor checks whether the 
	//               values of rank and suit 
	//               are valid. If a value is invalid, the 
	//               default value 1 is assigned (The Ace of Clubs).

	cardType();
	//Default constructor
	//The rank is set to -1
	//The suit is set to -1 (aka "no suit")
	//Postcondition: rank = -1 ; suit = -1

private:
	int rank; //variable to store the rank of the card: 1 = Ace, 13 = King
	int suit; //variable to store the suit of the card: 1 = Club, 2 = Diamond, 3 = Heart, 4 = Spade
};



int main()
{
    cardType draw;  

	int rank, suit = 0;
		cout << "enter the rank and suit";
		cin >> rank >> suit;

        //Set the card of draw
    draw.setCard(rank, suit);                         //Line 1

	   //Prints the card
	draw.printCard();
    
	system("pause");
    return 0;
}//end main

void cardType::setCard(int& rnk, int& sut)
{
	if (1 <= rank && rank < 14)
	    rnk = rank;
	else 
	    rnk = 1;

	if (1 <= suit && suit <= 60)
	    sut = suit;
	else 
	    sut = 1;

	
}


void cardType::printCard() const
{
	cout << "The ";
	if (rank == 11)
		cout << " Jack ";
	else if (rank == 12)
		cout << " Queen ";
	else if (rank == 13)
		cout << " King ";
	else
		cout << " " << rank << " ";

	cout << "of ";
	if (rank == 1)
		cout << "Clubs";
	if (rank == 2)
		cout << "Diamonds";
	if (rank == 3)
		cout << "Hearts";
	if (rank == 4)
		cout << "Spades";

	cout << endl;
	return;
}

void cardType::cheatCard(int& rank, int& suit)
{
	rank = 1;
	suit = 4;
}

void cardType::randCard(int& rank, int& suit)
{
	srand(time(0));

	rank = rand() % 12 + 1; //Sets rank between 1 and 13
	suit = rand() % 3 + 1;  //Sets suit between 1 and 4

	return;
}

cardType::cardType(int rnk, int sut)
{
    if (1 <= rank && rank < 13)
        rnk = rank;
    else 
        rnk = 1;

    if (1 <= suit && suit < 4)
        sut = suit;
    else 
        sut = 1;
}

cardType::cardType()  //default constructor
{
    rank = -1;
    suit = -1;
}
Hello!

Unfortunately there is not only your setRank function that has issues, but some of your other functions as well.

Let's start with void cardType::setCard(int& rnk, int& sut):

1
2
3
4
5
6
7
8
9
10
11
12
void cardType::setCard(int& rnk, int& sut)
{
	if (1 <= rank && rank < 14)
	    rnk = rank;
	else 
	    rnk = 1;

	if (1 <= suit && suit <= 60)
	    sut = suit;
	else 
	    sut = 1;
}


Here is what is wrong:
The first part in your if/else statement is in the wrong order.
The second part, even though it looks like as if it were correct, does not do what it should.

Here is why:
You are trying to determine whether rank >= 1 && rank <= 14
This will not work, because there is no value stored in the class's rank and suit member variables to make that determination. In your class constructor you initialized your class member's rank and suit variables to -1.

The second problem in this function is, that you assign the value stored in rank (-1) to rnk.

Here is what you really want to do:

1
2
3
4
5
6
7
8
9
10
11
12
void cardType::setCard(int& rnk, int& sut)
{
	if (rnk >= 1 && rnk <= 14)
		rank = rnk;
	else
		rank = 1;

	if (sut >= 1 && sut <= 60)
		suit = sut;
	else
		suit = 1;
}


First your function has to determine whether the variable's values are within the allowed range. Then, if that is the case, rank is assigned the value of rnk. Likewise, suit is assigned the value of sut if that condition is true. Else, rank and suit are assigned the value 1.

-

On to the card cardType's second constructor.

1
2
3
4
5
6
7
8
9
10
11
12
cardType::cardType(int rnk, int sut)
{
    if (1 <= rank && rank < 13)
        rnk = rank;
    else 
        rnk = 1;

    if (1 <= suit && suit < 4)
        sut = suit;
    else 
        sut = 1;
}


As you can see, it has exactly the same problems, plus the added range, instead of 14 it is 13. For a fix, look at the solution above.

-

void cardType::cheatCard(int& rnk, int& sut)

This function receives two integer values. You never assign them to the rank and suit variables.

1
2
3
4
5
void cardType::cheatCard(int& rnk, int& sut)
{
	rank = 1;
	suit = 4;
}


Here is one possible fix:

1
2
3
4
5
void cardType::cheatCard(int& rnk, int& sut)
{
	rank = rnk;
	suit = sut;
}


This fix assumes that in your main function you ask the user for a rank and suit cheat card, which is then passed to this function. If, however, you wish to assign a cheat card for rank and suit without ever asking, you don't need to pass any integer variables to this function. Make it a void function that accepts no parameters.

1
2
3
4
5
void cardType::cheatCard()
{
	rank = 1;
	suit = 4;
}


And finally the void cardType::printCard() const function

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
void cardType::printCard() const
{
	cout << "The ";
	if (rank == 11)
		cout << " Jack ";
	else if (rank == 12)
		cout << " Queen ";
	else if (rank == 13)
		cout << " King ";
	else
		cout << " " << rank << " ";

	cout << "of ";
	if (rank == 1)
		cout << "Clubs";
	if (rank == 2)
		cout << "Diamonds";
	if (rank == 3)
		cout << "Hearts";
	if (rank == 4)
		cout << "Spades";

	cout << endl;

return;
}


Here you only have ranks, but what with the suits? The fix:

1
2
3
4
	cout << " of ";
	if (suit == 1)
		cout << "Clubs";
....


You will also want to delete the return statement. There is nothing to return from this function, since it is a void function.

-

Now, with this, you might consider some additional changes to your code, once it works.

First of all, move all the functions related to the class above the main function. Second, you have a setCard function, but do all the work in the constructor already. So, once everything works, you could change your constructor to:

1
2
3
4
     cardType(int rnk, int sut)
     {
         setCard(rnk, sut);
     }


Your setCard function will then do all the work.

Edit, you might consider to put all your code inside the class, since there is no seperate header or implementation files involved. If you have your functions and your Constructors in one place in the public section of the class, you can use

cardType()
{
...
}

without the need of using the :: scope resolution operator.
Last edited on
Topic archived. No new replies allowed.