-
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
-
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.
-
Post a snippett of what you have written so far. Someone will advise you....
-
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.
-
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';