### Validating a Price.

Okay, I have been trying to figure this out for a week now just by looking up online. Now I have to ask for your help.
I am trying to verify that the price I have are all numbers with the decimal in the 5th position, i.e. 2435.12. I can't seem to get this thing to read the non numeric digit as a non numeric digit. Here is my code:

 ``12345678910`` `````` void vPrice() { char errorPrice; const string Price = "1100./0"; for(int i = 0; i < Price.length(); i++) if(! (Price[i] >= '0' && Price[i] <= '9' || Price[4] == '.' || Price[i] == ' ') ){ errorPrice = 'y'; break;} else errorPrice = 'n'; }``````
bump
use
 `` `` ``double price = atoi (Price);``
 ``12345678910`` `````` //assume correct entry char errorPrice = 'n'; //------------------------- //get the length of the string int length = Price.length() - 1; //ignore the terminating character if(Price[length-3] != '.') //check the position from the right isn't equal to '.' errorPrice = 'y';``````

question i have for you is why store price like this? Keep it as a double and format the output.

 ``12`` ``````double Price = 1200.324; std::cout<

by doing it like this you can operate on the value.
I am getting the string "1100./0" from an external file. I am using the getline to read it. Then I am using the errorPrice variable to make a decision in my main to either input the record or put out an error message and reject that line.
 ``1234567`` ``````for(int x = 0; x < Price.length() -1; x++) { if((int)Price[x] >= 48 && (int)Price[x] <= 57 || (int)Price[x] == 46) continue; else return false; }``````

I'm using the ascii codes here. What i'm doing is casting the value of the character as an integer and checking if the value is within the range of the ascii equivalent. if ti is, i continue. If it isn't no need to go on, the number is not valid.
Topic archived. No new replies allowed.