Pouyan - 5 months ago 28
C Question

# Find the greatest average among a student list using structure

The problem is to find the greatest average of a group of students. As you know Structure works here and to my point of view it's a good idea (Or maybe) that first make that structure using Array of Structure because I have for example 30 students. Then searching through the members of that structure and find the ultimate answer.

But I've encountered a problem which I can't fill the

`struct Stdinfo student[stdnum]`
and my
`for`
loop actually doesn't work correctly and I don't know why!

As a check I used
`printf()`
to print one of the members but I couldn't.

Here is my code :

``````#include <stdio.h>

struct Stdinfo
{
char name[30];
int score;
};

struct Stdinfo function();

int main(int argc, char **argv)
{
//Number of students ~stdnum
int stdnum, i;
puts("Input numbers of student(s) :");
scanf("%i", &stdnum);
stdnum--;

struct Stdinfo student[stdnum];

//Filling array of structure
for (i = 0; i < stdnum; i++)
{
student[i] = function();
}

return 0;
}

struct Stdinfo function()
{
struct Stdinfo student;
puts("Input the name of the student : ");
fgets(student.name, sizeof(student.name), stdin);
puts("Input his(her) score:");
scanf("%i", &student.score);
return student;
}
``````

Now searching is not my main problem and I appreciate any help by which I can solve "Structure's members filing" problem.

You decrease the size of the array incorrectly. When doing so, you ignore the last student. Discard that decrement.

Now think how the input comes. The user enters the number of students and then his Enter!

`scanf("%i" ,&stdnum);` will consume the number, but not the newline (from Enter).

As a result, `fgets()` will read that newline and stop there. In others words, it thinks that this is what the input actually is. To alleviate this problem, simply consume this newline, e.g. with `getchar()`, before `fgets()` gets called.

``````#include <stdio.h>
#include <string.h>

struct Stdinfo{
char name[30];
int score;
};

struct Stdinfo function() ;

int main(void)
{
//Number of students ~stdnum
int stdnum , i;
puts("Input numbers of student(s) :") ;
scanf("%i" ,&stdnum);
// Do not do that, it makes your program ignore the last student
//stdnum--;

struct Stdinfo student[stdnum]  ;

//Filling array of structure
for(i=0 ; i < stdnum ; i++)
{
student[i] = function() ;
}
for(i=0 ; i < stdnum ; i++)
{
printf("%s\n", student[i].name);
printf("%d\n", student[i].score);
}

return 0;
}

struct Stdinfo function() {

struct Stdinfo student;

getchar(); // Consume newline from previous input

puts("Input the name of the student : ") ;
fgets(student.name , sizeof (student.name) , stdin);
// remove trailing newline from 'fgets()'
student.name[strcspn(student.name, "\n")] = 0;

puts("Input his(her) score:") ;
scanf("%i" ,&student.score) ;

return student ;
}
``````

PS: You may want to Removing trailing newline character from fgets() input (my example does this).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download