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

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    101

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

  2. #2
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    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. #3
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by MacNilly View Post
    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. #4
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    You can't now. Moderators will fix it, maybe...

  5. #5
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    You're error (warning) seems to appear on a line (94) that you haven't posted.

  6. #6
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by MacNilly View Post
    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 ;
            }
        }
        printf("Data not 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;
    }
    The error is in line 99 here because I may added some spaces while copying my code.

    scanf("%s", name);
    Last edited by JorgeChemE; 05-20-2017 at 05:10 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    Your 'name' in main() needs to be say char name[100];, so it allocates space to store what you type.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by Salem View Post
    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. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,163
    Chi - Wikipedia
    Ch'i or qi (氣), "energy force" in Chinese culture
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    May 2017
    Posts
    101
    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.
    Last edited by JorgeChemE; 05-20-2017 at 08:45 AM.

  12. #12
    Registered User
    Join Date
    May 2010
    Posts
    4,552
    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. #13
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by jimblumberg View Post
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lyric search program - search function question
    By Jpaul8986 in forum C++ Programming
    Replies: 9
    Last Post: 07-28-2015, 11:56 AM
  2. Function pointers syntax and excercise.
    By Iceboxes in forum C++ Programming
    Replies: 5
    Last Post: 02-13-2014, 05:41 PM
  3. Data structures --binary search tree
    By shikhardeep in forum C Programming
    Replies: 3
    Last Post: 09-20-2011, 04:34 PM
  4. Allowing my search function to search sub directories!
    By Queatrix in forum Windows Programming
    Replies: 10
    Last Post: 09-30-2005, 04:54 PM
  5. Replies: 5
    Last Post: 04-11-2002, 11:29 AM

Tags for this Thread