0

So, I've been programming in C++ for a little while, and many of my programs involve checking for errors. For example, if a menu offers options 1, 2, 3, and 4, I have to handle situations like the user entering "three" or '#' instead. Presently, I use this:

int selection;
cout << "Enter your selection.\n";
cin >> selection;
while (cin.fail() || selection < 1 || selection > 4)
{
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin >> selection;
    cout << "Invalid input. Please ensure that your input is valid and try again: \n";
}

I am tired of having to type that over and over again in different programs, and often there is a lot of input validation like this required.

Does anyone know a faster/shorter way to do this in C++? It's quite possible that you more experienced coders will tell me that this is short as far as error checking goes, but if there is a shorter way, it's always good to know.

Please let me know if you need more clarification in the code or the question. Thank you in advance.

  • 2
    Write it once. Put it in a function. Include the function in any program you want. Call the function. – Eric Postpischil Jun 30 '22 at 19:20
  • 2
    It's not all that uncommon to see programs that are 10% actual work and 90% preventing the meatbag using the program from ing up. – user4581301 Jun 30 '22 at 19:20
  • A bit shorter if you replace `cin.fail()` with `cin >> selection`. – 273K Jun 30 '22 at 19:21
  • @Eric using a function is a good idea. –  Jun 30 '22 at 19:22
  • 1
    Sounds good @user4581301 I though some people might say that. –  Jun 30 '22 at 19:22
  • Slight correction to [above](https://stackoverflow.com/questions/72820990/is-there-a-shorter-way-to-perform-input-validation-in-c#comment128622992_72820990) `cin >> selection` should be `!(cin >> selection)` since you're looking for failure. – user4581301 Jun 30 '22 at 19:23
  • Ok 273K I will look at that –  Jun 30 '22 at 19:23
  • Ah good catch thank you! –  Jun 30 '22 at 19:23
  • I would ask that whoever downvote please tell me why; that will let me know how to improve posts in the future. And does anyone know why it closed? There are no similar questions. –  Jun 30 '22 at 19:24
  • Depends on how you look at it. If you go below the input validation, which it looks like you pretty much have correct, you are really asking "How do I reduce code duplication?" It's asked all the time, and the answer to that is almost always "Use a function." – user4581301 Jun 30 '22 at 19:31
  • Sounds good. I was just making sure there is not a way to reduce the code in that function. –  Jun 30 '22 at 19:34
  • I'm new to this site and perhaps that's why I' confused, but why was this voted down and closed. I asked how to shorten this code and reduce this code duplication. There is no such question yet. –  Jun 30 '22 at 19:40
  • I'm a bit iffy on the duplicate close. What πάντα ῥεῖ describes in the duplicate is more run-time efficient than what you have, but to me the question seemed more about programmer productivity efficiency and not writing the same code over and over. If you read the question the way πάντα ῥεῖ did, this gets asked a few times a week. Downvote and close. If you interpret it the way Eric Postpischil and I interpreted it, the answer is obvious, write a function, and questions with obvious answers tend to get downvoted. – user4581301 Jun 30 '22 at 19:47
  • Ah, so you are saying that the system must have read it like πάντα ῥεῖ. I'm not sure how that happens; they seem very different. Yes, in that case, I would get rid of the question, or not have even asked it in the first place for sure. Why ask if the answer is elsewhere? Now for you and Eric's interpretation, that's not what I meant also. Yes, you can write a function, which I recently did, but what I was wondering is this: within that function, can you shorten that code? In other words, you can have a function with 4 lines of code and one with 10 lines. It is a simple question. –  Jun 30 '22 at 20:12
  • This question is unrelated to the first (because it has nothing to do with strings or parsing) and therefore has no present answer. And if it were just about functions, I might downvote too. But that's not it: it's how to shorten the function itself. And that has no obvious answer. Unless of the top of your head and everyone else's you can name error-checking code different and shorter than mine. Simple: it's not obvious, nor is it duplicative. Thanks for letting me know and for taking the time to answer. This weirdness helps me learn the system I guess –  Jun 30 '22 at 20:15
  • 1
    The system made no interpretation. πάντα ῥεῖ closed the question based on how he read the question. He can do this because he has a long history of being right. We disagree in this case, but not enough that I'm going to reopen the question. The only thing I would change in your code is to do what 273K suggested and pop it into a function. – user4581301 Jun 30 '22 at 20:47
  • Oh ok thanks, I didn't know that one person could close a question. Thank you. And thank you to whoever upvoted :) –  Jun 30 '22 at 20:48

0 Answers0