Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You asked a lot.</p> <p>You want to validate user input. Users should not be able to enter huge numbers, non-integers, and so on.</p> <p>I'm going to start off by answering that <strong>this is absolutely not a scenario that exceptions should be used for.</strong> Exceptions are used to handle <strong>exceptional</strong> circumstances. These are ones you can't anticipate or really deal with.</p> <p>A user enters a number that's too big? You can handle that. Tell them that their number is too big, please enter a number between 1 and X.</p> <p>A user enters the word apple? You can handle that. Tell them that they can only enter integers.</p> <p>One way of doing this would be to make a <code>ValidateInput</code> function. You can have it return a number (or an enum, they're basically the same thing) to tell you whether there was an error.</p> <p>In order to do the validation, you will most likely have to receive input as an <code>std::string</code> and then validate it before turning it into a number. Getting input as an <code>unsigned int</code> or similar integral type doesn't really allow you to check for errors.</p> <p>This adds a bit of work, since you need to manually validate the input manually. There are libraries with functions to help with this, such as <code>boost::lexical_cast</code>, but that's probably too much for you right now.</p> <p>Below is some very basic psuedo code to illustrate what I mean. It's only meant to give you an idea of what to do, it won't compile or do the work for you. You could extend it further by making a generic function that returns a message based on an error code and so on.</p> <pre><code>enum error_code { SUCCESS, // No error OUT_OF_RANGE, // Out of the range specified by the user INCORRECT_VALUE, // If user input "one" instead of 1 RANGE_SIGNED, // If user inputs a signed value for range NUM_LIMITS // Number exceeds unsigned long long }; // This function will check if the input is valid. // If it's not valid, it will return an error code to explain why it's invalid. error_code ValidateInput(const std::string&amp; input) { // Check if input is too large for an unsigned long long if (InputIsTooLarge) return NUM_LIMITS; // Check if input is negative if (InputIsNegative) return RANGE_SIGNED; // Check if input is not an integer if (InputIsNotInteger) return INCORRECT_VALUE; // If we make it here, no problems were found, input is okay. return SUCCESS; } unsigned long long GetInput() { // Get the user's input std::string input; std::cin &gt;&gt; input; // Check if the input is valid error_code inputError = ValidateInput(input); // If input is not valid, explain the problem to the user. if (inputError != SUCCESS) { if (inputError == NUM_LIMITS) { std::cout &lt;&lt; "That number is too big, please enter a number between " "1 and X." &lt;&lt; std::endl; } else if (inputError == RANGE_SIGNED) { std::cout &lt;&lt; "Please enter a positive number." &lt;&lt; std::endl; } else if (inputError == INCORRECT_VALUE) { std::cout &lt;&lt; "Please enter an integer." &lt;&lt; std::endl; } else { std::cout &lt;&lt; "Invalid input, please try again." &lt;&lt; std::endl; } // Ask for input again return GetInput(); } // If ValidateInput returned SUCCESS, the input is okay. // We can turn it into an integer and return it. else { return TurnStringIntoBigInt(input); } } int main() { // Get the input from the user unsigned long long number = GetInput(); // Do something with the input } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload