在 while(true) 循环中切换大小写
Switch case inside a while(true) loop
int main() {
int choice;
while (true){
cout << "Enter choice: \n";
cin.clear();
cin >> choice;
switch(choice){
case 1:
cout << "you picked 1\n";
break;
case 2:
cout << "you picked 2\n";
break;
default:
cout << "invalid choice\n";
break;
}
}
}
大家好!你能帮我么?我制作了一个有多个选项可供选择的程序。问题是当我输入整数以外的东西时,它给了我一个无限循环。如何向输入抛出错误和 return?
已经有很多人对你的问题发表了很好的评论,我只是想提供一些提示,并为你指出一些链接以从中学习一些东西。
首先,不要忘记始终使示例代码完全运行并准备好编译:
- 你忘了包括
<iostream>
- 您使用的
cin
/cout
没有各自的命名空间 (std::
)
- 也许你应该多构建一些代码,使用一个函数来指出你在做什么——比如:
getChoiceFrom(std::istream& stream, int& choice)
和 printChoices(int choiceIGot)
——是的,有更好的选择,但你知道通过这些学习你的方式
What's the difference between passing by reference vs. passing by value?
对于第一部分,您可能已经完成了这些,但我建议您查看:Why is "using namespace std;" considered bad practice? - 也许您已经知道这一点 - 但我们无法从您的样本中判断出来。
让我们重写一些代码(我会故意尽可能地接近原始代码):
#include <iostream>
#include <limits>
int main() {
// I'd advise against it, but you could at least do:
using std::cout;
using std::cin;
// instead of using namespace std, which I assume you did
// OR, again, not recommended but at least limit the scope of
// using namespace std;
int choice;
while (true){
cout << "Enter choice: \n";
// `std::cin` (converts to `bool` indicating errors or lack thereof) - https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
// this is the concise version but you can also split it
// it works because cin returns a reference to self when doing operator >> - also called insertion or put - https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2
if(!(cin >> choice)) {
cout << "invalid input! please enter a number\n";
// clear the error state of this input stream
// docs: https://en.cppreference.com/w/cpp/io/basic_ios/clear
cin.clear();
// ignore all the input that was passed by the user until a certain max limit
// https://en.cppreference.com/w/cpp/io/basic_istream/ignore
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// here you can:
// 1) continue the loop and re-ask for input
// continue;
// 2) get out of the loop and end the program (seems that that's what you want to do
break;
}
switch(choice){
case 1:
cout << "you picked 1\n";
break;
case 2:
cout << "you picked 2\n";
break;
default:
cout << "invalid choice\n";
break;
}
}
}
您还应该研究一下 c++ 提供的语法糖(运算符和所有)发生了什么。第一步是使用 cpp insights
:
https://cppinsights.io/s/15b32b55
点击 Play
按钮。注意到我提到的关于 operator bool
和 operator!
的事情了吗?它们在右侧清晰可见。没有更多的糖。
复制粘贴我给出的示例可能就足够了,但我强烈建议您花点时间理解我在评论中指出的所有这些内容。
相信我,长期的努力是值得的 运行。祝你学得开心!
int main() {
int choice;
while (true){
cout << "Enter choice: \n";
cin.clear();
cin >> choice;
switch(choice){
case 1:
cout << "you picked 1\n";
break;
case 2:
cout << "you picked 2\n";
break;
default:
cout << "invalid choice\n";
break;
}
}
}
大家好!你能帮我么?我制作了一个有多个选项可供选择的程序。问题是当我输入整数以外的东西时,它给了我一个无限循环。如何向输入抛出错误和 return?
已经有很多人对你的问题发表了很好的评论,我只是想提供一些提示,并为你指出一些链接以从中学习一些东西。
首先,不要忘记始终使示例代码完全运行并准备好编译:
- 你忘了包括
<iostream>
- 您使用的
cin
/cout
没有各自的命名空间 (std::
) - 也许你应该多构建一些代码,使用一个函数来指出你在做什么——比如:
getChoiceFrom(std::istream& stream, int& choice)
和printChoices(int choiceIGot)
——是的,有更好的选择,但你知道通过这些学习你的方式 What's the difference between passing by reference vs. passing by value?
对于第一部分,您可能已经完成了这些,但我建议您查看:Why is "using namespace std;" considered bad practice? - 也许您已经知道这一点 - 但我们无法从您的样本中判断出来。
让我们重写一些代码(我会故意尽可能地接近原始代码):
#include <iostream>
#include <limits>
int main() {
// I'd advise against it, but you could at least do:
using std::cout;
using std::cin;
// instead of using namespace std, which I assume you did
// OR, again, not recommended but at least limit the scope of
// using namespace std;
int choice;
while (true){
cout << "Enter choice: \n";
// `std::cin` (converts to `bool` indicating errors or lack thereof) - https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool
// this is the concise version but you can also split it
// it works because cin returns a reference to self when doing operator >> - also called insertion or put - https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2
if(!(cin >> choice)) {
cout << "invalid input! please enter a number\n";
// clear the error state of this input stream
// docs: https://en.cppreference.com/w/cpp/io/basic_ios/clear
cin.clear();
// ignore all the input that was passed by the user until a certain max limit
// https://en.cppreference.com/w/cpp/io/basic_istream/ignore
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// here you can:
// 1) continue the loop and re-ask for input
// continue;
// 2) get out of the loop and end the program (seems that that's what you want to do
break;
}
switch(choice){
case 1:
cout << "you picked 1\n";
break;
case 2:
cout << "you picked 2\n";
break;
default:
cout << "invalid choice\n";
break;
}
}
}
您还应该研究一下 c++ 提供的语法糖(运算符和所有)发生了什么。第一步是使用 cpp insights
:
https://cppinsights.io/s/15b32b55
点击 Play
按钮。注意到我提到的关于 operator bool
和 operator!
的事情了吗?它们在右侧清晰可见。没有更多的糖。
复制粘贴我给出的示例可能就足够了,但我强烈建议您花点时间理解我在评论中指出的所有这些内容。
相信我,长期的努力是值得的 运行。祝你学得开心!