input validation and overflow in c++
<p>I'm writing a code that takes a user's input and calculates a discount based on how many units the user buys. Here is my problem; I want to use input validation to make sure the number entered is between 0 and 65535 (the max range for an unsigned int) but the way I have this program set up, if the user enters a number outside of this range I'm experiencing overflow/underflow and an incorrect number is stored in the variable before it ever even hits the if/else clauses. I'm new to C++ so please be kind. What can I do to check if this number is in the correct range when the user enters it. Also, is there a way to verify that the user has not entered a character other than a number?
Here is my code:</p>

<pre><code>#include&lt;iostream&gt;
#include&lt;iomanip&gt;

using namespace std;

int main()
{
    // display the instructions and inform the user of the discounts available

    cout &lt;&lt; " This software package sells for $99. Discounts are given according to the following list: \n";
    cout &lt;&lt; "----------------------------------" &lt;&lt; endl;
    cout &lt;&lt; " Quantity\t\t Discount" &lt;&lt; endl;
    cout &lt;&lt; "----------------------------------" &lt;&lt; endl;
    cout &lt;&lt; " 10 - 19\t\t 20%" &lt;&lt; endl;
    cout &lt;&lt; " 20 - 49\t\t 30%" &lt;&lt; endl;
    cout &lt;&lt; " 50 - 99\t\t 40%" &lt;&lt; endl;
    cout &lt;&lt; " 100 or more\t\t 50%" &lt;&lt; endl;
    cout &lt;&lt; "----------------------------------" &lt;&lt; endl;
    cout &lt;&lt; "\n\n";

    const double price = 99.00;

    unsigned short quantity;  // variable to hold the user's quantity. 
                              // shouldn't need more than 2 bytes for this (unsigned short)

    cout &lt;&lt; "How many units are sold?: ";
    cin &gt;&gt; quantity;

    double discount; // variable to hold the amount discounted
    double total; // to hold the total sales price

    cout &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setprecision(2); // set the display of numeric values

    // calculate the discounted prices

    if (quantity &gt;= 1 &amp;&amp; quantity &lt;= 9) {

        total = quantity * price; // calculate the total without a discount

        cout &lt;&lt; "There is no discount for this order \n";
        cout &lt;&lt; quantity &lt;&lt; " units were sold at $" &lt;&lt; price &lt;&lt; " a piece for a total of " &lt;&lt; total &lt;&lt; endl;
        cout &lt;&lt; "\n";
    }
    else if (quantity &gt;= 10 &amp;&amp; quantity &lt;= 19) {

        discount = (quantity * price) * .20; // calculate the discount
        total = (quantity * price) - discount; // calculate the total

        cout &lt;&lt; "There is a 20% discount \n";
        cout &lt;&lt; quantity &lt;&lt; " units were sold at $" &lt;&lt; price &lt;&lt; " with a discount of 20% applied to the order. \n";
        cout &lt;&lt; "The total cost of the sale is $" &lt;&lt; total &lt;&lt; endl;
        cout &lt;&lt; "\n";
    }
    else if (quantity &gt;= 20 &amp;&amp; quantity &lt;= 49) {

        discount = (quantity * price) * .30; // calculate the discount
        total = (quantity * price) - discount; // calculate the total

        cout &lt;&lt; "There is a 30% discount \n";
        cout &lt;&lt; quantity &lt;&lt; " units were sold at $" &lt;&lt; price &lt;&lt; " with a discount of 30% applied to the order. \n";
        cout &lt;&lt; "The total cost of the sale is $" &lt;&lt; total &lt;&lt; endl;
        cout &lt;&lt; "\n";
    }
    else if (quantity &gt;= 50 &amp;&amp; quantity &lt;= 99) {

        discount = (quantity * price) * .40; // calculate the discount
        total = (quantity * price) - discount; // calculate the total

        cout &lt;&lt; "There is a 40% discount \n";
        cout &lt;&lt; quantity &lt;&lt; " units were sold at $" &lt;&lt; price &lt;&lt; " with a discount of 40% applied to the order. \n";
        cout &lt;&lt; "The total cost of the sale is $" &lt;&lt; total &lt;&lt; endl;
        cout &lt;&lt; "\n";
    }
    else if(quantity &gt; 99 &amp;&amp; quantity &lt;= 65535) {

        // the maximum number allowed in a short int is 65535. I is unrealistic that someone would order more 
        // units than that so this else if clause checks to make sure the number of ordered items is below this number

        discount = (quantity * price) * .50; // calculate the discount
        total = (quantity * price) - discount; // calculate the total

        cout &lt;&lt; "There is a 50% discount \n";
        cout &lt;&lt; quantity &lt;&lt; " units were sold at $" &lt;&lt; price &lt;&lt; " with a discount of 50% applied to the order. \n";
        cout &lt;&lt; "The total cost of the sale is $" &lt;&lt; total &lt;&lt; endl;
        cout &lt;&lt; "\n";
    }
    else {

        // the trailing else clause is used to catch any value for quantity that is 0 or below or any quantity
        // bigger than what a short int can hold. 

        cout &lt;&lt; "You entered an invalid quantity.\n";
        cout &lt;&lt; "Please enter a value greater than 0 or less than 65,535. \n\n";
    }




    system("pause");
    return 0;
}
</code></pre>

<p>The final else clause is only executed when a value of 0 is entered. Here is an example of the output with a value outside the range</p>

<pre><code> This software package sells for $99. Discounts are given according to the following list:
----------------------------------
 Quantity                Discount
----------------------------------
 10 - 19                 20%
 20 - 49                 30%
 50 - 99                 40%
 100 or more             50%
----------------------------------


How many units are sold?: 65600
There is a 50% discount
52428 units were sold at $99.00 with a discount of 50% applied to the order.
The total cost of the sale is $2595186.00

Press any key to continue . . .
</code></pre>