Hello,

I am having problem with fgets accepting string from the keyboard.

I have this statement : fgets(T , sizeof(T) , stdin); in the code, but this thing is not accepting anything. I mean I have 2 strings to accept where I used fgets for the first, and then scanf for the second...

Only the scanf is being accepted.

I tried variants for fgets where sizeof(T) would be some int value. Originally the string was a part of a struct, but I made into T and copied into struct string... None work

Any help is greatly appreciated,
Thanks

More code please. Preferably a complete program that exhibits the problem so we can play with it.

More code please. Preferably a complete program that exhibits the problem so we can play with it.

Sure...

This is a part of the code...

for(i=0;i<n;i++)
	{
		printf("Employee #%d\n\n",i+1);
		printf("Name : ");
		fgets(T , sizeof(T) , stdin);
		strcpy(D[i].name,T);
		//scanf("%s",D[i].name);
		printf("Designation : ");
		scanf("%s",D[i].Designation);
		printf("Employee no. : ");
		scanf("%ld",&D[i].no);
		printf("No. of projects worked : ");
		scanf("%d",&D[i].pro);
		printf("\n");
		fprintf(k,"Employee %d\n  Name : %s\n  Designation : %s\n  Employee no. : %d\n  No. of projects worked : %d\n\n",i+1,D[i].name,D[i].Designation,D[i].no,D[i].pro);
	}

Where D[] is a structure variable & name, des etc., being its members...


In this, designation is accepted, but not name(fgets). If it was scanf, it would accept, but, only one word.

I hope this much code is enough

Thanks.

You're mixing scanf() with fgets().
scanf() leaves a \n almost all of the time, then fgets() does "mmm, yummy, a \n. I'm done here".

If you're going to use fgets(), use it for EVERYTHING, and then use sscanf() if you must.

Or one of many ideas on flushing the input stream which is NOT fflush(stdin).

char T[25];
fgets( T,25, stdin);

work fine with my cmplr...

It works fine with only fgets() independent. I guess the code with scanf is making the hampering as Mr. Salem said...

You're mixing scanf() with fgets().
scanf() leaves a \n almost all of the time, then fgets() does "mmm, yummy, a \n. I'm done here".

If you're going to use fgets(), use it for EVERYTHING, and then use sscanf() if you must.

Or one of many ideas on flushing the input stream which is NOT fflush(stdin).

Hello, I tried it as you said, but its still not working. Incidentally, its taking the value of Designation but no name!!!! and added to that, fgets is also putting 2 '/n' after I complete the input... May be that which I input(by pressing enter) + it adds an additional.

Can you suggest me some way around this?

By the way, I am using Dev-C++ now while last time I asked another question, I was using VC++, but I don't have it now:(

Thanks

Hello, I tried it as you said, but its still not working.

Show us the code.

Incidentally, its taking the value of Designation but no name!!!! and added to that, fgets is also putting 2 '/n' after I complete the input... May be that which I input(by pressing enter) + it adds an additional.

Show us the code; the modified code.

Can you suggest me some way around this?

I hope by now, you would understand that your words are pretty much irrelevant without showing us the pertaining portion of the code.
Any suggestion would be like shooting at a can of coke, with eyes closed.

Comments
Discipline

Show us the code.

Show us the code; the modified code.

I hope by now, you would understand that your words are pretty much irrelevant without showing us the pertaining portion of the code.
Any suggestion would be like shooting at a can of coke, with eyes closed.

OK! Sorry again!

for(i=0;i<n;i++)
   
      {
   
      printf("Employee #%d\n\n",i+1);
   
      printf("Name : ");
   
      fgets(T , sizeof(T) , stdin);
   
      strcpy(D[i].name,T);
   
      printf("Designation : ");
   
      fgets(T , sizeof(T) , stdin);
   
      strcpy(D[i].Designation,T);
        
      printf("Employee no. : ");
  
      scanf("%ld",&D[i].no);
  
      printf("No. of projects worked : ");
  
      scanf("%d",&D[i].pro);
  
      printf("\n");
  
      fprintf(k,"Employee %d\n Name : %s\n Designation : %s\n Employee no. : %d\n No. of projects worked : %d\n\n",i+1,D[i].name,D[i].Designation,D[i].no,D[i].pro);
  
      }

The part of the program o/p goes like this:

Enter the details one by one

Employee #1
Name : Designation : xxxxxxx
Employee no. : xxxxx
No. of projects worked : xxxx

Employee #2
Name : Designation : xxxxx
 
& so on...

The o/p in the file is :

Employee 1
  Name : 

  Designation : xxxxx

  Employee no. : xxxx
  No. of projects worked : xxxxx 


& so on...

But the file output in file originally expected:

Employee 1
  Name : xxxx xxx
  Designation : xxxxxx
  Employee no. : xxxx
  No. of projects worked : xxxx


& so on...

All the "xxxxxxx" here represent value that user inputs.


I think this much is enough to understand my problem...

Al help is greatly appreciated,
Thanks

Edit-
Sorry, I forgot to mention

In the code I didn't use fgets(T , sizeof(T) , stdin); . Instead, I used, the bufsize of T, ie 50.

OK, so what part of fgets() + sscanf() did you not understand?

> printf("Employee no. : ");
> scanf("%ld",&D.no);
Should be

printf("Employee no. : ");
      fgets( temp, sizeof temp, stdin );
      sscanf( temp, "%ld",&D[i].no);

temp is just a char array, I typically use BUFSIZ for it's size.

Comments
Good helper

OK, thanks. I got the program to work.

Though, I didn't use sscanf completely, I worked I way around. Now, I am trying the program with fgets & sscanf replaced for all. I think it should work.

Thanks for your kind help salem.
I greatly appreciate it.

This question has already been answered. Start a new discussion instead.