使用C ++中的循环创建顺序变量名 [英] Create sequential variable names using a loop in C++
本文介绍了使用C ++中的循环创建顺序变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用循环创建变量名.
I'm trying to create variable names using a loop.
具体来说,我正在使用以下结构:
Specifically, I am using this structure:
struct card{
string rank;
string suit;
};
这是我剩下的代码,上面写着"card + i"的地方就是我要说"card1"或"card2"等的地方.
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;
}
推荐答案
使用数组代替:
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;
}
这篇关于使用C ++中的循环创建顺序变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文