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.