0

I am trying to validate input, to accept only integers, and its working fine for letters and decimal points after 4. For example if I enter 1.22 it will read number one only and go to infinitive loop, but when I enter number what are bigger than 4, for example 5.55 it is working good then, how can I solve this problem? Thank you and appreciate your help!

void Furniture::getSelection()
{
    do {
        cout << "\nWhich object would you like to measure:\n"
             << "1.Table\n"
             << "2.Stool\n"
             << "3.Bookshelf\n"
             << "4.Exit\n" << endl;   

        while(!(cin >> choice)) {
            cerr << "The format is incorrect!" << endl;
            cin.clear();
            cin.ignore(132, '\n');
        }
        while(choice != 1 && choice != 2 && choice != 3 && choice != 4) {
            cerr << "Invalid Input!!Try again\n" << endl;
            break;
         }
    } while(choice != 1 && choice != 2 && choice != 3 && choice != 4);
3
  • 1
    Better validation would be to take input as a string and go from there. Even keeping it simple with ASCII-only validation takes a bit of work. And your while loop condition doesn't need to check each value individually, you just need two checks. That menu and selection should also not be a class member function. Choosing furniture has nothing to do with "being" furniture. Commented Mar 19, 2020 at 13:34
  • Note that if choice is an int then entering "1.22" will leave the ".22" in the cin buffer after cin >> choice. You need to get rid of that whether or not the insertion succeeds or fails. Or, see comment #1. Commented Mar 19, 2020 at 13:45
  • Side note: the inner while(choice != 1... should be an if. And the condition can be shortened to if (choice < 1 || choice > 4).... Commented Mar 19, 2020 at 13:48

1 Answer 1

1

Here's a short example program that can ensure an ASCII input is between 1 and 4 inclusive.

#include <exception>
#include <iostream>
#include <string>

int menu_selection() {
  int choice = 0;
  std::string input;

  do {
    std::cout << "\nWhich object would you like to measure:\n"
              << "1. Table\n"
              << "2. Stool\n"
              << "3. Bookshelf\n"
              << "4. Exit\n\n";
    std::getline(std::cin, input);

    // Handles the input of strings
    std::string::size_type loc = 0;
    try {
      choice = std::stoi(input, &loc);
    } catch (std::exception& e) {  // std::stoi throws two exceptions, no need
                                   // to distinguish
      std::cerr << "Invalid input!\n";
      continue;
    }

    // Handles decimal numbers
    if (loc != input.length()) {
      choice = 0;
    }

    // Handles the valid range
    if (choice < 1 || choice > 4) {
      std::cerr << "Invalid Input! Try again\n\n";
    }

  } while (choice < 1 || choice > 4);

  return choice;
}

int main() {
  int selection = menu_selection();

  std::cout << "You chose " << selection << ".\n";
}

This code does not belong in your Furniture class. Choosing furniture is not "being" furniture. The menu and selection should be outside the class, and then you make the appropriate calls to your Furniture class.

Another way of thinking about this is sharing the Furniture class with other devs. Maybe they don't care about measuring furniture. But now you've forced this measurement on them by including it in the class.

Sign up to request clarification or add additional context in comments.

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.