在C+;的开关语句中输入字符时出现用户输入错误+; 我对C++是相当新的,因为我现在正在研究和研究这个语言工具的转换语句。我的代码有问题,因为我缺乏进行相关配置所需的理解,这将有助于解决可能的输出问题。与输入相关输入字符时的理想情况相反,此问题解决了输出默认开关选项的问题: #include <iostream> using namespace std; int main() { char Poke; cout << "Please select your starter Pokemon:" << endl; cin >> Poke; switch (Poke) { case 'Bulbasaur': cout << "You have selected " << Poke << endl; break; case 'Charmander': cout << "You have selected " << Poke << endl; break; case 'Squirtle': cout << "You have selected" << Poke << endl; break; default: cout << "Entry Unknown" << endl; break; } } #包括 使用名称空间std; int main() { 炭戳; 不能戳; 开关(拨动){ “Bulbasaur”案: cout
在C+;的开关语句中输入字符时出现用户输入错误+; 我对C++是相当新的,因为我现在正在研究和研究这个语言工具的转换语句。我的代码有问题,因为我缺乏进行相关配置所需的理解,这将有助于解决可能的输出问题。与输入相关输入字符时的理想情况相反,此问题解决了输出默认开关选项的问题: #include <iostream> using namespace std; int main() { char Poke; cout << "Please select your starter Pokemon:" << endl; cin >> Poke; switch (Poke) { case 'Bulbasaur': cout << "You have selected " << Poke << endl; break; case 'Charmander': cout << "You have selected " << Poke << endl; break; case 'Squirtle': cout << "You have selected" << Poke << endl; break; default: cout << "Entry Unknown" << endl; break; } } #包括 使用名称空间std; int main() { 炭戳; 不能戳; 开关(拨动){ “Bulbasaur”案: cout,c++,C++,'Bulbasaur'是一个多字符文字(请参见语法6)。“Bulbasaur”将是一个字符串(请注意不同的引号字符)。您不能使用字符串或std::string切换。要实现此目的,您需要使用一系列if或使用容器,如std::map或std::unordered\u map P>另外,在 POKE; >代码> char 表示“代码> POKE < /Case>是单个字符。您只能在 POKE < /C>中存储单个字符。使用 STD::String < /Cord>。 < P>您应该看到一些基本的输入
'Bulbasaur'
是一个多字符文字(请参见语法6)。“Bulbasaur”
将是一个字符串(请注意不同的引号字符)。您不能使用字符串或std::string
切换。要实现此目的,您需要使用一系列if
或使用容器,如std::map
或std::unordered\u map
<> P>另外,在 POKE; >代码> char 表示“代码> POKE < /Case>是单个字符。您只能在<代码> POKE < /C>中存储单个字符。使用<代码> STD::String < /Cord>。 < P>您应该看到一些基本的输入/输出C++教程,如:
还要记住,开关
只能用于整数值
我已经做了必要的修改,效果很好:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string Poke;
cout << "Please select your starter Pokemon:" << endl;
getline(cin, Poke);
if (Poke == "Bulbasaur")
cout << "You have selected " << Poke << endl;
else if (Poke == "Charmander")
cout << "You have selected " << Poke << endl;
else if (Poke == "Squirtle")
cout << "You have selected" << Poke << endl;
else
cout << "Entry Unknown" << endl;
}
#包括
#包括
使用名称空间std;
int main()
{
线戳;
库特
我目前正在研究这个语言工具的switch语句
switch语句只能用于整型值,不能用于用户定义类型的值。因此,即使尝试使用C++的std:string类,它也不会起作用。由于您的主要要求是使用开关大小写
,请尝试以下操作:
#include <iostream>
using namespace std ;
int main()
{
char Poke;
cout<<"Please select your starter Pokemon:"<<endl;
cout<<"b for Bulbasaur, c for Charmander, s for Squirtle"<<endl;
poke = getchar(); //Take one char from standard input
switch(Poke){
case 'b' :
cout<<"You have selected "<<Poke<<endl;
break;
case 'c' :
cout<<"You have selected "<<Poke<<endl;
break;
case 's' :
cout<<"You have selected"<<Poke<<endl;
break;
default:
cout<<"Entry Unknown"<<endl;
break;
}
}
#包括
使用名称空间std;
int main()
{
炭戳;
CUT< Calp> Car AR/CODE >表示一个字符。您应该使用字符串。@ M.M OH。什么样的变量类型最好用于用户输入?您应该查阅一些参考资料,如书籍或教程,通过猜测<代码> STD:String >代替“代码> char < /C>”,不可行C++学习。只需用普通类型切换工作即可。s、 无法比较字符串。这里的解决方案是创建适当字符串的std::list并尝试查找输入..如果输入不在列表中。哦,您应该考虑大写,等等。