Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Validate character input

thrillkisserthrillkisser Posts: 1Member
Hello Guys,

Im new to using C++. I have on equestion, please help me.

How fo I validate that the user has inputed a character and not numeric value.

char fname[25], lname[25];
int age;

cout<<"enter first name:
";
cin>>fname; // need to verify that user inputs char
//otherwise issue error message or confirmation

cout<<"enter last name:
";
cin>>lname;
cout<<"enter age:
";
cin>>age; // need to verify that user inputs integer
//otherwise issue error message or confirmation


Hope to hear from you guys.

many thanks...


Comments

  • pseudocoderpseudocoder Posts: 687Member ✭✭✭
    A basic approach for digits in a string would be to loop through the name using isdigit or

    for(int i=0; name[i] != ''; i++)
    if(isdigit(name[i])) // a digit is in name

    Detecting if a character exists in a number is a little more difficult because the input stream (cin) would have gone into a failed state - a character is wrong for an int data type. A cheap remedy would be to use assert(cin.good()) after reading in the value, but that's not really useful in the real world I don't think.

    You can test cin using cin.fail, and then attempt to recover the failed state with the ignore and clear members, but my preference is to use either a string or char array and the conversion functions so that I avoid (or at least lessen the chance of) stream errors altogether.

    HTH

    : Hello Guys,
    :
    : Im new to using C++. I have on equestion, please help me.
    :
    : How fo I validate that the user has inputed a character and not numeric value.
    :
    : char fname[25], lname[25];
    : int age;
    :
    : cout<<"enter first name:
    ";
    : cin>>fname; // need to verify that user inputs char
    : //otherwise issue error message or confirmation
    :
    : cout<<"enter last name:
    ";
    : cin>>lname;
    : cout<<"enter age:
    ";
    : cin>>age; // need to verify that user inputs integer
    : //otherwise issue error message or confirmation
    :
    :
    : Hope to hear from you guys.
    :
    : many thanks...
    :
    :
    :
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [blue]Always input a string - even for a numeric values, then validate it and then convert it into a type you need with atoi()/atof().
    [/blue]
    : A basic approach for digits in a string would be to loop through the name using isdigit or
    :
    : for(int i=0; name[i] != ''; i++)
    : if(isdigit(name[i])) // a digit is in name
    :
    : Detecting if a character exists in a number is a little more difficult because the input stream (cin) would have gone into a failed state - a character is wrong for an int data type. A cheap remedy would be to use assert(cin.good()) after reading in the value, but that's not really useful in the real world I don't think.
    :
    : You can test cin using cin.fail, and then attempt to recover the failed state with the ignore and clear members, but my preference is to use either a string or char array and the conversion functions so that I avoid (or at least lessen the chance of) stream errors altogether.
    :
    : HTH
    :
    : : Hello Guys,
    : :
    : : Im new to using C++. I have on equestion, please help me.
    : :
    : : How fo I validate that the user has inputed a character and not numeric value.
    : :
    : : char fname[25], lname[25];
    : : int age;
    : :
    : : cout<<"enter first name:
    ";
    : : cin>>fname; // need to verify that user inputs char
    : : //otherwise issue error message or confirmation
    : :
    : : cout<<"enter last name:
    ";
    : : cin>>lname;
    : : cout<<"enter age:
    ";
    : : cin>>age; // need to verify that user inputs integer
    : : //otherwise issue error message or confirmation
    : :
    : :
    : : Hope to hear from you guys.
    : :
    : : many thanks...
    : :
    : :
    : :
    :

  • blitzblitz Posts: 620Member
    : [blue]Always input a string - even for a numeric values, then validate it and then convert it into a type you need with atoi()/atof().
    : [/blue]

    Or, even better, use strtol()/strtod() for the conversion...

    Regards,
    Blitz

Sign In or Register to comment.