我有这个函数,我正在尝试在结构中的数组中搜索。现在我无法理解我的错误在哪里,但我相信在定义函数的部分,我尝试将播放器国家/地区转换为小写。(文件中包含国家/地区名称)。当我运行程序并输入国家名称时,程序在我输入要搜索的名称后停止并崩溃。
任何人都可以帮助我吗?谢谢你 。
#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]);
}
}
}