【发布时间】:2012-12-09 05:42:08
【问题描述】:
我在 c++ 中遇到了 while 循环的问题。 while 循环总是第一次执行,但是当程序到达 while 循环的 cin 时,while 循环完美地工作,我想知道我做错了什么。提前致谢。如果问题是noobish,我也很抱歉。我还是个初学者。
cout<<"Would you like some ketchup? y/n"<<endl<<endl<<endl; //Ketchup
selection screen
cin>>optketchup;
while (optketchup != "yes" && optketchup != "no" && optketchup != "YES" && optketchup != "Yes" && optketchup != "YEs" && optketchup != "yEs" && optketchup != "YeS"
&& optketchup != "yeS" && optketchup != "YeS" && optketchup != "yES" && optketchup != "y" && optketchup != "Y" && optketchup != "No" && optketchup != "nO"
&& optketchup != "NO" && optketchup != "n" && optketchup != "No");
{
cout<<"You have entered an entered "<<optketchup<<" which is an invalid
option. Please try again."<<endl;
cin>>optketchup;
}
if (optketchup == "yes" || optketchup == "YES" || optketchup == "Yes" || optketchup == "YEs" || optketchup == "yEs" || optketchup == "YeS"
|| optketchup == "yeS" || optketchup == "YeS" || optketchup == "yES" || optketchup == "y" || optketchup == "Y")
{
slcketchup == "with";
}
else
{
slcketchup == "without";
}
cout<<"Your sandwich shall be "<<slcketchup<<" ketchup."<<endl;
system ("pause");
再次提前感谢。
【问题讨论】:
-
optketchup是什么类型?此外,您需要一个等号来分配一个值,并且不推荐使用 homework 标签。 -
简单地将您的字符串转换为小写或大写并仅针对“是”或“是”和“否”或“否”进行测试比测试每个大写/小写字母。
-
不仅仅是他在测试每个大写/小写组合,不是一次,而是两次。经过所有这些测试,他会很累,以至于他需要那个三明治。
-
完整代码不用展示:你展示的代码就够了,独立于其他代码
标签: c++