简体   繁体   English

C ++类型为char的输入,类型为int的输出

[英]C++ input of type char, output of type int

I am taking my very first C++ class and I am stuck. 我正在上我的第一个C ++类,但遇到了麻烦。 I would really appreciate some help from you experienced programmers. 我非常感谢您有经验的程序员的帮助。

The assignment is creating a blackjack-scoring program. 作业正在创建二十一点计分程序。 Not a very realistic one, but hey. 不是很现实,但是嘿。 The user inputs how many cards he wants and then the values of each of those cards. 用户输入他想要多少张卡,然后输入每张卡的值。 The assignment specifies that the inputs should be in type char . 分配指定输入应为char类型。 So if the user has a 2 card they enter 2, but that 2 is actually char and must be converted to int . 因此,如果用户拥有2卡,则输入2,但该2实际上是char ,必须将其转换为int Or they would enter "Q" if they have a queen and my program is supposed to convert that Q to ten points for scoring. 否则,如果他们有一个女王/王后,他们将输入“ Q”,而我的程序应该将该Q转换为10分才能得分。 I cannot figure out what is the right way to do this. 我不知道什么是正确的方法。 The assignment suggests I will use either a switch statement or nested if-else statement, but I am afraid I don't understand switch very well from the book examples. 作业表明我将使用switch语句或嵌套的if-else语句,但是恐怕我从本书示例中不太了解switch

So here's a tiny bit of my attempts at switch. 因此,这只是我尝试切换的一小部分。 *points_for_card* is of type char and *number_value* is int. * points_for_card *的类型为char ,* number_value *的类型为int。

switch (points_for_card)
{    
case '2':
   number_value = 2  ;
   break;
case '3':
   number_value = 3  ;
   break;

// ETC
}

So what I am going for here is: if the user enters '3' as a char, it becomes int 3. But maybe this is not how switch works at all. 因此,我要讲的是:如果用户将3作为字符输入,它将变成int3。但这也许根本不是switch起作用的方式。

The thing is, my program compiles and works, but returns weird crazy huge numbers. 问题是,我的程序可以编译并运行,但是返回的是怪异的疯狂巨大数字。 If I move points_for_card to int instead of char , then the arithmetic works perfectly for whatever numbers I enter, because at that point it's just adding them together. 如果我将points_for_card移到int而不是char ,那么该算术对于输入的任何数字都非常有效,因为在那一点上,它们只是将它们加在一起。

I hope I explained this ok, will clarify as much as possible if necessary. 我希望我可以对此进行解释,如有必要,将尽可能地澄清。

it can be something like this code: 可能是这样的代码:

if (points_for_card >= '1' && points_for_card <= '9'){
    number_value = points_for_card - '0'; // convert to number
}else if (points_for_card == 'Q'){
    ...
}

A map comes to mind. 我想到了一张地图 You can store the scores directly, or you can make one map to look up the card type and other maps to associate other information (like score) to each card. 您可以直接存储分数,也可以制作一张地图以查找卡类型,而制作其他地图以将其他信息(例如分数)与每张卡相关联。 Here's the baby example: 这是婴儿的例子:

std::map<char, int> scores;

scores['Q'] = 10; scores['A'] = 13; scores['2'] = 2; // etc.

char c;

std::cout << "Please enter a card: ";
std::cin >> c;

std::cout << "Your card has score " << scores[c] << std::endl;

Oftentimes when your heart says "switch", your brain should say "map" :-) 通常,当您的心脏说“切换”时,您的大脑应该说“地图” :-)

Personnally I'd define an enum ECardType { Card_2, ..., Card_10, Card_Jack, ... }; 我个人定义一个enum ECardType { Card_2, ..., Card_10, Card_Jack, ... }; and have one map be std::map<char, ECardType> , and then other maps from card type to secondary information like scores. 并将一张地图设为std::map<char, ECardType> ,然后将另一张地图从卡类型映射到诸如得分之类的次要信息。

How are you taking inputs into points_for_card ? 您如何将输入输入points_for_card

Your input should be cin >> points_for_card ; 您的输入应为cin >> points_for_card ;

Instead of comparing a character to a character, you can also compare it to the ASCII value of a character. 除了将字符与字符进行比较之外,还可以将其与字符的ASCII值进行比较。 For example, 例如,

char letter = 'A'
if(letter == 65){
cout << "Match";
}

The above code will output "Match!". 上面的代码将输出“ Match!”。

Also, your switch statements are perfectly worded. 另外,您的switch语句用词很完美。 The problem lies elsewhere in your program, so please provide the relevant source. 问题出在程序的其他地方,因此请提供相关来源。 Another point related to your program but not your problem: How are you dealing with Aces ? 另一点与您的程序有关,但与您的问题无关:您如何处理Aces? You know that they can be counted as either 1 or 11, depending on the player's hand value, right ? 您知道他们可以算为1或11,具体取决于玩家的手牌值,对吧?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM