|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- #include <stdlib.h>
- struct Ym
- {
- char name[8];
- int age;
- char one_time[11];
- char two_time[11];
- };
- struct Ym* input(struct Ym *pt,int j);
- struct Ym* input(struct Ym *pt,int j)
- {
- char ch;
- printf("请问姓名是:");
- scanf("%s",pt[j].name);
- printf("请问年龄是:");
- scanf("%d",&pt[j].age);
- printf("请问是否接种过疫苗(Y/N):");
- getchar();
-
- if((ch = getchar()) == 'Y')
- {
- printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
- scanf("%s",pt[j].one_time);
-
- }
- else
- {
- pt[j].one_time[0] = '0';
- return;
- }
- printf("请问是否接种过第二针疫苗(Y/N):");
- getchar();
-
- if((ch = getchar()) == 'Y')
- {
-
- printf("请输入第二针疫苗接种的日期(yyyy-mm-dd):");
- scanf("%s",pt[j].two_time);
- }
- else
- pt[j].two_time[0] = '0';
-
- printf("\n\n");
- return;
- }
- int main(void)
- {
- struct Ym *pt[255];
- int i = 0;
- char j,ch;
- printf("请问是否要录入疫苗登记(Y/N):");
-
- while(1)
- {
- ch = getchar();
-
- if(ch == 'Y')
- {
- pt[i] = (struct Ym *)malloc(sizeof(struct Ym));
- input(*pt,i);
- i++;
- getchar();
- }
- else
- break;
- printf("当前已经录入%d人疫苗档案\n请问是否要继续录入疫苗登记(Y/N):",i);
- }
-
- printf("====录入数据完毕(总共%d人如下)====\n\n",i);
- int k;
- for(k = 0; k < i; k++)
- {
- printf("姓名:%s",pt[k]->name);
- printf("年龄:%d\n",pt[k]->age);
- if(pt[k]->one_time[0] != '0')
- printf("第一针疫苗接种日期:%s,",pt[k]->one_time);
- else
- {
- printf("未接种疫苗!");
- continue;
- }
- if(pt[k]->two_time[0] != '0')
- printf("第二针疫苗接种日期:%s\n\n",pt[k]->two_time);
- else
- printf("未接种第二针疫苗!\n\n");
- }
- return 0;
- }
复制代码
首先,我注意到您的 input 函数的原型和定义中包含了返回值类型 struct Ym*,但是在函数内部没有返回任何值。
为了修复这个问题,您应该将这个函数的返回值类型更改为 void,因为您实际上没有返回任何值。
下面是修复后的函数原型和定义:
- void input(struct Ym *pt,int j);
- void input(struct Ym *pt,int j)
- {
- ...
- }
复制代码
接下来,关于乱码的问题,我注意到您使用了一个指针数组 struct Ym *pt[255]。
但在调用 input 函数时,您使用了 input(*pt, i)。这里应该使用 input(pt[i], i) 而不是 input(*pt, i)。
这是因为 input 函数的第一个参数是一个指向 struct Ym 的指针,而 *pt 会解引用第一个元素,从而导致未定义的行为。
以下是修复后的 main 函数中的部分代码:
- while(1)
- {
- ch = getchar();
-
- if(ch == 'Y')
- {
- pt[i] = (struct Ym *)malloc(sizeof(struct Ym));
- input(pt[i], i); // 修复这里的调用
- i++;
- getchar();
- }
- else
- break;
- printf("当前已经录入%d人疫苗档案\n请问是否要继续录入疫苗登记(Y/N):",i);
- }
复制代码
按照这些更改后,程序应该正常运行,不会出现乱码问题。如果还有其他问题,请随时告诉我。
|
|