【问题标题】:C++ Switch case with string argument [duplicate]带有字符串参数的C ++ Switch案例[重复]
【发布时间】:2014-05-04 23:31:39
【问题描述】:

大家好,

所以我正在尝试对字符串使用 Case 操作,Case 运算符是否仅适用于整数? 因为编译错误:

switch quantity not an integer

我以这段代码为例:

#include <iostream>
using namespace std;

 int main(){
string type;
cout<<"Please enter your computer type (PC ,Laptop, Smartphone) : "<<endl;
getline(cin, type);

 switch(type){

case "PC":
 cout<<"Ok so you'vr got a "<<type;
 break;

case "Laptop":
 cout<<"Aha ,u got a "<<type;
 break;

case "Smartphone":
 cout<<"That s not bad ,u ve got :"<<type;
 break;
}


return 0;
}

我知道我可以使用其他逻辑运算符,例如 IF ;但我正在研究 Case 运算符,所以我很好奇我是否可以使用字符串。

【问题讨论】:

标签: c++ string case


【解决方案1】:

c/c++ switch 语句只对数字整数原语进行操作。

您不能将它与char* 指针(文字)一起使用。

attr(可选) switch (condition) 语句

条件 - 整数或枚举类型的任何表达式,或具有到整数或枚举类型的转换函数的类类型,或使用大括号或等号初始化器声明此类类型的单个非数组变量。

【讨论】:

  • 谢谢!对不起我的菜鸟问题
  • @user3602556 天哪!欢迎来到 SO。 THX 接受。在提出问题之前,在未来的研究中更彻底。你会发现大部分问题在这里已经有了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-22
相关资源
最近更新 更多