0

I have this piece of code and it makes an input check. It works until some point but when I enter for example "12rc" which is supposed to be invalid the check is skipped. How can I change it? Thank you in advance!

cout << "Enter your choice 1, 2, 3: ";
cin >> choice;
cout << endl;
while (cin.fail() || choice <=0 || choice >=4) {  // check input value
    cin.clear();
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    cout << "Wrong input value! Please enter only 1, 2, 3: ";
    cin >> choice;
    cout << endl;
2
  • Please edit your question and include the definition of 'choice'. Thanks. Commented May 17, 2016 at 11:09
  • Define "the check is skipped". C++ does not "skip" things. So your logic must be wrong! Step through the code with a debugger and examine the states of all your objects at each step.... Commented May 17, 2016 at 11:12

2 Answers 2

1

I give an assumption that you want to get an integer from the standard input stream. In other cases you may take the same idea and realize how to generalize your problem. I think that it might be solved somehow like this

#include <iostream>
#include <cctype>
#include <stdexcept>

void skip_to_int() {
    if (std::cin.fail()) {
      // try to fix up a mess in the input
      std::cin.clear();

      for (char ch; std::cin >> ch; ) {
        if (std::isdigit(ch) || ch == '-') {
            std::cin.unget()
            return;
        }
      }
    }

    // throw an error for example
    throw std::invalid_argument{"Not integral input"};
}

int get_int() {
  int n;

  // try to get the integer number
  while (true) {
    if (std::cin >> n) {
      return n;
    }

    std::cout << "Sorry, that was not a number. Try again" << std::endl;
    // if user inputed not an integral try to search through stream for
    // int occurence
    skip_to_int();
  }
}

int main() {
  std::cout << "Enter your choice 1, 2, 3: " << std::endl;

  int choice = get_int(); 
  while (choice <= 0 && choice >= 3) {
    // continue searching
    choice = get_int();
  }

  // process choice somehow
}
Sign up to request clarification or add additional context in comments.

Comments

0

There is nothing wrong with your code. It works fine for inputs like "12rc": http://ideone.com/Ma0j7r

The inputs:

12rc
0
a
11
$
10
2

Yield:

Enter your choice 1, 2, 3:
Wrong input value! Please enter only 1, 2, 3:
Wrong input value! Please enter only 1, 2, 3:
Wrong input value! Please enter only 1, 2, 3:
Wrong input value! Please enter only 1, 2, 3:
Wrong input value! Please enter only 1, 2, 3:
Wrong input value! Please enter only 1, 2, 3:

Is it possible that you had a space before the "2rc"? These inputs would be read as 1:

  • "1 2rc"
  • "1rc"
  • \n1\nrc"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.