Amount validation

hi all,

can anyone enlighten me on this? i need a validation function that must only take in 'numbers' and '.'

for example the amount i want to enter is 9.90
but what my code is taking in is all other punctuation also. like !:"+()

my code is as follows:

bool valid=true;

if(income == "0")
{
cout << "\n";
cout << "Invalid income entered." << endl;
cout << "Income should not be 0." << endl;
cout << "\n";
valid=false;
}
else
{
for(int j=0; j<income.length(); j++)
{
if(!isdigit(income[j]) && !ispunct(income[j]))
{
cout << "\n";
cout << "Invalid income entered." << endl;
cout << "Income should not contain alphabets/characters." << endl;
cout << "\n";
valid=false;
}
}

any help/comments are welcome. Thanks alot!!
sorry forget to add the main body

do
{
cout << "Enter income amount or other cash inflows values:" << endl;
cout << "$";
cin >> income;

valid = incomeValid(income); // Income validation
}
while(!valid);

thanks!
Declare 'inc ' as a double then write the double val_income(double inc) function and place it in a loop which will exit if 'inc' is true.
1
2
3
4
5
6
7
...double inc=0.0;
double val_income(double inc)
{
if(inc>100.0 && inc<100000.0) cout<<"income is valid"<<endl;
else cout<<"income not valid"<<endl;
}
 

hth`s
Topic archived. No new replies allowed.