c语言S1E44,疫苗接种系统课后作业求助_已解决
本帖最后由 涛4091 于 2023-2-23 11:03 编辑你好,这里debug是等于Y但是判断不了呢,为什么 不要用==去判断字符串是否相等
你可以用strcmp函数,需要在使用之前#include <string.h>来引用string.h头文件 临时号 发表于 2022-6-25 20:11
不要用==去判断字符串是否相等
你可以用strcmp函数,需要在使用之前#include 来引用string.h头文件
不行,用了strcmp也不行 涛4091 发表于 2022-6-25 21:03
不行,用了strcmp也不行
把完整代码发出来,我帮你看看 临时号 发表于 2022-6-25 21:06
把完整代码发出来,我帮你看看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Date{
int year;
int month;
int day;
};
struct PersonInfo{
char name;
int age;
char firstYesOrNot;
struct Date firstDate;
char secondYesOrNot;
struct Date secondDate;
};
void inputInfo(struct PersonInfo *personInfo){
printf("请问姓名是:"); scanf("%s",personInfo->name);
printf("请问年龄是:");scanf("%d",&personInfo->age);
printf("请问是否接种过疫苗(Y/N):");
scanf("%s",personInfo->firstYesOrNot);
if(strcmp(personInfo->firstYesOrNot,"Y")){
printf("请出入第一针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->firstDate.year,&personInfo->firstDate.month,&personInfo->firstDate.day);
printf("请问是否接种第二针疫苗(Y/N):");gets(personInfo->secondYesOrNot);
if(strcmp(personInfo->secondYesOrNot,"Y")){
printf("请出入第二针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->secondDate.year,&personInfo->secondDate.month,&personInfo->secondDate.day);
}else if(strcmp(personInfo->secondYesOrNot,"N")){
printf("请尽快接种第二针疫苗!");
exit(1);
}
}else {
printf("请尽快接种过疫苗!");
exit(1);
}
}
void printInfo(struct PersonInfo *personInfo){
printf("姓名:%s,年龄:%d \n",personInfo->name,personInfo->age);
if(strcmp(personInfo->firstYesOrNot,"N")){
printf("未接种疫苗!");
}else if(strcmp(personInfo->firstYesOrNot,"Y")){
printf("第一针疫苗接种日期:%d-%d-%d \n",personInfo->firstDate.year,personInfo->firstDate.month,personInfo->firstDate.day);
if(strcmp(personInfo->secondYesOrNot,"N")){
printf("未接种第二针疫苗!");
}else if(strcmp(personInfo->secondYesOrNot,"Y")){
printf("第二针疫苗接种日期:%d-%d-%d",personInfo->secondDate.year,personInfo->secondDate.month,personInfo->secondDate.day);
}
}
}
int main(void){
struct PersonInfo person1,person2,person3;
inputInfo(&person1);
inputInfo(&person2);
inputInfo(&person3);
printInfo(&person1);
printInfo(&person2);
printInfo(&person3);
return 0;
} 涛4091 发表于 2022-6-25 21:09
strcmp函数不是这么用的,如果strcmp函数发现两个字符串一样的话会返回0的,而你却直接放到了if判断里,所以你输入Y当然会进else啦
详情请查看->https://fishc.com.cn/forum.php?mod=viewthread&tid=70567&extra=page%3D1%26filter%3Dtypeid%26typeid%3D583
所以应当改成这样
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Date{
int year;
int month;
int day;
};
struct PersonInfo{
char name;
int age;
char firstYesOrNot;
struct Date firstDate;
char secondYesOrNot;
struct Date secondDate;
};
void inputInfo(struct PersonInfo *personInfo){
printf("请问姓名是:"); scanf("%s",personInfo->name);
printf("请问年龄是:");scanf("%d",&personInfo->age);
printf("请问是否接种过疫苗(Y/N):");
scanf("%s",personInfo->firstYesOrNot);
if(!strcmp(personInfo->firstYesOrNot,"Y")){
printf("请出入第一针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->firstDate.year,&personInfo->firstDate.month,&personInfo->firstDate.day);
printf("请问是否接种第二针疫苗(Y/N):");gets(personInfo->secondYesOrNot);
if(!strcmp(personInfo->secondYesOrNot,"Y")){
printf("请出入第二针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->secondDate.year,&personInfo->secondDate.month,&personInfo->secondDate.day);
}else if(!strcmp(personInfo->secondYesOrNot,"N")){
printf("请尽快接种第二针疫苗!");
exit(1);
}
}else {
printf("请尽快接种过疫苗!");
exit(1);
}
}
void printInfo(struct PersonInfo *personInfo){
printf("姓名:%s,年龄:%d \n",personInfo->name,personInfo->age);
if(!strcmp(personInfo->firstYesOrNot,"N")){
printf("未接种疫苗!");
}else if(!strcmp(personInfo->firstYesOrNot,"Y")){
printf("第一针疫苗接种日期:%d-%d-%d \n",personInfo->firstDate.year,personInfo->firstDate.month,personInfo->firstDate.day);
if(!strcmp(personInfo->secondYesOrNot,"N")){
printf("未接种第二针疫苗!");
}else if(!strcmp(personInfo->secondYesOrNot,"Y")){
printf("第二针疫苗接种日期:%d-%d-%d",personInfo->secondDate.year,personInfo->secondDate.month,personInfo->secondDate.day);
}
}
}
int main(void){
struct PersonInfo person1,person2,person3;
inputInfo(&person1);
inputInfo(&person2);
inputInfo(&person3);
printInfo(&person1);
printInfo(&person2);
printInfo(&person3);
return 0;
}
页:
[1]