我要在主要功能之外设置 Enumeration 并使用 void PlayingCard() 来提取枚举等级和花色以向用户显示等级和花色。但是我很难编写正确的代码来让它按照我想要的方式工作。我是 C++ 的新手。任何帮助将不胜感激。
谢谢!
我已经用它的卡号和套装设置了 enum rank() 和 enum suit()。 然后我按照教授的指示在 void PrintCard() 函数中设置了一个 switch-case 语句。我试图将它拉入 main() 函数,但它什么也做不了。
#include <iostream>
#include <conio.h>
using namespace std;
enum Rank // ace high
{
TWO = 2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE
};
enum Suit
{
SPADE, DIAMOND, CLUB, HEART
};
struct Card
{
Rank rank;
Suit suit;
};
void PrintCard(Card card);
int main()
{
int num1 = 2;
cin >> num1;
PrintCard;
_getch();
return 0;
}
void PrintCard(Card card)
{
switch (card.rank)
{
case 14: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 13: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 12: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 11: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 10: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 9: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 8: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 7: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 6: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 5: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 4: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 3: cout << "The" << card.rank << "of" << card.suit << endl;
break;
case 2: cout << "The" << card.rank << "of" << card.suit << endl;
break;
default:
cout << "Wrong Input" << endl;
}
}
我希望它在我输入数字时向用户显示西装和等级,但输入只是空白,我无法从 PrintCard 函数中提取任何内容以供使用。
最佳答案
您没有正确调用您的 PrintCard
函数:
int main()
{
int num1 = 2;
cin >> num1;
Card card;
card.rank = Rank::TWO;
card.suit = Suit::SPADE;
PrintCard(card);
_getch();
return 0;
}
创建一个卡片对象,为其分配等级和花色,然后使用该对象调用您的 PrintCard
函数。
在你的 cout 中额外添加空格:
cout << "The " << card.rank << " of " << card.suit << endl;
你不需要你的 switch.case 因为每个 case 都是一样的:
void PrintCard(Card card)
{
cout << "The " << card.rank << " of " << card.suit << endl;
}
如果您想打印枚举名称,则必须使用 switch-case。 示例:
std::string str;
switch (card.rank):
case Rank::TWO:
str = "two";
break;
...
cout << str;
关于c++ - 使用 Switch-case 语句拉取随机纸牌等级和花色以显示给用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57921142/