Thread: user input

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    16

    Unhappy user input

    i am still having problems trying to get the user to input a string and being able to allow them to use the arrow keys to go back and delete and re-enter the data before enter is pressed, i've tried using gets, fgets, and getchar but the program just keeps going crazy. when i was just using scanf everything worked but this didn't allow any validation checks as i was just entering the data using long to store it. any advice ? please

  2. #2
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    instead of posting a new thread keep continuing with you old one. i dont think there is a command that does what you want, so you have to make it yourself. you could make an array of chars and if a normal letter is pressed it saves it as an element of the array and prints on screen if user presses left arrow key you can make it go back one element and let the user input another value for that array element and so on.
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post a snippett of what you have written so far. Someone will advise you....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    16

    user input

    Code:
    /*printf("\n Please enter the ticket number\n 	");
      fgets(day.t_no,7,stdin);
    
       for(loop=0;loop<strlen(day.t_no);loop++)
        {
          if(!isdigit(day.t_no[loop]))
           {
             printf("\n Error non numerical ticket number! Try again.\n");
             fgets(day.t_no,7,stdin);
           }
       }  */
    day.t_no is a array 8 characters long and part of a structure

    this is the bit code that is giving me problems. when the program gets to this bit it asks for the ticket number and then immediatly displays the error message. the program worked perfectly when i was using a long integer to store the ticket number but then i changed it to a string to allow me to validate it and if possible to allow the user to change his input before enter is pressed.
    Last edited by hen; 06-29-2002 at 02:16 PM.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: user input

    >when the program gets to this bit it asks for the ticket number and then immediatly displays the error message.
    From this I presume that you mean the user doesn't get the opportunity to actually enter the number, the program appears to fly right passed that bit, right?

    If so, it's because the input buffer already contains data. Do you have call the other user input functions prior to this one? In particular, are you calling scanf()? If so, mixing scanf() and fgets() often gets people into trouble like this. To get round this, either stop mixing the 2 functions, or flush the input buffer like this in the correct places in your code:
    >while (getchar() != '\n');
    This simply reads from stdin until a newline is received. Each byte read is discarded. Thus is the effect of flushing the buffer.

    Also, don't forget that fgets() puts a trailing newline character into the array, which will make your number validation routine fail. You'll need to overwrite tha newline with a \0. One way of doing this is:
    Code:
    len = strlen(myarray);
    if (myarray[len-1] == '\n') myarray[len-1] = '\0';
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  4. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 06:37 AM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM