Switch 语句 C++ 中的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19273092/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Character in Switch-Statement C++
提问by Jemar Villareal
Please help! I can't produce the output of my program. This is the condition: Construct a program that gives a discount of 100 pesos if the shirt bought is XL and the the price is greater than 500; and a discount of 50 pesos if the shirt bought is L and the price is greater than 600.
请帮忙!我无法生成程序的输出。这是条件:如果购买的衬衫是 XL 并且价格大于 500,则构建一个程序,该程序提供 100 比索的折扣;如果购买的衬衫是L并且价格大于600,则可以享受50比索的折扣。
#include <iostream>
using namespace std;
int main()
{
int p;
int s;
cout << "Input price: ";
cin >> p;
cout << "Input size: ";
cin >> s;
switch (s)
{
case 'XL': case 'xl':
{
if (p>500){
cout << "Total price: " << p-100 << " pesos.";
break;
}
else if ((s=='XL' || s=='xl') && (p<500)){
cout << "Total price: " << p << " pesos.";
break;
}
}
case 'L': case 'l':
{
if (p>600){
cout << "Total price: " << p-50 << " pesos.";
break;
}
else if ((s=='XL' || s=='xl') && (p<600)){
cout << "Total price: " << p << " pesos.";
break;
}
}
case 'M': case 'm':
{
cout << "Total price: " << p << " pesos.";
break;
}
case 'S': case 's':
{
cout << "Total price: " << p << " pesos.";
break;
}
}
return 0;
}
}
The output of the program:
程序的输出:
Input price: 500
Input size: XL
Process returned 0 (0x0) execution time : 5.750 s
Press any key to continue.
P.S. How can I remove the warning (multi-character character constant) in my program? Thanks in advance!
PS 如何删除程序中的警告(多字符字符常量)?提前致谢!
回答by Mike Seymour
If the size can be more than a single character, then you'll need to represent it with a string. You can't switch
on a string, so you'll have to use if..else..else..
to deal with the value:
如果大小可以超过单个字符,那么您需要用字符串表示它。您不能switch
在字符串上,因此您必须使用if..else..else..
来处理该值:
std::string size;
cin >> size;
if (size == "XL") {
// deal with size XL
} else if (size == "L") {
// deal with size L
} // and so on
If it were a single character, then you could use char
(notint
) to represent that:
如果它是单个字符,那么您可以使用char
( notint
) 来表示:
char size;
cin >> size;
switch (size) {
case 'L':
// deal with size L
break;
// and so on
}
but for multiple characters, you'll need a string.
但是对于多个字符,您需要一个字符串。
回答by Tun Zarni Kyaw
switch
statement can handle int
and char
in C++. char
data type can hold only one letter. Thus, if you input only one letter (X
) for XL size will be fine ...
switch
语句可以处理int
和char
在 C++ 中。char
数据类型只能容纳一个字母。因此,如果您只X
为 XL 尺寸输入一个字母 ( ) 就可以了...
cout << "Input size (X/L/M/S): ";
cin >> s;
switch (s){
case 'X': case 'x':
回答by patrickvacek
You've declared s
as an integer but attempt to use it as a character and character array. You should probably declare it is char s;
and then use it consistently as just a single character -- which does mean that you can't check for XL
. You could, however, just check for X
in your switch.
您已声明s
为整数,但尝试将其用作字符和字符数组。您可能应该声明它是char s;
,然后始终将它用作单个字符 - 这确实意味着您无法检查XL
. 但是,您可以只检查X
您的交换机。
If you absolutely must check for XL
, then you'll need to use either a character array or std::string
, although switch statements can only be used with single characters, so you may have to nest your switch to check for multiple characters or just use a series of if (strncmp(...)...)
calls.
如果您绝对必须检查XL
,那么您将需要使用字符数组或std::string
,尽管 switch 语句只能与单个字符一起使用,因此您可能必须嵌套 switch 以检查多个字符或只使用一系列if (strncmp(...)...)
调用。