|
发表于 2022-6-25 21:19:38
|
显示全部楼层
本楼为最佳答案
strcmp函数不是这么用的,如果strcmp函数发现两个字符串一样的话会返回0的,而你却直接放到了if判断里,所以你输入Y当然会进else啦
详情请查看->https://fishc.com.cn/forum.php?m ... peid%26typeid%3D583
所以应当改成这样
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct Date{
- int year;
- int month;
- int day;
- };
- struct PersonInfo{
- char name[20];
- int age;
- char firstYesOrNot[2];
- struct Date firstDate;
- char secondYesOrNot[2];
- 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;
- }
复制代码 |
|