Complementary task for topic: 5

M Nemeth · 2023-08-29 15:21:04.622218'

Structs: Return struct from function

Structs: Return struct from function

Modify the program to add students to the array from user. The input handling should be done by a separate function.

Hint: The first idea is that in the function we need to create an array of students, and we need to return with that function. There are two capital mistakes in this:
-Firstly, the static array created in the stack and will be deleted after return, so even if we try to pass back it will be deleted. (pass by address you will see later, solution: Dynamic Arrays (later))
-We need to return the size as well!
Solution:
We need to define the static array outside the IO-function, in the main.
What we need is to handle the size with the array, and these are different data types we want to keep tight. For that we need another struct!
Let's see how it can be handled:

Solution
#include 

enum House{
    Griffindor,
    Huffelpuff,
    Ravenclaw,
    Slytherin
    };

typedef struct Student {
    char name[100];//Max. 99 chars for a name!
    int age;
    float magic_attack;
    float magic_defense, cleverness;
    enum House house; //it is an enum, must be defined before first use!
}Student;

typedef struct{
    Student Team[100]; //we will not allow more than 100 students!
    int size; //we keep the size here (that should be size_t!)
}Stud_Arr;

Stud_Arr fill_up(Stud_Arr Students){
Students.size=0; //set to zero
char c='y'; //This variable will responsible to terminate the read
int i=0;
char name[100];
while(c=='y'||c=='Y'){

printf("please give the name of the student:\n");
scanf("%99[^\n]", Students.Team[i].name);
printf("please give magic attack of the student:\n");
scanf("%d", &Students.Team[i].age);
printf("please give magic attack of the student:\n");
scanf("%f", &Students.Team[i].magic_attack);
printf("please give magic defense of the student:\n");
scanf("%f", &Students.Team[i].magic_defense);
printf("please give the inteligence of the student:\n");
scanf("%f", &Students.Team[i].cleverness);
printf("please give the house of the student 0-Griffindor, 1-Huffelpuff, 2-Ravenclaw, 3-Slytherin:\n");
scanf("%f", &Students.Team[i].house);
//This is a portable way to flush the input! Newline sticks!
int r;
while ((r = getchar()) != '\n' && r != EOF) { }
    printf("Do you want to add one more? y/n\n");
scanf("%c",&c);
//This is a portable way to flush the input! Newline sticks!
while ((r = getchar()) != '\n' && r != EOF) { }
Students.size++;
i++;
}
return Students;
}

float force(Student stud){
return stud.magic_defense+stud.magic_attack;
}

int best_student(Student Stud[], size_t size){//can be int size as well
int max_index=0;
int max_value=force(Stud[0]);//force must be declared earier!
for(int i=1;imax_value){
	max_index=i;
	max_value=force(Stud[i]);}
}
return max_index;
}




int main() {
Stud_Arr Friends;
Stud_Arr Foes;
printf("Add members to team one\n");
Friends=fill_up(Friends);
printf("%s",Friends.Team[0].name);
printf("Add members to team two\n");
Foes=fill_up(Foes);

int max_index=best_student(Foes.Team,Foes.size);
printf("%s",Foes.Team[0].name);
printf("In foes:%s with %.0f\n",Foes.Team[max_index].name,force(Foes.Team[max_index]));


max_index=best_student(Friends.Team,Friends.size);

printf("In Friends:%s with %.0f\n",Friends.Team[max_index].name,force(Friends.Team[max_index]));

return 0;
}



Explanation
As in the hint the new function is ready, but there is a funny part:
/This is a portable way to flush the input! Newline sticks!
int r;
while ((r = getchar()) != '\n' && r != EOF) { }
Why do we need it??
Because after the scanf() usage a \n is in the consol (keyboard buffer), that is instantly read by the scanf() and stored in the c character. That \n must be read out... That can be done by fflush(stdin), but it is unsafe to use this way. after the input of the character the \n also in the buffer that will give no character to the next name. It worth to try what happens without flushing the buffer. It is a very common and nasty problem.
< < previous    next > >