Reputation:
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:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
// display the instructions and inform the user of the discounts available
cout << " This software package sells for $99. Discounts are given according to the following list: \n";
cout << "----------------------------------" << endl;
cout << " Quantity\t\t Discount" << endl;
cout << "----------------------------------" << endl;
cout << " 10 - 19\t\t 20%" << endl;
cout << " 20 - 49\t\t 30%" << endl;
cout << " 50 - 99\t\t 40%" << endl;
cout << " 100 or more\t\t 50%" << endl;
cout << "----------------------------------" << endl;
cout << "\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 << "How many units are sold?: ";
cin >> quantity;
double discount; // variable to hold the amount discounted
double total; // to hold the total sales price
cout << fixed << showpoint << setprecision(2); // set the display of numeric values
// calculate the discounted prices
if (quantity >= 1 && quantity <= 9) {
total = quantity * price; // calculate the total without a discount
cout << "There is no discount for this order \n";
cout << quantity << " units were sold at $" << price << " a piece for a total of " << total << endl;
cout << "\n";
}
else if (quantity >= 10 && quantity <= 19) {
discount = (quantity * price) * .20; // calculate the discount
total = (quantity * price) - discount; // calculate the total
cout << "There is a 20% discount \n";
cout << quantity << " units were sold at $" << price << " with a discount of 20% applied to the order. \n";
cout << "The total cost of the sale is $" << total << endl;
cout << "\n";
}
else if (quantity >= 20 && quantity <= 49) {
discount = (quantity * price) * .30; // calculate the discount
total = (quantity * price) - discount; // calculate the total
cout << "There is a 30% discount \n";
cout << quantity << " units were sold at $" << price << " with a discount of 30% applied to the order. \n";
cout << "The total cost of the sale is $" << total << endl;
cout << "\n";
}
else if (quantity >= 50 && quantity <= 99) {
discount = (quantity * price) * .40; // calculate the discount
total = (quantity * price) - discount; // calculate the total
cout << "There is a 40% discount \n";
cout << quantity << " units were sold at $" << price << " with a discount of 40% applied to the order. \n";
cout << "The total cost of the sale is $" << total << endl;
cout << "\n";
}
else if(quantity > 99 && quantity <= 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 << "There is a 50% discount \n";
cout << quantity << " units were sold at $" << price << " with a discount of 50% applied to the order. \n";
cout << "The total cost of the sale is $" << total << endl;
cout << "\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 << "You entered an invalid quantity.\n";
cout << "Please enter a value greater than 0 or less than 65,535. \n\n";
}
system("pause");
return 0;
}
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
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 . . .
Upvotes: 0
Views: 1495
Reputation: 1090
So in all honesty, I don't think this is a bad question.
It just deals with error checking whatever comes out of cin >> quantity
.
Like described here: User Input of Integers - Error Handling, a way to handle this is to wrap the cin >> quantity
with some error handling code like below.
if (cin >> quantity) {
// read succeeded
} else if (cin.bad()) {
// IO error
} else if (cin.eof()) {
// EOF reached (perhaps combined with a format problem)
} else {
// format problem
}
This will not take care of the integer overflows however, so a full solution would be to make quantity
an int
and use
cout << "How many units are sold?: ";
if (cin >> quantity) {
// read succeeded
// check for range
if (quantity < 0 || quantity > 65535) {
cout << "Number needs to be between 0 and 65535" << endl;
return -1;
}
} else if (cin.bad()) {
// IO error
cout << "Couldn't do a read from stdin :(" << endl;
return -1;
} else if (cin.eof()) {
// EOF reached (perhaps combined with a format problem)
cout << "Stdin gave EOF :(" << endl;
return -1;
} else {
// format problem
cout << "Encountered incorrect format" << endl;
return -1;
}
Upvotes: 0