比较字符时没有正确的输出
Don't have right output while comparing chars
#include <stdio.h>
#include <stdlib.h>
struct patients{
char last_name[15];
int passport_number;
char disease[30];
char doctors_last_name[15];
};
int main (){
int n,i;
char enter_doctors_last_name [15];
struct patients mas_struct[3]={{"Ivanov",5457401,"COVID-18","Davis"},{"Petrov",2864228,"COVID-19","Davis"},{"Petrova",63863380,"COVID-19","Dixon"}};
printf("\nPatients:");
printf("\n Last name | Passport number | \tDisease | Doctor's last name ");
for (i=0;i<3;i++)
printf("\n %s \t%d \t%s \t%s",mas_struct[i].last_name,mas_struct[i].passport_number,mas_struct[i].disease,mas_struct[i].doctors_last_name);
printf("\n");
printf("\nEnter doctor's last name:");
scanf("%s", enter_doctors_last_name);
printf("\nPatients:");
for (i=0;i<3;i++)
if(mas_struct[i].doctors_last_name == enter_doctors_last_name)
printf("\n %s \t%d \t%s \t%s",mas_struct[i].last_name,mas_struct[i].passport_number,mas_struct[i].disease,mas_struct[i].doctors_last_name);
return 0;
}
在比较字符时遇到问题,它不起作用,我无法用谷歌搜索找到合适的词。
在最后几行 我键入 "Davis" 或 "Dixon" for enter_doctors_last_name
输出只是患者
我也试过用gets函数
您不能使用 ==
运算符比较字符串,请改用 strcmp
:
if (!strcmp(mas_struct[i].doctors_last_name,enter_doctors_last_name)){/*...*/}
scanf
和 "%s"
说明符是非常不安全的,使用 "%14s"
代替,-1 字符是为空终止符保留 space。
如果您需要超过 1 个单词的名称,您应该使用 "%14[^\n]"
,读取所有内容,直到找到换行符。
您应该使用 strcmp()
函数来比较字符串。
#include <stdio.h>
#include <stdlib.h>
struct patients{
char last_name[15];
int passport_number;
char disease[30];
char doctors_last_name[15];
};
int main (){
int n,i;
char enter_doctors_last_name [15];
struct patients mas_struct[3]={{"Ivanov",5457401,"COVID-18","Davis"},{"Petrov",2864228,"COVID-19","Davis"},{"Petrova",63863380,"COVID-19","Dixon"}};
printf("\nPatients:");
printf("\n Last name | Passport number | \tDisease | Doctor's last name ");
for (i=0;i<3;i++)
printf("\n %s \t%d \t%s \t%s",mas_struct[i].last_name,mas_struct[i].passport_number,mas_struct[i].disease,mas_struct[i].doctors_last_name);
printf("\n");
printf("\nEnter doctor's last name:");
scanf("%s", enter_doctors_last_name);
printf("\nPatients:");
for (i=0;i<3;i++)
if(mas_struct[i].doctors_last_name == enter_doctors_last_name)
printf("\n %s \t%d \t%s \t%s",mas_struct[i].last_name,mas_struct[i].passport_number,mas_struct[i].disease,mas_struct[i].doctors_last_name);
return 0;
}
在比较字符时遇到问题,它不起作用,我无法用谷歌搜索找到合适的词。
在最后几行 我键入 "Davis" 或 "Dixon" for enter_doctors_last_name
输出只是患者
我也试过用gets函数
您不能使用 ==
运算符比较字符串,请改用 strcmp
:
if (!strcmp(mas_struct[i].doctors_last_name,enter_doctors_last_name)){/*...*/}
scanf
和 "%s"
说明符是非常不安全的,使用 "%14s"
代替,-1 字符是为空终止符保留 space。
如果您需要超过 1 个单词的名称,您应该使用 "%14[^\n]"
,读取所有内容,直到找到换行符。
您应该使用 strcmp()
函数来比较字符串。