gpt4 book ai didi

C++ 带字符串参数的 Switch 大小写

转载 作者:行者123 更新时间:2023-11-28 06:53:28 28 4
gpt4 key购买 nike

<分区>

大家好

所以我正在尝试对字符串使用 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 运算符,所以我很好奇我是否可以使用字符串。

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com