+ 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; }
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 ];
+ 3
Now i fix code but still can't input name???
+ 3
My code link:
https://code.sololearn.com/c5ZAxnQTeXSR
+ 3
you click my code link again, i've just changed it
+ 2
i don't see you made any change in the code. Make the change so I can check whether you took my suggestion.
+ 2
How did you provide the inputs?
+ 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
+ 1
your link is error
+ 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.
+ 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.
0
It do not work, still not printing name like"a b c d"
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
0
having "fgets(a[i].name,50,stdin);" but code doesn't give me input of "name".
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:"
0
you try this :
1
tom maguire
12 34 56
1.234
2.345
3.456
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.