1

I tried to initialize an array of a class then use a loop to change the data members in each of the objects. I'm not sure how to get the values to stick, because after I changed the values, I tried to print a random object out and it's just the default values and not the changed values. Any help would be appreciated, Thanks!

#include <iostream>
#include <string>
#include <time.h>
using namespace std;

class card {
private:
    int rank;
    int suit;
////////////////////////////////////////////////////////////
public:
/////////////////////////////////////////////////////////////
// default constructor with initialization list
    card(int userRank = (2,3,4,5,6) , int userSuit=15)
    :rank(userRank), suit(userSuit){}
///////////////////////////////////////////////////////////
// function to validate user's rank choice.
int cardcheckRank(int pRank){
    while(pRank<2 || pRank>14)
    {
        cout << "Choose a playing card rank between 2-14, where 11=Jack, "
            "12=Queen, 13=King, 14=Ace"<<endl;
        cin >> pRank;
    }
    return pRank;
}

/////////////////////////////////////////////////////////////
// function to validate user's suit choice.
int cardcheckSuit(int pSuit){
while(pSuit<15 || pSuit>18)
{
   cout << "Choose a playing card suit "
    "between 15-18, where 15=Diamond, 16=Club, 17=Heart, 18=Spades.";                                  
cin >> pSuit;
    }
    return pSuit;
}

//////////////////////////////////////////////////////////////////
// functioin to get a card value from user.
void storeCard(int pRank, int pSuit){

    card(cardcheckRank(pRank),cardcheckSuit(pSuit) );
}

/////////////////////////////////////////////////////////////////
// translates 
string faceRank(int translateRank){
switch (translateRank) {
    case 2:
        return "Two";
    case 3:
        return "Three";
    case 4:
        return "Four";
    case 5:
        return "Five";
    case 6:
        return "Six";
    case 7:
        return "Seven";
    case 8:
        return "Eight";
    case 9:
        return "Nine";
    case 10:
        return "Ten";
    case 11:
        return "Jack";
    case 12:
        return "Queen";
    case 13:
        return "King";
    case 14:
        return "Ace";
    default: return "Invalid";
    }
}
///////////////////////////////////////////////////////////
// translate integer suit value into a word.
string faceSuit(int translateSuit){
switch (translateSuit){
    case 15:
        return "Diamonds";
        break;
    case 16:
        return "Clubs";
        break;
    case 17:
        return "Hearts";
        break;
    case 18:
        return "Spades";
        break;
    default: return "Invalid";
    }
}
///////////////////////////////////////////////////////
// Function to print the current card.
void printCard(){

    cout << "The rank of the card is ";
    cout << faceRank(rank);
    cout << " and the suit is " << faceSuit(suit) << "." << endl;
}
};  // End of card class.
//////////////////////////////////////////////////////
// main function.
int main()
{   
srand (time(NULL)); // initialize random seed.
card deck[52];

char choice = 'n';
int h = 0; // card number.

    for(int i = 15; i < 19; i++)
    {
        int y = i;
        for(int j =2; j < 15; j++)
        {
            int z = j;
            (deck [h]).storeCard(z,y);
            cout << "Card rank " << (deck [h]).faceRank(z);
            cout << ", suit " << (deck [h]).faceSuit(y) <<endl;
            ++h;
        }
    }

do{ 
    cout <<"Would user like to play?(y/n)"<<endl;
    cin >> choice;
    int ranNum = (rand()% 51 + 0);
    if(choice == 'y')
    {
        cout << "User: ";
        deck[7].printCard();
    }
}
while(choice == 'y');
return 0;
}
2
  • 1
    Something weird's going on in your constructor. Your default value for userRank is just 6. Later, you create a card object and throw it away. Commented Nov 24, 2013 at 21:06
  • I don't really understand, but I was trying to set a default value to just give the object something but intended to make the whole array a deck of cards, with each card being an object. Commented Nov 24, 2013 at 21:19

1 Answer 1

1

Values are not sticking because your storeCard function doesn't really store the card, it creates a new card object and throws it away. If you really want to express this by invoking the card constructor, then assign the constructed object to the current one:

void storeCard(int pRank, int pSuit){
    *this = card(cardcheckRank(pRank),cardcheckSuit(pSuit) );
}

A more idiomatic approach would be for storeCard to directly modify the object's attributes, much like the constructor does:

void storeCard(int pRank, int pSuit){
  rank = cardcheckRank(pRank);
  suit = cardcheckSuit(pSuit);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I tried your suggestion about using the idiomatic approach and the values are sticking now. Thanks again!
@Davy2468 It's also important to realize why your implementation failed. Although a constructor looks like an ordinary function, you cannot just call it. Calling a constructor is interpreted as constructing a new object - and leaving it hanging in the air. Unless you assign it to something (as I did in the modified storeCard), it will disappear, or even be altogether eliminated by the optimizer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.