鱼C论坛

 找回密码
 立即注册
查看: 2464|回复: 5

[已解决]c语言S1E44,疫苗接种系统课后作业求助_已解决

[复制链接]
发表于 2022-6-25 19:55:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 涛4091 于 2023-2-23 11:03 编辑

屏幕截图 2022-06-25 195118.png
你好,这里debug是等于Y但是判断不了呢,为什么
最佳答案
2022-6-25 21:19:38

strcmp函数不是这么用的,如果strcmp函数发现两个字符串一样的话会返回0的,而你却直接放到了if判断里,所以你输入Y当然会进else啦
详情请查看->https://fishc.com.cn/forum.php?m ... peid%26typeid%3D583
所以应当改成这样
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct Date{
  5.         int year;
  6.         int month;
  7.         int day;
  8. };
  9. struct PersonInfo{
  10.         char name[20];
  11.         int age;
  12.         char firstYesOrNot[2];
  13.         struct Date firstDate;
  14.         char secondYesOrNot[2];
  15.         struct Date secondDate;
  16. };
  17. void inputInfo(struct PersonInfo *personInfo){
  18.         printf("请问姓名是:");        scanf("%s",personInfo->name);
  19.         printf("请问年龄是:");scanf("%d",&personInfo->age);
  20.         printf("请问是否接种过疫苗(Y/N):");
  21.         scanf("%s",personInfo->firstYesOrNot);
  22.         if(!strcmp(personInfo->firstYesOrNot,"Y")){
  23.                 printf("请出入第一针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->firstDate.year,&personInfo->firstDate.month,&personInfo->firstDate.day);
  24.                 printf("请问是否接种第二针疫苗(Y/N):");gets(personInfo->secondYesOrNot);
  25.                 if(!strcmp(personInfo->secondYesOrNot,"Y")){
  26.                         printf("请出入第二针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->secondDate.year,&personInfo->secondDate.month,&personInfo->secondDate.day);
  27.                 }else if(!strcmp(personInfo->secondYesOrNot,"N")){
  28.                         printf("请尽快接种第二针疫苗!");
  29.                         exit(1);
  30.                 }
  31.         }else {
  32.                 printf("请尽快接种过疫苗!");
  33.                 exit(1);
  34.         }
  35. }
  36. void printInfo(struct PersonInfo *personInfo){
  37.         printf("姓名:%s,年龄:%d \n",personInfo->name,personInfo->age);
  38.         if(!strcmp(personInfo->firstYesOrNot,"N")){
  39.                 printf("未接种疫苗!");
  40.         }else if(!strcmp(personInfo->firstYesOrNot,"Y")){
  41.                 printf("第一针疫苗接种日期:%d-%d-%d \n",personInfo->firstDate.year,personInfo->firstDate.month,personInfo->firstDate.day);
  42.                 if(!strcmp(personInfo->secondYesOrNot,"N")){
  43.                         printf("未接种第二针疫苗!");
  44.                 }else if(!strcmp(personInfo->secondYesOrNot,"Y")){
  45.                         printf("第二针疫苗接种日期:%d-%d-%d",personInfo->secondDate.year,personInfo->secondDate.month,personInfo->secondDate.day);
  46.                 }
  47.         }
  48. }
  49. int main(void){
  50.         struct PersonInfo person1,person2,person3;
  51.         inputInfo(&person1);
  52.         inputInfo(&person2);
  53.         inputInfo(&person3);
  54.         printInfo(&person1);
  55.         printInfo(&person2);
  56.         printInfo(&person3);
  57.         return 0;
  58. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-6-25 20:11:56 | 显示全部楼层
不要用==去判断字符串是否相等
你可以用strcmp函数,需要在使用之前#include <string.h>来引用string.h头文件
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-25 21:03:26 | 显示全部楼层
临时号 发表于 2022-6-25 20:11
不要用==去判断字符串是否相等
你可以用strcmp函数,需要在使用之前#include 来引用string.h头文件

不行,用了strcmp也不行
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-25 21:06:11 | 显示全部楼层
涛4091 发表于 2022-6-25 21:03
不行,用了strcmp也不行

把完整代码发出来,我帮你看看
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-6-25 21:09:57 | 显示全部楼层
临时号 发表于 2022-6-25 21:06
把完整代码发出来,我帮你看看
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct Date{
  5.         int year;
  6.         int month;
  7.         int day;
  8. };
  9. struct PersonInfo{
  10.         char name[20];
  11.         int age;
  12.         char firstYesOrNot[2];
  13.         struct Date firstDate;
  14.         char secondYesOrNot[2];
  15.         struct Date secondDate;
  16. };
  17. void inputInfo(struct PersonInfo *personInfo){
  18.         printf("请问姓名是:");        scanf("%s",personInfo->name);
  19.         printf("请问年龄是:");scanf("%d",&personInfo->age);
  20.         printf("请问是否接种过疫苗(Y/N):");
  21.         scanf("%s",personInfo->firstYesOrNot);
  22.         if(strcmp(personInfo->firstYesOrNot,"Y")){
  23.                 printf("请出入第一针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->firstDate.year,&personInfo->firstDate.month,&personInfo->firstDate.day);
  24.                 printf("请问是否接种第二针疫苗(Y/N):");gets(personInfo->secondYesOrNot);
  25.                 if(strcmp(personInfo->secondYesOrNot,"Y")){
  26.                         printf("请出入第二针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->secondDate.year,&personInfo->secondDate.month,&personInfo->secondDate.day);
  27.                 }else if(strcmp(personInfo->secondYesOrNot,"N")){
  28.                         printf("请尽快接种第二针疫苗!");
  29.                         exit(1);
  30.                 }
  31.         }else {
  32.                 printf("请尽快接种过疫苗!");
  33.                 exit(1);
  34.         }
  35. }
  36. void printInfo(struct PersonInfo *personInfo){
  37.         printf("姓名:%s,年龄:%d \n",personInfo->name,personInfo->age);
  38.         if(strcmp(personInfo->firstYesOrNot,"N")){
  39.                 printf("未接种疫苗!");
  40.         }else if(strcmp(personInfo->firstYesOrNot,"Y")){
  41.                 printf("第一针疫苗接种日期:%d-%d-%d \n",personInfo->firstDate.year,personInfo->firstDate.month,personInfo->firstDate.day);
  42.                 if(strcmp(personInfo->secondYesOrNot,"N")){
  43.                         printf("未接种第二针疫苗!");
  44.                 }else if(strcmp(personInfo->secondYesOrNot,"Y")){
  45.                         printf("第二针疫苗接种日期:%d-%d-%d",personInfo->secondDate.year,personInfo->secondDate.month,personInfo->secondDate.day);
  46.                 }
  47.         }
  48. }
  49. int main(void){
  50.         struct PersonInfo person1,person2,person3;
  51.         inputInfo(&person1);
  52.         inputInfo(&person2);
  53.         inputInfo(&person3);
  54.         printInfo(&person1);
  55.         printInfo(&person2);
  56.         printInfo(&person3);
  57.         return 0;
  58. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-6-25 21:19:38 | 显示全部楼层    本楼为最佳答案   

strcmp函数不是这么用的,如果strcmp函数发现两个字符串一样的话会返回0的,而你却直接放到了if判断里,所以你输入Y当然会进else啦
详情请查看->https://fishc.com.cn/forum.php?m ... peid%26typeid%3D583
所以应当改成这样
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. struct Date{
  5.         int year;
  6.         int month;
  7.         int day;
  8. };
  9. struct PersonInfo{
  10.         char name[20];
  11.         int age;
  12.         char firstYesOrNot[2];
  13.         struct Date firstDate;
  14.         char secondYesOrNot[2];
  15.         struct Date secondDate;
  16. };
  17. void inputInfo(struct PersonInfo *personInfo){
  18.         printf("请问姓名是:");        scanf("%s",personInfo->name);
  19.         printf("请问年龄是:");scanf("%d",&personInfo->age);
  20.         printf("请问是否接种过疫苗(Y/N):");
  21.         scanf("%s",personInfo->firstYesOrNot);
  22.         if(!strcmp(personInfo->firstYesOrNot,"Y")){
  23.                 printf("请出入第一针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->firstDate.year,&personInfo->firstDate.month,&personInfo->firstDate.day);
  24.                 printf("请问是否接种第二针疫苗(Y/N):");gets(personInfo->secondYesOrNot);
  25.                 if(!strcmp(personInfo->secondYesOrNot,"Y")){
  26.                         printf("请出入第二针疫苗接种的日期(yyyy-mm-dd):");scanf("%d-%d-%d",&personInfo->secondDate.year,&personInfo->secondDate.month,&personInfo->secondDate.day);
  27.                 }else if(!strcmp(personInfo->secondYesOrNot,"N")){
  28.                         printf("请尽快接种第二针疫苗!");
  29.                         exit(1);
  30.                 }
  31.         }else {
  32.                 printf("请尽快接种过疫苗!");
  33.                 exit(1);
  34.         }
  35. }
  36. void printInfo(struct PersonInfo *personInfo){
  37.         printf("姓名:%s,年龄:%d \n",personInfo->name,personInfo->age);
  38.         if(!strcmp(personInfo->firstYesOrNot,"N")){
  39.                 printf("未接种疫苗!");
  40.         }else if(!strcmp(personInfo->firstYesOrNot,"Y")){
  41.                 printf("第一针疫苗接种日期:%d-%d-%d \n",personInfo->firstDate.year,personInfo->firstDate.month,personInfo->firstDate.day);
  42.                 if(!strcmp(personInfo->secondYesOrNot,"N")){
  43.                         printf("未接种第二针疫苗!");
  44.                 }else if(!strcmp(personInfo->secondYesOrNot,"Y")){
  45.                         printf("第二针疫苗接种日期:%d-%d-%d",personInfo->secondDate.year,personInfo->secondDate.month,personInfo->secondDate.day);
  46.                 }
  47.         }
  48. }
  49. int main(void){
  50.         struct PersonInfo person1,person2,person3;
  51.         inputInfo(&person1);
  52.         inputInfo(&person2);
  53.         inputInfo(&person3);
  54.         printInfo(&person1);
  55.         printInfo(&person2);
  56.         printInfo(&person3);
  57.         return 0;
  58. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-17 09:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表