Fgets not working, is like jumped plz help.(line 26) , problem is i want the user input full name(exm. "Foo Boo") and not "Foo". | Sololearn: Learn to code for FREE!

0

Fgets not working, is like jumped plz help.(line 26) , problem is i want the user input full name(exm. "Foo Boo") and not "Foo".

https://code.sololearn.com/ciHH0g5WqVM6/?ref=app

c

9/25/2021 8:59:47 PM

Omerg

15 Answers

New Answer

+1

In your scanf() calls, put an extra whitespace after the format specifier: scanf( "%d ", ...); ^--- Right now, fgets() is blocked by the newline character entered after a number, which remains in the input buffer. Adding that whitespace will have scanf() consume all additional whitespace after the number, which is turn means fgets() will not immediately stop reading characters because it encounters a whitespace character first.

+1

Yes.

+1

In that case, don't do what I said earlier, but insert a single scanf( " " ); ^--- space right before the call to fgets() instead. That should still take care of the whitespace issue, while properly displaying the printf() messages (well, it at least does on my terminal).

+1

That sounds weird. I'll have to look into it later though, after getting some sleep. Can you update your code in the description to reflect the current version?

+1

Oh, you simply switched fgets() and scanf(). This is what I meant: ... scanf(" "); fgets((ptr+i)->name, sizeof((ptr+i)->name), stdin); ...

+1

As far as I can see, the ':' is there because you printed it yourself: printf("\nName -> %s: ", (ptr+i)->name); So you could just change the output to: printf("\nName -> %s", (ptr+i)->name); Note fgets() stores the final newline character in the string, that is why the ':' appears in a new line. If you also want to get rid of that, you'll have to do so manually.

0

Sorry for my bad english, you mean scanf leaves newline in buffer and then fgets encoutres it by loop?

0

I did that, and when i hit input n=3 Nothing get show

0

It does fgets work but not show me lines 24-25 before fgets

0

I did that it worked but its skipped age and id heres the output -> Item#0 Enter name -> Foo Boo Enter age -> Enter id -> Item#1 Enter name -> Foo Bar Enter age -> Enter id -> Item#2 Enter name -> Bar Foo Enter age -> Enter id -> Item#0 Name -> : Age -> 0 Id -> 0 Item#1 Name -> Foo Boo : Age -> 0 Id -> 0 Item#2 Name -> Foo Bar : Age -> 0 Id -> 0

0

Not a problem my friend

0

#include <stdio.h> #include <stdlib.h> struct humans { char name[15]; int age; int id; }; int main () { struct humans *ptr; int n, i; printf("\nEnter number of persons: "); scanf("%d", &n); ptr = (struct humans*) malloc(n*sizeof(struct humans)); printf("\n"); for(i=0; i<n; i++) { printf("Item#%d", i); printf("\nEnter name -> "); fgets((ptr+i)->name, sizeof((ptr+i)->name), stdin); scanf(" "); printf("Enter age -> "); scanf("%d", &(ptr+i)->age); printf("Enter id -> "); scanf("%d", &(ptr+i)->id); printf("\n"); } printf("\n"); for(i=0; i<n; i++) { printf("Item#%d", i); printf("\nName -> %s: ", (ptr+i)->name); printf("\nAge -> %d", (ptr+i)->age); printf("\nId -> %d", (ptr+i)->id); printf("\n\n"); } free(ptr); printf("\n"); return 0; }

0

It seems to worked, but do you know how to remove ' : ' after printf here -> Item#0 Enter name -> Foo Bar Enter age -> 24 Enter id -> 111 Item#1 Enter name -> Bar Foo Enter age -> 21 Enter id -> 333 Item#2 Enter name -> Foo BAR Enter age -> 20 Enter id -> 222 Item#0 Name -> Foo Bar : Age -> 24 Id -> 111 Item#1 Name -> Bar Foo : Age -> 21 Id -> 333 Item#2 Name -> Foo BAR : Age -> 20 Id -> 222

0

Every thing worked but last thing how do i get rid of newline fgets manually ? Can you show me Cause after 'Name' theres newline print Item#0 Name -> Foo Bar Age -> 56 Id -> 333 Item#1 Name -> Bar Foo Age -> 34 Id -> 22 Item#2 Name -> Foo Foo Age -> 32 Id -> 111

0

Thanks for your help Shadow I just needed to add "(ptr+i)->name[strlen((ptr+i)->name) - 1] = '\0'; " After fgets to remove newline