0

I need help with Linked Lists

My code needs to be able to enter in a person's name, age and weight. Here's where I am so far https://code.sololearn.com/cuQILz73yb92/#c When I put my code into Visual Studio though, it skips the name

23rd Nov 2020, 12:23 AM
Riley Freeman
Riley Freeman - avatar
15 Respostas
+ 1
Can you give me input examples I can test the code with? I don't know for sure cause I haven't fully read the code and understand the expected behaviour of the code. But maybe you can try flushing input stream buffer after reading <n> (amount of data to be inserted).
23rd Nov 2020, 12:44 AM
Ipang
+ 1
Do I give same input (name, age, weight) for when I give <n> greater than 1? let's say <n> is 3, then I give 3 set of input or what?
23rd Nov 2020, 1:02 AM
Ipang
+ 1
Is there a reason for the space in the scanf of line 28? scanf("%[^\n]% *c", &temp->name); I never saw a "% *c"
23rd Nov 2020, 2:37 AM
Davide
Davide - avatar
+ 1
There is also the & wrong. temp->name is an array, you don't have to put the & before the array name. Since the array name is a pointer to its first element you either do: scanf("%[^\n]s%*c", temp->name); or: scanf("%[^\n]s%*c", &temp->name[0]); EDIT: also you are missing an "s" after [^\n] I think that your professor put this little bugs knowing that somehow the program would run good enough
23rd Nov 2020, 3:39 AM
Davide
Davide - avatar
+ 1
LastSecond959 you are right, thanks for letting me know đŸ€—
24th Nov 2020, 12:14 AM
Davide
Davide - avatar
0
When the user selects (1), it should prompt them for a name, age, and weight, which will insert the record into the linked list which will be alphabetically ordered by name (first letter of full name). like 1 Abby 23 115 even though my code also prompts the user for the amounts of records they want to input
23rd Nov 2020, 12:56 AM
Riley Freeman
Riley Freeman - avatar
0
yes, the same input
23rd Nov 2020, 2:00 AM
Riley Freeman
Riley Freeman - avatar
0
Davide, my college prof. put it like that. He didn't explain why.
23rd Nov 2020, 2:45 AM
Riley Freeman
Riley Freeman - avatar
0
All right. Anyway try to change this scanf with: scanf("%s", temp->name); Or if you want to read multiple words use fgets instead: fgets(temp->name, 100, stdin); But keep in mind that in this case your temp->name will contain a new line at the end. If you want you can remove it doing: temp->name[ strlen(temp->name) -1 ] = '\0';
23rd Nov 2020, 3:02 AM
Davide
Davide - avatar
0
nothing worked, Davide
23rd Nov 2020, 3:20 AM
Riley Freeman
Riley Freeman - avatar
0
Nice. Anyway I've searched for the space in "% *c" and I couldn't find anything, so it's probably an error. It was meant to be %*c to read and ignore the new line after the entered name.
23rd Nov 2020, 3:26 AM
Davide
Davide - avatar
0
Yeah, I tried it like %*c and It gave me an error
23rd Nov 2020, 3:30 AM
Riley Freeman
Riley Freeman - avatar
0
Change the first %d to %d%*c. And make your life easier please, you have *start but why no *end (it's ok if you want the challenge or try something out on your own). The last thing is, when you make char a[] = { 0 };, I suggest you to avoid it, use proper size for the array and when you do {0} it means that a[0] = '\n', there's nothing wrong with it but if you want to "empty" it, you can just simply do {} without 0 in it, except for initialization for specific index like char a[n] = { 65 } which makes a[0] to be 'A', or char a[n] = {[5] = 'B'};. Davide Putting s after %[^\n] is unnecessary, there are a lot of sources, here's one of them, https://www.quora.com/What-do-we-call-n-s-in-C-programming
23rd Nov 2020, 11:49 PM
LastSecond959
LastSecond959 - avatar
0
%*c acts like getchar(), the difference is that getchar() TAKES the input but not storing it unless you store it e.g char ch = getchar(); while %*c completely IGNORES the input of a character. Your prof put it that way probably because he was trying to do 2 string inputs in different row (let's just say it's adjacent row), why your name doesn't take any input (skipped)? Because after you scanf an int, the '\n' that comes from your enter button is still on the buffer, floating, and after that you do a scanf which reads an input until \n is detected, so it immediately reads the \n that still floating in the buffer and that's why it look like it skipped, to be safe, before scanf a str, put %*c in the scanf before, like %d%*c, or put getchar() after the scanf before. Optionally, you can make a macro like #define clr_buff while(getchar() != '\n'); before every string input (I don't suggest this one btw). And also, %[^\n] means it reads inputs until \n, but the \n is excluded, that's why your prof did what he did.
24th Nov 2020, 12:15 AM
LastSecond959
LastSecond959 - avatar
0
Thanks, I really appreciate the help!
30th Nov 2020, 4:57 AM
Riley Freeman
Riley Freeman - avatar