+ 1
Why my function isnt work properly
#include<stdio.h> int display(); struct student { char name[50]; int roll; }s[50]; int main() { int i,n; printf("Enter The Information of Students :\n\n"); printf("enter total number of students:\n"); scanf("%d",&n); for(i=0;i<n;i++) { printf("\nRoll No. : "); scanf("%d",&(s[i].roll)); printf("Name : "); scanf("%s",s[i].name); display(); } } int display() { int i,n; // int y=n; for(i=0;i<n;i++) { printf("\nRoll No. : %d",s[i].roll); printf("\nName : %s",s[i].name); printf("\n\n"); } }
4 Réponses
+ 12
narthana wickramasinghe
In your code function display is use for displaying details of students .
And display() function call is in between the loop where we enter the details which is wrong
(Means suppose if you enter details of three students first we input detail of 1 and display will call which shows detail of 1st student and remaining student 2,3 become = 0.
Then you input detail of second again display will call which show details of 1st and 2nd , and the remaining 3rd one is 0 )
Corrections -
1 - put display() out of loop
2 - we have to pass n to display no. of students.
If you don't want to pass n then you have to declare n as static so value of n won't change throughout the program
Here is your code I've declared n as static
https://code.sololearn.com/cmY516ek6rny/?ref=app
+ 1
looks like n dont have a value in display(). you can pass n as an argument
+ 1
You have declared function with int return type so you have to call function with an int variable and you used variable n in function without assigning any value.