c++ - While 循环总是执行一次,但在第二次时可以正常工作。帮助? [C++]

标签 c++

我在使用 C++ 中的 while 循环时遇到问题。 while 循环总是在第一次执行,但是当程序到达 while 循环的 cin 时,while 循环完美运行,我想知道我做错了什么。提前致谢。如果问题很麻烦,我也很抱歉。我还是个初学者。

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");

再次提前致谢。

最佳答案

您在 while 中有一个分号 (';')。这就是问题所在。

不要写

while(.... lots of conditions ...);
{
    //stuff
}

while(.... lots of conditions ...)
{
    //stuff
}

注意第二个中缺少;

除此之外,如果您必须检查单词 Pneumonoultramicroscopicsilicovolcanoconiosis 怎么办?您最终会检查多少种大小写组合? 相反,将输入转换为大写字母并与大写字母 YESNOPNEUMONOULTRAMICROSCOPICSILICOVOLCANOCONIOSIS 进行比较。

关于c++ - While 循环总是执行一次,但在第二次时可以正常工作。帮助? [C++],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13784995/

相关文章:

c++ - 如何将文件的行读入结构数组

c++ - C++ 新手,想知道 getline() 和 cin 在我的代码中做了什么

c++ - 访问基类的公共(public)成员失败

c++ - Eigen :将 vector 除以标量可以分为两步,但不能一步一步

c++ - 在没有预先初始化的情况下使用 map 订阅运算符的结果是否安全?

c++ - 合并链表时输出错误

C++ 设置结构地址

c++ - 使用带有 std::unique_ptr 的模板时出现奇怪的链接器错误

c++ - 从偏移量读取写入

c++ - 内联 asm 跳转后抛出 C++ 异常