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;
}
}
}
Hello guys! Can you please help me? I made a program that has multiple options to choose from. The problem is when I enter something other than an integer, it is giving me an infinite loop. How do I throw an error and return to the input?
CodePudding user response:
There are great comments on your question already, I just wanted to give a few hints and point you to some links to learn something from this.
First, don't forget to always make the sample code fully working and ready to compile:
- you forgot to include
<iostream>
- you used
cin
/cout
without their respective namespace (std::
) - you should, maybe, structure the code a bit more, use a function to point out what you are doing - something like:
getChoiceFrom(std::istream& stream, int& choice)
andprintChoices(int choiceIGot)
- yes, there are better options, but you know work your way through those to learn What's the difference between passing by reference vs. passing by value?
For the first part, you probably already have those done, but I'd suggest seeing: Why is "using namespace std;" considered bad practice? - maybe you already knew this - but we can't tell from your sample.
Let's rewrite some of the code (I'll keep it as close to the original as I can, intentionally):
#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;
}
}
}
You should also look into what happens with the syntactic sugar that c provides (operators and all). A first step is using cpp insights
:
https://cppinsights.io/s/15b32b55
Hit the Play
button. Notice the things I mentioned about the operator bool
and operator!
? They are in plain sight in the right hand side. No more sugar.
Copy-pasting the sample I gave may be enough, but I strongly advise you take your time to understand all these things I pointed in the comments.
Trust me, It's worth the effort in the long run. Have fun learning c !