# Thread: My excercise of structures... Need help to fix a function to search names

1. ## My excercise of structures... Need help to fix a function to search names

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);
do {
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.

2. Forum rule: Don't create a new thread for the same problem. For everyone's sake.

Maybe if this was Javascript, you'd get a "0" instead of a SEGFAULT.

3. Originally Posted by MacNilly
Forum rule: Don't create a new thread for the same problem. For everyone's sake.
I'm sorry MacNilly, I thought it was a different issue. How can I remove this thread and merge with the previous one?

4. You can't now. Moderators will fix it, maybe...

5. You're error (warning) seems to appear on a line (94) that you haven't posted.

6. Originally Posted by MacNilly
You're error (warning) seems to appear on a line (94) that you haven't posted.
Because I have only put a section of my code. This is my entire 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 ;
}
}
}

int main()
{
struct players data[10];
int n;
char choice;
char *name;

printf("Insert the number of registrations: ");
scanf("%d", &n);
do {
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;
}```
The error is in line 99 here because I may added some spaces while copying my code.

scanf("%s", name);

7. Your 'name' in main() needs to be say char name[100];, so it allocates space to store what you type.

8. Originally Posted by Salem
Your 'name' in main() needs to be say char name[100];, so it allocates space to store what you type.
I'm stupid! I forgot that. Hopefully Jim is not here... Fixed and works excellent!.

You guys are amazing and saved me tons of hours. By the way when I am over 4 hours in my laptop doing my exercises I got headache and get tired. Is that issue normal among programmers?

9. > By the way when I am over 4 hours in my laptop doing my exercises I got headache and get tired. Is that issue normal among programmers?
You need to take 10 mins every 60 to 90 minutes just relaxing, and say 30 minutes break every 4 hours or so.

Or whenever you sense your 'chi' isn't flowing as it should.

10. Chi - Wikipedia
Ch'i or qi (氣), "energy force" in Chinese culture

11. 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 ;
}
}
}

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);
do {
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.

12. Look at this snippet:
Code:
```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);```
First using fflush() with an input stream is not defined in the C standard, you should only use fflush() with a stream that is in output mode.

Next and probably much much more important. Never, NEVER, NEVER use a function that doesn't limit the number of characters to extract into a string. The gets() function can never be used safely and has actually been removed from the current C standard. By the way why aren't you using the current C standard? You should start using fgets() instead of gets() and remember when using scanf() you should also use the correct optional width specifier to limit the number of characters it will try to retrieve.

Jim

13. Originally Posted by jimblumberg
Look at this snippet:
Code:
```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);```
First using fflush() with an input stream is not defined in the C standard, you should only use fflush() with a stream that is in output mode.

Next and probably much much more important. Never, NEVER, NEVER use a function that doesn't limit the number of characters to extract into a string. The gets() function can never be used safely and has actually been removed from the current C standard. By the way why aren't you using the current C standard? You should start using fgets() instead of gets() and remember when using scanf() you should also use the correct optional width specifier to limit the number of characters it will try to retrieve.

Jim
That's ok, I will never use gets again but I only know scanf and gets so from now I will always use scanf instead of gets.

I fixed the exercise by myself. The problem was that heights are in international system units, for example: 1.78 m and what is more important, I declared heights with float in the structure. So I changed int for float in findMax function and now works fine. Problem solved.