Hello jcthomas556,
I worked with your program, but alas, I could not duplicate your described problem.
tpb makes a good point about the "isalpha". If you have not take a look at
http://www.cplusplus.com/reference/cctype/isalpha/
The do/while loop is going everything in the wrong way.
First what you need to understand is
cin >> basePrice;
. This is known as formatted input. Because "basePrice" is defined as a numeric variable that i what is expected from "cin". When you enter something other than a number it caused "cin" to fail and sets the fail bit of "cin".
Your do/while loop works better like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
cout << "Enter price: ";
cin >> basePrice;
while (!cin || basePrice < 0)
{
std::cout << "\n Invalid entry try again.";
if (basePrice < 0)
std::cout << "\n Price must be greater tha 0." << std::endl;
else
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
cout << " Enter price: ";
cin >> basePrice;
}
|
Notice that after printing an error message the next line, the if statement checks for less than 0 otherwise it clears the state bits and clears the input buffer. For line 13 tpb's method works, but this I believe is the more accepted and most used method. The last two lines allow you to enter a new price and stays in the while loop until a valid price is entered.
You might make the do/while loop work, but this is what I see more often.
Since there are two variables that take a numeric value tps's use of the function is a good idea and I do like the way it works. Something to think about for the future. Also if the function is generic enough you can use it in other programs.
It is better to use "double" over "float" as a double is more accurate and with the greater precision it will store numbers better. These days the "double" is preferred over the "float".
Since "weight" is a numeric variable a similar while loop would be a good idea after you enter the "weight".
Hope that helps,
Andy