Click to See Complete Forum and Search --> : fgets trouble
goon12
12-09-2002, 01:01 PM
I am having trouble using fgets, well actually it is causing a problem for me. Here is a simple example. I have a function called "int get_choice()", which returns an int, and another function "struct company get_info()" that returns a struct I am using. I use the function like so userchoice = get_choice();" If the user is choosing 1, then they will be prompted for the information using the get_info() function. I am calling it like this current_comp = get_info(); . Here is my problem. In the function get_info(), it prompts the user for someinfo like this
....
struct company in;
printf("Enter company name: ");
fgets(in.name, sizeof(in.name), stdin);
printf("Company Fax: ");
fgets(in.fax, sizeof(in.fax), stdin);
...
So the problem I have is this.. It wont let me enter anything for the company name, it skips the prompt as if something was passed ( by the prorgam ) to my first call of fgets(). This is only happening when I am getting int choice before calling the "get_info()" function. I am getting choice with "scanf("%d", &choice);" Am I calling fgets wrong? I have tried doing a memset(&in, 0, sizeof(in)); to set the entire struct to 0's before I use it, but get the same results.
bwkaz
12-09-2002, 02:14 PM
You need to "flush" the input stream. But you can't use e.g. fflush(stdin), because fflush only works with output streams (or at least, is only guaranteed to work portably for output streams).
What you need to do is, right after the scanf, read single characters (with e.g. getc or something) until you hit a '\n' character. Then it should work. If not, post more code. ;)
goon12
12-09-2002, 03:00 PM
Thank you bwkaz,
Seemed all I needed was a simple "getchar()" just before prompting for the first use of "fgets()".
Thanks again,
goon12
TheLinuxDuck
12-11-2002, 11:42 AM
goon12:
In my opinion, fgets is the best thing for buffered user input. The problem with using it comes into play when the users input is as long as or longer than the char * to which the inputted data will be stored.
When getting input, fgets looks for one of two things:
1. A newline
2. The maximum number of characters alloweed
If it finds a newline before reaching the maximum number of characters, then that newline will be the last character on the input (in the char *), the input buffer will be empty, and all is good. But, if it reaches the maximum number of allowable characters before a newline is found, it stops reading input, and now there is a newline (and possibly more data) still in the input buffer. This will affect any future calls to input functions using buffered input. They won't wait for input, because input is already available in the buffer.
What you're probably experiencing is that the data being input is exactly the same number of characters as the maximum allowed for the char *, and everything BUT the newline is left in the input buffer. Then, when fgets gets called again, it's finding that newline and using it as input, so the char * will end up being blank.
The getchar is working, probably, only because it's finding that newline. I bet if the input your typing is less than the maximum number of characters, the getchar will pause and wait for input, virtually causing your program to stall until enter is pressed.
The best way to deal with this is to:
1. Get input from fgets
2. Check for the existance of a newline in the input.
3. if a newline exists, remove it, and move on.
4. If it does not exist, clear the input buffer.
#include <stdio.h>
#include <string.h>
void getInput(char *myInput, const short int len)
{
int dummy, sLen;
fgets(myInput, len, stdin);
sLen = strlen(myInput) - 1;
if(myInput[sLen] =='\n') myInput[sLen] = '\0';
else while((dummy = fgetc(stdin)) != '\n');
}
int main(void)
{
char foofoo[25];
getInput(foofoo, 25);
printf("You entered %s\n", foofoo);
getInput(foofoo, 25);
printf("You entered %s\n", foofoo);
return 1;
}
That will handle buffered input nicely. (=
Hope this helps!
goon12
12-11-2002, 11:53 AM
Thank you TLD for the info. :)