Checking of inputs.

Hello there.

Is there any where to check what users input, in a simpler way?

for example.

cout<<"What is your name : ";
cin>>userName;

Am I able to also set all to CAPS? In that cin statement?

cout<<"what is your age : ";
cin>>userAge;

Can I put a check that it is integer?
As long as userAge is a integer, you need no check, and the checks are done from the STL.

As long as you write such:
1
2
int userAge=0;
cin>>userAge;

userAge will be a integer, and if user inputs something wrong, he will have to write twice the input.

If you want to set all text in upper case, get the text and loop in all the string with a "toupper" function (Example:
1
2
3
4
5
6
7
8
char Text[512];
cin>>Text;
unsigned int Length = strlen(Text);
for(unsigned int i = 0; i < Length; ++i)
{
    Text[i] = toupper(Text[i]);
}
// Now Text is uppercase. 


You can also get raw text and convert it to Number with atoi or strtoul/strtol/strtod.
You also have the opposite way (itoa). There is no ftoa unless you make your own routine.
Last edited on
What about checking for strings that is integers only?
I Fixed a Typo:
You can also get raw text and convert it to Number with atoi or strtoul/strtol/strtod.
Topic archived. No new replies allowed.