+ 3

Why can't I input name of student structure? Help me

#include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct{ char name[50]; int d,m,y; float Mi,Mm,Me; }student; int main(){ int n; scanf("%d",&n); student a[n]; int i; char name1[50]; for(i=0;i<n;i++){ printf("student %d: \n",i+1); printf("Name:"); fgets(name1,50,stdin); strcpy(a[i].name,name1); printf("Date of birth:"); scanf("%d%d%d",&a[i].d,&a[i].m,&a[i].y); printf("Mark of IT:"); scanf("%f",&a[i].Mi); printf("Mark of maths:"); scanf("%f",&a[i].Mm); printf("Mark of english:"); scanf("%f",&a[i].Me); } return 0; }

11th Dec 2021, 4:29 AM
Hảo Nguyễn Thiên
Hảo Nguyễn Thiên - avatar
16 Respostas
+ 3
Your array of student is not properly initialized. <n> is uninitialized (may contain garbage value), and thus the following lines doesn't make sense int n; // uninitialized student a[ n ];
11th Dec 2021, 5:00 AM
Ipang
+ 3
Now i fix code but still can't input name???
11th Dec 2021, 7:55 AM
Hảo Nguyễn Thiên
Hảo Nguyễn Thiên - avatar
11th Dec 2021, 8:30 AM
Hảo Nguyễn Thiên
Hảo Nguyễn Thiên - avatar
+ 3
you click my code link again, i've just changed it
11th Dec 2021, 12:35 PM
Hảo Nguyễn Thiên
Hảo Nguyễn Thiên - avatar
+ 2
i don't see you made any change in the code. Make the change so I can check whether you took my suggestion.
11th Dec 2021, 11:38 AM
Ipang
+ 2
How did you provide the inputs?
11th Dec 2021, 2:54 PM
Ipang
+ 1
Save your code bit and share its link here so I can see the modified version https://www.sololearn.com/post/75089/?ref=app
11th Dec 2021, 7:56 AM
Ipang
+ 1
your link is error
11th Dec 2021, 8:15 AM
Hảo Nguyễn Thiên
Hảo Nguyễn Thiên - avatar
+ 1
You must be using SoloLearn web, the link works only within SoloLearn app. * Save your code in SoloLearn. * Copy code link from browser's address bar. *Edit your post, and paste the code link in the Description.
11th Dec 2021, 8:18 AM
Ipang
+ 1
Line 13 Add space after %d specifier scanf( "%d ", &n ); Line 20: Read input directly to 'name' field fgets( a[i].name, 50, stdin ); Line 23: Add space after the last %d specifier scanf( "%d %d %d ", &a[i].d, &a[i].m, &a[i].y ); Line 25: Add space after %f specifier scanf( "%f ", &a[i].Mi ); Line 27: Add space after %f specifier scanf( "%f ", &a[i].Mm ); Line 29: Add space after %f specifier scanf( "%f ", &a[i].Me ); The space after a conversion specifier is meant to consume the line break character that are read after the input. The line break came from Enter key.
11th Dec 2021, 10:11 AM
Ipang
0
It do not work, still not printing name like"a b c d"
11th Dec 2021, 11:34 AM
Hảo Nguyễn Thiên
Hảo Nguyễn Thiên - avatar
0
Okay how you give input? I got no problem running in Code Playground with the following input 1 Student #1 12 34 56 1.234 2.345 3.456
11th Dec 2021, 12:53 PM
Ipang
0
having "fgets(a[i].name,50,stdin);" but code doesn't give me input of "name".
11th Dec 2021, 2:50 PM
Hảo Nguyễn Thiên
Hảo Nguyễn Thiên - avatar
0
I run the code with your input but output is: "student 1: Name:Date of birth:Mark of IT:Mark of maths:Mark of english:"
11th Dec 2021, 3:01 PM
Hảo Nguyễn Thiên
Hảo Nguyễn Thiên - avatar
0
you try this : 1 tom maguire 12 34 56 1.234 2.345 3.456
11th Dec 2021, 3:33 PM
Hảo Nguyễn Thiên
Hảo Nguyễn Thiên - avatar
0
I get the name, date of birth and marks correctly. The "student 1: Name:Date of birth:Mark of IT:Mark of maths:Mark of english:" are output from printf() calls inside the loop.
11th Dec 2021, 4:09 PM
Ipang