Results 1 to 9 of 9

Thread: checking inputs

  1. #1
    Junior Member
    Join Date
    Dec 2001
    Posts
    7

    checking inputs

    Hi all
    I am learning C++ and I want to restrict users to input only valid data.for example:
    int age;
    cout<<"Enter age: ";
    cin>>age;

    now it should be an integer but what if user input a character like a,b,c etc.so now u understand what I mean?restricting users.any help?

  2. #2
    Senior Member cwk9's Avatar
    Join Date
    Feb 2002
    Posts
    1,207
    cout
    cin
    That binges back some memories.
    Basically all you have to do is make a loop with an if statement that checks the data to see if its valid. I'll just get out my c++ in 12 easy lessons book and see what I can dig up.

    int age = 0;
    int loop = 0;

    while (loop = 0) {
    cout<<"Enter Age: ";
    cin>>age;
    if (age > 21) {loop = 1;} else {cout <<"You are not over 21 and there for the beer vending machine is off limits to you";
    loop = o; }
    } // end of loop
    cout<<"You are over 21 get hammered and crawl home"<< endl;

    As for text I can't remember if cin automatically rejects it or if returns an error value when writing to a int variable. I don’t have c++ on this comp so I can’t test it.

    All you c++ gurus can feel free to make fun of my crappy coding ability.
    Its not software piracy. I’m just making multiple off site backups.

  3. #3
    ahmed: I dont know how to do that with an integer but if you take input in character form and the use the various functions of ctype.h like isdigit(),isalpha() etc you can check for integer or character input.

    cwk9:
    >>All you c++ gurus can feel free to make fun of my crappy coding ability.
    dont flame me you asked fo it

    >>while (loop = 0)
    this should be -> while(loop==0)

    >>if (age > 21) {loop = 1;}
    if there is just 1 statement in the loop there's no need for putting "{" and "}"

    >>loop=o;
    this should be -> loop=0;

  4. #4
    Senior Member cwk9's Avatar
    Join Date
    Feb 2002
    Posts
    1,207
    Well thats why im in networking not programing.
    Its not software piracy. I’m just making multiple off site backups.

  5. #5
    Senior Member
    Join Date
    Nov 2001
    Location
    Ireland
    Posts
    734
    Why don't you set type "char" for the variable and then convert it to an integer later?

  6. #6
    Senior Member
    Join Date
    Sep 2001
    Posts
    1,027
    Test cin.fail() to see if the input generated errors...

    Ammo
    Credit travels up, blame travels down -- The Boss

  7. #7
    Junior Member
    Join Date
    Dec 2001
    Posts
    7

    thanx for the replies

    HI all
    I take the number as char and it helps me to some extent but it is not so accurate
    like
    char age[2];
    do
    {
    gotoxy(1,1);
    cout<<"Enter age: ";
    gets(age);
    }

    while((age[0]=='\0')||(age[0]>='a'));

    ok this can restricts the user to some extents but what if user input shift+3=# or something like this.I want full restrictions but don't know how to do it.anyway thanx u all for some help.
    Actually a good programmers should take all precautions in order to delevop a good product which includes input restrictions,buffer-overflows and something like this.I am also learning some security now a days so I try to make my amateur coding as good as possible.
    bye and have fun

  8. #8
    dont use gets. it leads to buffer overflow.

    use ->

    Code:
    --
    --
    char age[2];
    int len;
    
    a:
    gotoxy(1,1);
    cout<<"enter name : ";
    cin.getline(age,2,'/n');
    
    len=strlen(age);
    switch(len)
    {
    case 1:
          if(!isalpha(age[0]))
          {
           cout<<"Error in inputting number : "; 
           goto a;
          }
    break;
    case 2:
          if(!isalpha(age[0]) && !isalpha(age[1]))
          {
           cout<<"Error in inputting number : "; 
           goto a;
          }
    break;
    }
    --
    --

    this is just roughly what you have to do. not tested this

    now before you all start flaming me for using goto just check if there is any instance where it will bypass any code or runaway with the program

  9. #9
    Senior Member
    Join Date
    Aug 2001
    Posts
    356
    Originally posted here by ihsir

    Code:
    ...
    cout<<"enter name : ";
    cin.getline(age,2,'/n');
    ...


    there is no need for the '/n'(which SHOULD be '\n' :-P) because the actual functionality of getline is to go untill a return, with the optional third input of a deliminator. specifying '\n' does nothing cause its gonna go untill '\n' anyway...


    now before you all start flaming me for using goto just check if there is any instance where it will bypass any code or runaway with the program
    goto still sucks...hell, they dont even teach it anymore. textbooks mention that the command exists but spefically say "using goto is not good programming practice blablabla"

    if for no other reason, its a better idea to not use goto mearly because people will not be used to seeing/using it.

    better code(further tweaked cause i'm just like that, hehe) would look like this:
    Code:
    #include<string.h>
    #include<ctype.h>
    ...
    ...
    char age[4];  //some people DO live to 100 + 1 char for NULL
    int len, i;
    bool dataIsValid = false;
    
    while(!dataIsValid)
    {
         cout << "enter age :  ";
         cin.getline(age, 3);
      
         len=strlen(age);
     
         dataIsValid = true; //is used flag to kick out when data found invalid
         for(i = 0; i<(len - 1) && dataIsValid; i++)
         {
                if( !isalnum(age[i]) || isalpha(age[i]) ) //cur char is not numeric
                      dataIsValid = false;
         }
    }
    -8-

    There are 10 types of people in this world: those who understand binary, and those who dont.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •