PDA

Click to See Complete Forum and Search --> : How to validate cin input?


transcendental
Jan 3rd, 2002, 01:33 AM
Now I am making transition from C to C++.

In the past using C, I validated by using getchar or gets, then I validated them.

For example, the input is supposed to be integer, I will check the string for any non-numeric char, if no, I will convert it to int.

Now I was writing a program using cout and getchar. It compiles alright and links alright but it hangs after cout and not getting any input from me. ????

I have changed all to cin as I am the only one who use the program so I can't enter any invalid input, given the fact the program is written by me too.

Given the scenario, the program's end user is not me, how do I verify?

Thanks.

[praetorian]
Jan 3rd, 2002, 04:03 AM
To validate users input you can use cctype.h with its functions like isalpha(), isdigit(), etc.......

To convert from integer to double, I think you can just write double(entered numbers).

CornedBee
Jan 6th, 2002, 10:41 AM
int i;
cin >> i;
You don't need to validate. i will be an integer no matter what (worst case: i will be 0).