Thank you for your recommendations.
The last thing I have to do with the same program: add an option to print the age and the name of the tallest player on the list.
My code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct players
{
char names[100];
float heights;
int ages;
};
void read_players(struct players list[100], int s) {
for (int i=0; i<s; i++) {
printf("\nInsert the data of the players %d", i+1);
fflush(stdin);
printf("\nInsert the name of the player : ");
gets(list[i].names);
printf("Insert the height of the player : ");
scanf("%f", &list[i].heights);
printf("Insert the age of the player : ");
scanf("%d", &list[i].ages);
}
}
void print_names(struct players list[100], int s) {
printf("players\n");
for (int i=0; i<s; i++) {
printf("%s\n", list[i].players);
}
}
void print_heights(struct players list[100], int s) {
printf("heights\n");
for (int i=0; i<s; i++) {
printf("%.2f\n", list[i].heights);
}
}
void print_ages(struct players list[100], int s) {
printf("ages\n");
for (int i=0; i<s; i++) {
printf("%d\n", list[i].ages);
}
}
void search(struct players list[100], int s, char *name) {
for (int i=0; i<s; i++) {
if (strcmp(list[i].names,name)==0) {
printf("Names : Heights : %.2f\nAges : %d", list[i].heights, list[i].ages);
return ;
}
}
printf("Data not found\n");
}
int findMax(struct players list[100], int s) {
int max=list[0].heights;
for (int i=1; i<s; i++) {
if (list[i].heights>max) {
max=list[i].heights;
}
}
return max;
}
void top_height(struct players list[100], int s) {
for (int i=0; i<s; i++) {
if (list[i].heights==findMax(list, s)) {
printf("Height: %.2f\nAge: %d\nName: %s", list[i].heights, list[i].ages, list[i].names);
}
}
}
int main()
{
struct players data[10];
int n;
char choice;
char name[100];
printf("Insert the number of registrations: ");
scanf("%d", &n);
read_players(data, n);
do {
printf("\nMenu :\n");
printf("Press A to print names.\n");
printf("Press B to print heights.\n");
printf("Press C to print ages.\n");
printf("Press D to search a name in the registry. \n");
printf("Press S to exit.\n");
printf("\nSelect an option (A-E), S to exit: ");
scanf(" %c", &choice);
switch (choice)
{
case 'A':
print_names(data,n);
break;
case 'B':
print_heights(data,n);
break;
case 'C':
print_ages(data,n);
break;
case 'D':
printf("Insert the name to search : ");
scanf("%s", name);
search(data, n, name);
break;
case 'E':
top_height(data,n);
break;
}
}
while (choice!='S');
return 0;
}
I added two functions: findMax and top_height to find the tallest player and another option case 'E'. This time the program runs but when I choose E nothing happens, the program prints nothing. What's wrong?
Edited: I had E to exit because I translated the code from spanish, I changed here for S that is what I have in my code.