Thread: Problem about reading an int, but allow ESC to be pressed

  1. #1
    Registered User
    Join Date
    Nov 2001
    Location
    Mexico City
    Posts
    33

    Thumbs up Problem about reading an int, but allow ESC to be pressed

    Hi There.

    I'm making a routine in C that reads a simple int. I want this routine to allow ESC key, so when the user changes his/her mind about continuing entering digits, he or she can press ESC to exit this routine.

    I have been searching in this site all the way around, and seems that there's no way Hard to beleive! I have studied getch(), getc() getchar(), etc.. as well as kbhit(), nothing seems to work. does anyone of you genious have any idea?

    Here's what I have so far:

    int ReadNumber( int *number) {

    /* modifies it's argument depending on what number the user types, if the user change his/her mind he/she can press ESC and the function returns 1 if so, otherwise, it returns 0 */

    {
    char c,digit;
    char strnumber[10];
    int i=0;


    printf("Enter your number (ESC to quit) :");

    do {

    c = (char) getch();
    if ( is_a_digit(c, *digit) ) {
    strnumber[i++] = digit;
    }
    else { c = int(c);
    switch(c) {
    case 8 : /* backspace bla bla */
    break;
    .
    .
    .
    } /* switch */
    } /* else */

    } while (c !=27);

    strnumber[i] = '/0' /* sorry couldn't find the backslash in this machine */
    *number = atoi(strnumber);
    return(0);
    }


    int is_a_digit (char c, char *dig) {

    /* assume this returns 1 if c is a digit, and if so puts it in *dig, otherwise return 0 . Could you do that for me, thanks */

    }


    I run it but when the user types ESC, I have a little arrow on the screen (pointing to the left).
    Is there any other way to catch digit by digit to analyse them?
    I'm working with Dev-C++, windows 98, pentium processor.

    Thanks in advance.

    Gustaff.










    If you want to be happy one hour: take a nap
    if you want to be happy one day: go fishing
    If you want to be happy a year: inherit a fortune
    if you want to be happy for a life time: HELP SOMEBODY
    chinisse say.

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    else { c = int(c);
    switch(c) {
    case 8 : /* backspace bla bla */
    break;
    .
    .
    .
    } /* switch */
    } /* else */

    } while (c !=27);

    strnumber[i] = '/0' /* sorry couldn't find the backslash in this machine */
    *number = atoi(strnumber);
    return(0);
    }
    Perhaps someone can add onto my reply, but I believe I had this problem at once.

    Insinde of a switch / case statement, it would be:
    Code:
    char AcceptCharacter; //Whatever name you want to use
    AcceptCharacter=getch();
    switch(AcceptCharacter)
    {
        case 27:
        //Do code for when the user presses escape
    
        default:
        //Your default case
    
        case '1':
        case 'b':
        // case etc,
        //Your other cases
    }
    BTW, what is this code for?

    I see some switch and case statements in there. I'm making a program that is switch and case statement heavy. It has ALOT of goto's inside of it. I'm thinking of re-doing bits and pieces of the code. Ya know? Analyze, test, change, & retest something until it's as good as you can make it before continuing.

    Menu
    Albeit simple, take a look at "Menu". It's a personal program of mine I'm making.
    Last edited by Shadow; 03-07-2002 at 11:48 AM.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM