2

I'm trying to create variable names using a loop.

Specifically, I am using this structure:

struct card{
    string rank;
    string suit;
};

This is the rest of my code as it stands, and where it says "card+i" is where I need it to say "card1", or "card2", etc.

string aSuit[4] = {" Hearts"," Clubs"," Diamonds"," Spades"};
string aRank[13] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
string aDeck[52];

int main(int argc, char *argv[])
{
    int i = 0;
    for (int s=0; s<4; s++) {
        for (int r=0; r<13; r++) {
            card card+i;
            card+i.rank = aRank[r];
            card+i.suit = aSuit[s];
            cout << card + i.rank << card + i.suit << endl;
            i++;
        }
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}
2
  • 3
    This isn't possible, variable names all disappear at compile time. Maybe use a std::map? (Ok strictly that's not entirely true, what with debug symbols, dynamic linking, etc). Commented Nov 22, 2012 at 21:45
  • 3
    But isn't this exactly why there are arrays? Commented Nov 22, 2012 at 21:45

4 Answers 4

3

Use arrays instead:

card cards[52];

int main(int argc, char *argv[])
{
    int i = 0;
    for (int s = 0; s<4; s++) {
        for (int r = 0; r<13; r++) {
            cards[i].rank = aRank[r];
            cards[i].suit = aSuit[s];
            cout << cards[i].rank << cards[i].suit << endl;
            i++;
        }
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}
Sign up to request clarification or add additional context in comments.

Comments

2

You think this is the best solution, but I can assure you, it is not. Tying your logic to the names of variables is a bad, bad idea, from a logical as well as maintenance standpoibnt. What you really want is a collection which can associate one piece of data (in this case, a string) with another.

Look into a data structure like a map

Comments

0

This isn't possible. When you compile a C++ program, any concept of variable names completely disappears. It can't generate variable names at run time because they just don't exist.

However, there is a way to do this, of course. This is precisely what arrays are for. They give you a whole set of objects that you can index by number (i, in your case). You already have a variable named aDeck but I think you've defined it incorrectly. Did you perhaps want:

card aDeck[52];

Now you'll have a deck of 52 cards. Each card in this deck has suit and rank members. You can access each member of the array with aDeck[i]. So change the inside of your loop to:

aDeck[i].rank = aRank[r];
aDeck[i].suit = aSuit[s];

Comments

0

You need to use arrays for this, they allow a single variable to contain a group of items.

Then you simply use the variable name with an index, such as:

int xyzzy[5];
for (int i = 0; i < 5; i++)
    xyzzy[i] = i * 7;
// gives 0, 7, 14, 21, 28

In fact, your code already has arrays representing the suits and card face values, not really that different from what you need here, so I'm not certain why you didn't make the logic-leap to using them for the cards as well. But that's basically how you'd do it.

3 Comments

The issue I'm having using an array, is to preserve the .suit and .rank of each card in the array. Would I need to create an array[104] {card1.rank, card1.suit, card2.rank, card2.suit, etc...} ?
@user1846123 You've got an array of the wrong things. You don't want an array of string. See my answer.
@user1846123, the answer to that is to define an array of that structure of yours, and use things like card[7].rank.

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.