0

I have this functions , and i am trying to search in an array in a struct. Now I cant understand where my mistake is, but I believe that is, at the part where function is defined, where I try convert the player country into lowercase.( which in the file holds names of country). when I run the program, and I enter the country name the program just stops and crashes after I enter the name i want to search for.

Anyone can help me? Thank you .

#define NAME_LENGTH 50
#define NUM_PLAYERS 200

struct player_champ
{
    char player_country[NAME_LENGTH];
};

int search_player_by_country( struct player_champ  ptr_player[] , char asked_name[NAME_LENGTH], int lines_got);   
int main (void)    
{
    struct player_champ  player_info[NUM_PLAYERS] = { { 0 } };
    char asked_country[NAME_LENGTH]= {0};

    fflush(stdin);
    printf("\nEnter the name of the country you want to search for.\n\n>>>");
    fgets(asked_country, sizeof(asked_country)-1, stdin);
    asked_country[strlen(asked_country)-1] = '\0';
    search_player_by_country ( player_info, asked_country, num_lines_read);

    int search_player_by_country( struct player_champ ptr_player[] , char asked_country[NAME_LENGTH], int lines_got)
    {
        char temp_asked_country[NAME_LENGTH], temp_country_name[NAME_LENGTH];
        int i,k,z=0,j,counter=0;

        // there is a part of the code here that converts what user entered to lower case as well.

        for (i = 0 ; i < lines_got; i ++)
        {     
            k=0;

            /* while (ptr_player[i].player_country)
            {
                temp_country_name[j] = tolower (ptr_player.player_country);
                j++;
            }*/

            for (k = 0 ; k < lines_got; k ++)
            {
               temp_country_name[k] = tolower (ptr_player[k].player_country);
               k++;
            }
            temp_country_name[k] = '\0';

            if (strstr(temp_country_name, temp_asked_country) != NULL)
            {
               print_info( ptr_player[i]);
            }
        }
    }
16
  • fflush(stdin) doesn't do anything. Commented Jul 23, 2013 at 4:13
  • @Barmar, at this piece of code it doesn't, but in my program, I think it does because I call scanf and fgets multiply times. Commented Jul 23, 2013 at 4:15
  • 1
    fflush only has an effect on output streams, not input streams. Commented Jul 23, 2013 at 4:16
  • 1
    You didn't initialize ptr_player beyond setting it to empty in this code segment. Commented Jul 23, 2013 at 4:18
  • 1
    also you have nested function definition... mate that code needs house keeping. Commented Jul 23, 2013 at 4:23

1 Answer 1

3

This code is totally wrong:

    for (k = 0 ; k < lines_got; k ++)
    {
        temp_country_name[k] = tolower (ptr_player[k].player_country);
        k++;
    }
  1. You're incrementing k twice -- in the loop header and in the body.
  2. The target of the assignment is a single character in the temp_country_name string, but the parameter to tolower() is an entire string. Aren't you getting a warning from the compiler saying that the parameter to tolower() is the wrong type (it expects a char, you're giving it a char*)?
  3. You're already iterating over lines in the outer loop using i. This loop should just iterate over characters.

Try this:

    for (k = 0 ; ptr_player[i].player_country[k]; k ++)
    {
        temp_country_name[k] = tolower (ptr_player[i].player_country[k]);
    }

ptr_player[i] is the player from element i in the array. player_country[k] is character k in that string. So ptr_player[i].player_country[k] is the k'th character in the i'th player's country.

There are probably other problems in your code, I haven't tried to find them.

Sign up to request clarification or add additional context in comments.

6 Comments

passing argument 1 of 'tolower' makes integer from pointer without a cast [enabled by default]| this is the warning I get, Yeah you are right about that k++, but if you see there is a while loop commented out, I was trying to use a while loop before.
Would you mind explaining to me, what does:ptr_player[i].player_country[k] do in each places it occurs.
I added an explanation.
@user2512806 ptr_player[i] points to a player at an instance and ptr_player[i].player_country[k] will point to a players country char sequence.. assumiing you have a player user1 and he is from INDIA.. than ptr_player[0] will point to user1 and ptr_player[0].player_country[0] will point to first character of user1 's country.. (i.e) ptr_player[0].player_country[0] should point to ' I ', ptr_player[0].player_country[1] should point to ' N ' and it goes like this..
@AmarnathKrishnan Yeah, I do understand that part, but why it needs to be on the for loop, inside the parenthesis?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.