String 如何使用switch case获取字符串参数?
如何将字符串输入作为开关大小写参数?我可以用String 如何使用switch case获取字符串参数?,string,switch-statement,String,Switch Statement,如何将字符串输入作为开关大小写参数?我可以用int但不能用字符串 如果我使用的是int输入,下面的代码就可以工作,但是如果我改为string,它就不工作了 #include <iostream> #include <sstream> #include <string> #include <math.h> class MissionPlan //start of MissionPlan class { public: MissionP
int
但不能用字符串
如果我使用的是int
输入,下面的代码就可以工作,但是如果我改为string,它就不工作了
#include <iostream>
#include <sstream>
#include <string>
#include <math.h>
class MissionPlan //start of MissionPlan class
{
public:
MissionPlan();
float computeCivIndex(string,int,int,float,float);
}; //end of MissionPlan class
LocationData::LocationData()
{
switch(sunType)
{
case "Type A": //compute
break;
case "Type B": //compute
break;
//and many more case..
default: break;
}
}
int main()
{
for(;;)
{
MissionPlan plan;
}
return 0;
}
#包括
#包括
#包括
#包括
类任务计划//开始任务计划类
{
公众:
任务计划();
float computeCivIndex(字符串,int,int,float,float);
}; //任务计划课程结束
LocationData::LocationData()
{
开关(阳光型)
{
案例“类型A”://计算
打破
案例“B型”://计算
打破
//还有更多的情况。。
默认:中断;
}
}
int main()
{
对于(;;)
{
任务计划;
}
返回0;
}
> > > p>不能在C++中使用字符串上的开关语句,对不起。在这里,最好使用枚举。若不想使用枚举,那么你唯一的选择就是做一组检查字符串的相等的ELSE。< /P> < P>你不能在C++中使用一个String语句,对不起。在这里,最好使用枚举。如果您不想使用枚举,那么您唯一的其他选择就是执行一组If-else来检查字符串是否相等。C/C++不支持带有字符串的switch
语句。使用if-else if
:
if (sunType.compare("Type A") == 0) {
//compute
} else if (sunType.compare("Type B") == 0) {
// compute
} else {
// default
}
C/C++不支持带字符串的switch
语句。使用if-else if
:
if (sunType.compare("Type A") == 0) {
//compute
} else if (sunType.compare("Type B") == 0) {
// compute
} else {
// default
}
我已经超过C++ 12年了,但我确信你不能。我确信你不能。但是我不能使用if(unSyt==A类型){{//Metr}}?我在C++中没有开发那么多,但是我没有看到过代码< >代码> STD::String < /Cord> C++类。{//Metr}}?我在C++中不太多,但是我没有看到过< <代码> > = />代码>运算符,用于<代码> STD::String 类。