The same exercise I posted... we are required to add a new option in the menu to search a name and print the name that I have stored previously. I failed to do so. This is what I have up to now:
Code:
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("No data found\n");
}
int main()
{
struct players data[10];
int n;
char choice;
char *name;
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 E to exit.\n");
printf("\nSelect an option (A-D), E 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;
}
}
while (choice!='E');
return 0;
}
I have a warning: |94|warning: 'name' may be used uninitialized in this function [-Wmaybe-uninitialized]|
The result is the most annoying one: crash when I try to search a name. What I am doing wrong? I used strcmp to compare strings, I have string.h library. I posted only the section where I have problems. The code also has structure and other functions to read/store the data and print names, ages, heights of hypothetical players. If you need the full code I can write it.
Thank you in advance.