hello guys this is my first post. I was wondering if you guys can help me out validating this peace of my crazy programming. Just taking a class this semester, and i have looked around every where but i cant seem to find a standard it seems like there are 20 different viable solutions but only some work. Here is my code.
-------
while (sale < 0)
{
sale = 0;
cout << "Error: You entered a negative number.\n";
cout << "Please enter a valid sale amount: ";
cin >> sale;
}
while (!cin || cin.gcount() != 0)
{
cin.clear();
//cin.ignore(INT_MAX);
//cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Not a numeric value.\n";
cout << "Please enter a valid amount: ";
cin >> sale;
}
----------
I was trying to validate the sale variable which is a float so that the user is forced to enter a non-negative, and not a letter. The non negative part works fine. The problem is the second while statement were i am trying to clear cin so that it wont do and endless loop, and also ask for the information. I tried the 2 that have the backslashes but both will ignore the information completely, and keep looping asking for the same information even if its right. Im not too sure how to use the cin.ignore feature but its the only thing that's stoping the loop. I know that it ignores the buffer and i guess i need something else to not ignore all the information and still stop the loop from being endless.
any help is appreciated.