程序录入的第一个人之后的资料不对 乱码了 什么问题呢?求解答 谢谢!
#include
#include
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;
}
在你的代码基础上修改了多处,仅供参考,谢谢!
结构体指针,指针数组相关细节要搞清楚哦!
#include<stdio.h>
#include <stdlib.h>
struct Ym
{
char name[8];
int age;
char one_time[11];
char two_time[11];
};
void input(struct Ym *pt);
void input(struct Ym *pt)
{
char ch;
printf("请问姓名是:");
scanf("%s", pt->name);
printf("请问年龄是:");
scanf("%d", &pt->age);
printf("请问是否接种过疫苗(Y/N):");
getchar();
if ((ch = getchar()) == 'Y')
{
printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
scanf("%10s", pt->one_time);
}
else
{
*pt->one_time = '\0';
return;
}
printf("请问是否接种过第二针疫苗(Y/N):");
getchar();
if ((ch = getchar()) == 'Y')
{
printf("请输入第二针疫苗接种的日期(yyyy-mm-dd):");
scanf("%10s", pt->two_time);
}
else
*pt->two_time = '\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);这里有问题 */
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')
printf("第一针疫苗接种日期:%s,", pt[k]->one_time);
else
{
printf("未接种疫苗!");
continue;
}
if (*pt[k]->two_time != '\0')
printf("第二针疫苗接种日期:%s\n\n", pt[k]->two_time);
else
printf("未接种第二针疫苗!\n\n");
}
return 0;
}
发现程序存在一些问题:
在函数 input 中,当用户没有接种第一针疫苗时,直接使用了 return 语句退出函数,但该语句的返回值类型为 void,会导致程序出现不可预计的错误。可以将该语句改为 return pt,返回指向结构体的指针。
在主函数中,使用了 *pt[i] 调用指向结构体的指针,实际上应该使用 pt[i]。
在主函数中,每次录入完一个人的信息后,没有及时清空缓存区,导致在下一个人的信息录入时出现了乱码。可以在每次录入完信息后,调用 getchar() 函数清空输入缓冲区。
修改上述问题后的代码如下:
#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 pt;
}
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 pt;
}
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(); // 清空输入缓冲区
printf("当前已经录入%d人疫苗档案\n请问是否要继续录入疫苗登记(Y/N):", i);
}
else
break;
}
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;
}
线程池的五种状态:Running、ShutDown、Stop、Tidying、Terminated