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.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.

use the search text box on the top right hand corner of your screen. search in this forum for 'validating input' or something like that. you will get some good ideas and approach.

if you want a simple solution, go here and check out operator<< and operator>>

the basic idea is to shove into the string stream a stream you read in when asking for a value
and to read into a float from the stringstream to have it formatted


if you want to make your own function to take care of this problem just for kicks, read on.

The problem you have with your second loop is that if someone inputs a character value, it is unable to read it in as an int, and for some reason it turns out to be near impossible to clear whatever is stuck in the input buffer, what you can do, if it isn't above you is to read in a string and pass the string to a function that returns a float.

in this function you make that returns a float, it returns a -1 if the string passed to it is not in the format of a number (check each character to see if it is between 0 and 9, or if it is a decimal, and make sure there are no more than 1 decimal points, if these conditions aren't met, pass back a -1 to indicate invalid input) if the string passes the criteria needed to be considered a valid number, you must then parse the string into a number.

First you need to make a value, num and initialize it to 0 to hold the parsed number. in a while loop, read in the numbers, if the numbers are before the decimal space, multiply num by 10 and then add the value of that number at that character in the string (char - '0' will return the character num into an integer num.)

if the character holding the numeric value is after the decimal point, add that value divided by 10 to the nth power where n is how many spaces away from the decimal point the character is to the variable num.

Then return num.

After typing all that out, I kind of wish I learned to use stringstream instead of making my own functions to do this (although time consuming, it was fun!)