Input Control

This is a discussion on Input Control within the C++ Programming forums, part of the General Programming Boards category; Hi all, Can any one help me with the following program? I want to make sure that user input is ...

  1. #1
    Unregistered
    Guest

    Question Input Control

    Hi all,

    Can any one help me with the following program?
    I want to make sure that user input is an integer. My code works reasonably well, except if user does not type anything and just hits enter, I want the program to capture it and says "INVALID INPUT, Try again..." or says "OUT OF RANGE, Try again..."
    Thanks in advance. My compiler Borland C++.

    //integer input control
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>
    #include <ctype.h>
    #include <string.h>

    int main()
    {
    do
    {
    int number=0;
    unsigned int pass,chk;
    char input[6]; //input control

    clrscr();

    do
    {
    cout<<"\t\t\t\tInteger Input Control"<<endl;
    cout<<"\t\t\t\t====================="<<endl;
    cout<<"Enter Integer Number (1 - 30000) ";
    cin>>input;
    pass=strlen(input);

    //check if isdigit
    for (chk=0;chk<pass;chk++)
    {
    if (!isdigit(input[chk]) || input[chk]=='£')
    {
    cout<<"INVALID INPUT! Try again...\n";
    pass=0;
    }
    }
    if (chk==pass)
    {
    number=atoi(input);
    if (number<1 || number>30000 || pass>5)
    {
    cout<<"OUT OF RANGE! Try again...\n";
    pass=0;
    }
    }
    } while (pass==0);
    cout<<"\nThanks, that's an integer number: "<<number;
    cout<<"\n\nAny key for more, Esc to quit.";
    } while (getch()!=27);

    return(0);
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    I believe there's an STL function called isdigit, don't have a reference handy, but look for that.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    I know there is an isdigit function but I cannot think of the header right now. Sorry.

  4. #4
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    you can indeed use isdigit(); it is included in the header file ctype.h and it returns a nonzero if the character is a digit, else it returns zero.

    so

    include <ctype.h>//required header

    int data; //data u wish to check

    if(isdigit(data))==0) {cout<<"Not a numeric entry!";}
    Monday - what a way to spend a seventh of your life

  5. #5
    Unregistered
    Guest

    Question

    Yeah, I am using isdigit headher file already, but what I want is to capture empty input from user, eg. if they don't type in anything, when they just hit enter, I want the program to say one of the two things "INVALID INPUT" or "OUT OF RANGE". At the moment if you just hit return key without typing anything, it just gives you a blank line. Any body can help or understand what I am trying to do? - Thanks all the same.

  6. #6
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    from my experience in c++, if while inputting a integer you press enter it will move to the next line and if you enter a char it will give an error

    the 'empty input catching' might work for strings and characters.

  7. #7
    Barjor
    Guest
    I don't have a ref here but look in Ctype.h . I know there is things in there to check for no input.
    ~Barjor

  8. #8
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    there are many checking facilities included in ctype.h
    you can check for control characters and punctuation characters.
    Monday - what a way to spend a seventh of your life

  9. #9
    Banned frenchfry164's Avatar
    Join Date
    Oct 2001
    Posts
    1,552
    I know there is a function that checks it, I had to use it once, but I haven't had a need for it in a long time so I forgot what the command is. I am 98% certain there is a function that does it.

    Man.....if you weren't using numbers you could just use the keycodes...

  10. #10
    Unregistered
    Guest

    Unhappy

    Whoa!
    with all these replies I'm still no where near solving my problem of checking for empty input. There must be a way to do it, can any body please give me the command or function to achieve this?? - thanks.

  11. #11
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    I think I know what u mean. Maybe two loops will do w/the first one checking if it is a digit and the second one checking if it is in range.

  12. #12
    Unregistered
    Guest

    Unhappy

    golfinguy4: I'm asking a 3rd checking for empty input, like pressing enter key without typing anything for the prompt to integer input? it seems to me nobody knows how this can be done. It's sad, very sad that such as this basic and simple requirement cannot be had easily. Come on expert coders show your real skills and true colours? - else I'll give up programming? - Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. large program code ,please help
    By Ash1981 in forum C Programming
    Replies: 14
    Last Post: 01-30-2006, 05:16 AM
  2. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  3. edit control - testing input
    By Micko in forum Windows Programming
    Replies: 2
    Last Post: 08-04-2004, 01:23 AM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 12:50 PM
  5. Problem with changing input focus
    By Clyde in forum Windows Programming
    Replies: 2
    Last Post: 05-25-2002, 05:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21