鱼C论坛

 找回密码
 立即注册
查看: 3501|回复: 7

[已解决]程序录入的第一个人之后的资料不对 乱码了 什么问题呢?求解答 谢谢!

[复制链接]
发表于 2023-4-7 00:22:10 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>
  2. #include <stdlib.h>

  3. struct Ym
  4. {
  5.         char name[8];
  6.         int age;
  7.         char one_time[11];
  8.         char two_time[11];
  9. };

  10. struct Ym* input(struct Ym *pt,int j);
  11. struct Ym* input(struct Ym *pt,int j)
  12. {
  13.         char ch;
  14.         printf("请问姓名是:");
  15.         scanf("%s",pt[j].name);
  16.         printf("请问年龄是:");
  17.         scanf("%d",&pt[j].age);
  18.         printf("请问是否接种过疫苗(Y/N):");
  19.         getchar();

  20.        
  21.         if((ch = getchar()) == 'Y')
  22.         {
  23.                 printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
  24.                 scanf("%s",pt[j].one_time);
  25.                
  26.         }
  27.         else
  28.         {
  29.                 pt[j].one_time[0] = '0';
  30.                 return;
  31.         }
  32.         printf("请问是否接种过第二针疫苗(Y/N):");
  33.         getchar();

  34.        
  35.         if((ch = getchar()) == 'Y')
  36.         {
  37.        
  38.                 printf("请输入第二针疫苗接种的日期(yyyy-mm-dd):");
  39.                 scanf("%s",pt[j].two_time);
  40.         }
  41.         else
  42.         pt[j].two_time[0] = '0';
  43.        
  44.         printf("\n\n");

  45.         return;
  46. }


  47. int main(void)
  48. {
  49.         struct Ym *pt[255];
  50.         int i = 0;
  51.         char j,ch;
  52.         printf("请问是否要录入疫苗登记(Y/N):");
  53.        
  54.         while(1)
  55.         {
  56.                 ch = getchar();
  57.                
  58.                 if(ch == 'Y')
  59.                 {
  60.                         pt[i] = (struct Ym *)malloc(sizeof(struct Ym));
  61.                         input(*pt,i);
  62.                         i++;
  63.                         getchar();
  64.                 }
  65.                 else
  66.                 break;
  67.                 printf("当前已经录入%d人疫苗档案\n请问是否要继续录入疫苗登记(Y/N):",i);
  68.         }
  69.        
  70.         printf("====录入数据完毕(总共%d人如下)====\n\n",i);
  71.         int k;
  72.         for(k = 0; k < i; k++)
  73.         {
  74.                 printf("姓名:%s",pt[k]->name);       
  75.                 printf("年龄:%d\n",pt[k]->age);       
  76.                 if(pt[k]->one_time[0] != '0')
  77.                 printf("第一针疫苗接种日期:%s,",pt[k]->one_time);
  78.                 else
  79.                 {
  80.                         printf("未接种疫苗!");
  81.                         continue;
  82.                 }
  83.                 if(pt[k]->two_time[0] != '0')
  84.                 printf("第二针疫苗接种日期:%s\n\n",pt[k]->two_time);
  85.                 else
  86.                 printf("未接种第二针疫苗!\n\n");

  87.         }         

  88.         return 0;
  89. }
复制代码
最佳答案
2023-4-7 00:25:52
首先,我注意到您的 input 函数的原型和定义中包含了返回值类型 struct Ym*,但是在函数内部没有返回任何值。

为了修复这个问题,您应该将这个函数的返回值类型更改为 void,因为您实际上没有返回任何值。

下面是修复后的函数原型和定义:

  1. void input(struct Ym *pt,int j);
  2. void input(struct Ym *pt,int j)
  3. {
  4.     ...
  5. }
复制代码


接下来,关于乱码的问题,我注意到您使用了一个指针数组 struct Ym *pt[255]。

但在调用 input 函数时,您使用了 input(*pt, i)。这里应该使用 input(pt[i], i) 而不是 input(*pt, i)。

这是因为 input 函数的第一个参数是一个指向 struct Ym 的指针,而 *pt 会解引用第一个元素,从而导致未定义的行为。

以下是修复后的 main 函数中的部分代码:

  1. while(1)
  2. {
  3.     ch = getchar();
  4.    
  5.     if(ch == 'Y')
  6.     {
  7.         pt[i] = (struct Ym *)malloc(sizeof(struct Ym));
  8.         input(pt[i], i); // 修复这里的调用
  9.         i++;
  10.         getchar();
  11.     }
  12.     else
  13.     break;
  14.     printf("当前已经录入%d人疫苗档案\n请问是否要继续录入疫苗登记(Y/N):",i);
  15. }
复制代码


按照这些更改后,程序应该正常运行,不会出现乱码问题。如果还有其他问题,请随时告诉我。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-4-7 00:25:52 | 显示全部楼层    本楼为最佳答案   
首先,我注意到您的 input 函数的原型和定义中包含了返回值类型 struct Ym*,但是在函数内部没有返回任何值。

为了修复这个问题,您应该将这个函数的返回值类型更改为 void,因为您实际上没有返回任何值。

下面是修复后的函数原型和定义:

  1. void input(struct Ym *pt,int j);
  2. void input(struct Ym *pt,int j)
  3. {
  4.     ...
  5. }
复制代码


接下来,关于乱码的问题,我注意到您使用了一个指针数组 struct Ym *pt[255]。

但在调用 input 函数时,您使用了 input(*pt, i)。这里应该使用 input(pt[i], i) 而不是 input(*pt, i)。

这是因为 input 函数的第一个参数是一个指向 struct Ym 的指针,而 *pt 会解引用第一个元素,从而导致未定义的行为。

以下是修复后的 main 函数中的部分代码:

  1. while(1)
  2. {
  3.     ch = getchar();
  4.    
  5.     if(ch == 'Y')
  6.     {
  7.         pt[i] = (struct Ym *)malloc(sizeof(struct Ym));
  8.         input(pt[i], i); // 修复这里的调用
  9.         i++;
  10.         getchar();
  11.     }
  12.     else
  13.     break;
  14.     printf("当前已经录入%d人疫苗档案\n请问是否要继续录入疫苗登记(Y/N):",i);
  15. }
复制代码


按照这些更改后,程序应该正常运行,不会出现乱码问题。如果还有其他问题,请随时告诉我。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-7 00:36:06 From FishC Mobile | 显示全部楼层
isdkz 发表于 2023-4-7 00:25
首先,我注意到您的 input 函数的原型和定义中包含了返回值类型 struct Ym*,但是在函数内部没有返回任何值 ...

666,厉害呀!你是机器人吗?回复这么快。等明天我看看有没有其他回复,明天给你最佳答案,谢谢大佬!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-7 17:01:45 | 显示全部楼层
a905448839 发表于 2023-4-7 00:36
666,厉害呀!你是机器人吗?回复这么快。等明天我看看有没有其他回复,明天给你最佳答案,谢谢大佬!

机器人实锤
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-7 22:49:58 | 显示全部楼层
isdkz 发表于 2023-4-7 00:25
首先,我注意到您的 input 函数的原型和定义中包含了返回值类型 struct Ym*,但是在函数内部没有返回任何值 ...

你好,录入第三次数据卡住了!

不知道怎么上传图片。。。

以下是已修改代码。
  1. #include<stdio.h>
  2. #include <stdlib.h>

  3. struct Ym
  4. {
  5.         char name[8];
  6.         int age;
  7.         char one_time[11];
  8.         char two_time[11];
  9. };

  10. void input(struct Ym *pt,int j);
  11. void input(struct Ym *pt,int j)
  12. {
  13.         char ch;
  14.         printf("请问姓名是:");
  15.         scanf("%s",pt[j].name);
  16.         printf("请问年龄是:");
  17.         scanf("%d",&pt[j].age);
  18.         printf("请问是否接种过疫苗(Y/N):");
  19.         getchar();

  20.        
  21.         if((ch = getchar()) == 'Y')
  22.         {
  23.                 printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
  24.                 scanf("%s",pt[j].one_time);
  25.                
  26.         }
  27.         else
  28.         {
  29.                 pt[j].one_time[0] = '0';
  30.                 return ;
  31.         }
  32.         printf("请问是否接种过第二针疫苗(Y/N):");
  33.         getchar();

  34.        
  35.         if((ch = getchar()) == 'Y')
  36.         {
  37.        
  38.                 printf("请输入第二针疫苗接种的日期(yyyy-mm-dd):");
  39.                 scanf("%s",pt[j].two_time);
  40.         }
  41.         else
  42.         pt[j].two_time[0] = '0';
  43.        
  44.         printf("\n\n");

  45.         return ;
  46. }


  47. int main(void)
  48. {
  49.         struct Ym *pt[255];
  50.         int i = 0;
  51.         char j,ch;
  52.         printf("请问是否要录入疫苗登记(Y/N):");
  53.        
  54.         while(1)
  55.         {
  56.                 ch = getchar();
  57.                
  58.                 if(ch == 'Y')
  59.                 {
  60.                         pt[i] = (struct Ym *)malloc(sizeof(struct Ym));
  61.                         input(pt[i],i);
  62.                         i++;
  63.                         getchar();
  64.                 }
  65.                 else
  66.                 break;
  67.                 printf("当前已经录入%d人疫苗档案\n请问是否要继续录入疫苗登记(Y/N):",i);
  68.         }
  69.        
  70.         printf("====录入数据完毕(总共%d人如下)====\n\n",i);
  71.         int k;
  72.         for(k = 0; k < i; k++)
  73.         {
  74.                 printf("姓名:%s",pt[k]->name);       
  75.                 printf("年龄:%d\n",pt[k]->age);       
  76.                 if(pt[k]->one_time[0] != '0')
  77.                 printf("第一针疫苗接种日期:%s,",pt[k]->one_time);
  78.                 else
  79.                 {
  80.                         printf("未接种疫苗!");
  81.                         continue;
  82.                 }
  83.                 if(pt[k]->two_time[0] != '0')
  84.                 printf("第二针疫苗接种日期:%s\n\n",pt[k]->two_time);
  85.                 else
  86.                 printf("未接种第二针疫苗!\n\n");

  87.         }         

  88.         return 0;
  89. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-7 22:56:09 | 显示全部楼层
a905448839 发表于 2023-4-7 22:49
你好,录入第三次数据卡住了!

不知道怎么上传图片。。。

录入第三次数据卡住的问题是因为在main函数中的循环部分,你需要在循环末尾清空输入缓冲区。

修改后的main函数如下:

  1. int main(void)
  2. {
  3.     struct Ym *pt[255];
  4.     int i = 0;
  5.     char ch;
  6.     printf("请问是否要录入疫苗登记(Y/N):");
  7.    
  8.     while(1)
  9.     {
  10.         ch = getchar();
  11.       
  12.         if(ch == 'Y')
  13.         {
  14.             pt[i] = (struct Ym *)malloc(sizeof(struct Ym));
  15.             input(pt[i],i);
  16.             i++;
  17.         }
  18.         else
  19.         break;

  20.         while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  21.         printf("当前已经录入%d人疫苗档案\n请问是否要继续录入疫苗登记(Y/N):",i);
  22.     }
  23.    
  24.     printf("====录入数据完毕(总共%d人如下)====\n\n",i);
  25.     int k;
  26.     for(k = 0; k < i; k++)
  27.     {
  28.         printf("姓名:%s",pt[k]->name);      
  29.         printf("年龄:%d\n",pt[k]->age);      
  30.         if(pt[k]->one_time[0] != '0')
  31.         printf("第一针疫苗接种日期:%s,",pt[k]->one_time);
  32.         else
  33.         {
  34.             printf("未接种疫苗!");
  35.             continue;
  36.         }
  37.         if(pt[k]->two_time[0] != '0')
  38.         printf("第二针疫苗接种日期:%s\n\n",pt[k]->two_time);
  39.         else
  40.         printf("未接种第二针疫苗!\n\n");

  41.     }         

  42.     return 0;
  43. }
复制代码


这样修改后,程序应该可以正常录入第三次(以及后续)数据。

请尝试使用这个修改后的代码,并检查是否解决了您遇到的问题。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-7 23:06:43 | 显示全部楼层
isdkz 发表于 2023-4-7 22:56
录入第三次数据卡住的问题是因为在main函数中的循环部分,你需要在循环末尾清空输入缓冲区。

修改后的 ...

还是卡住的。。不过现在更快的运算出来了 等待几秒钟时间 程序便结束 不会让我录入第三个人的数据
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-4-7 23:39:36 | 显示全部楼层
a905448839 发表于 2023-4-7 23:06
还是卡住的。。不过现在更快的运算出来了 等待几秒钟时间 程序便结束 不会让我录入第三个人的数据

[b]我发现在input函数中仍然需要进行一些调整。我将input函数和main函数进行了优化,以解决卡住的问题。

以下是修改后的代码:

  1. #include<stdio.h>
  2. #include <stdlib.h>

  3. struct Ym
  4. {
  5.         char name[8];
  6.         int age;
  7.         char one_time[11];
  8.         char two_time[11];
  9. };

  10. void input(struct Ym *pt);
  11. void input(struct Ym *pt)
  12. {
  13.     char ch;
  14.     printf("请问姓名是:");
  15.     scanf("%s", pt->name);
  16.     printf("请问年龄是:");
  17.     scanf("%d", &pt->age);

  18.     while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  19.     printf("请问是否接种过疫苗(Y/N):");

  20.     if ((ch = getchar()) == 'Y')
  21.     {
  22.         printf("请输入第一针疫苗接种的日期(yyyy-mm-dd):");
  23.         scanf("%s", pt->one_time);
  24.     }
  25.     else
  26.     {
  27.         pt->one_time[0] = '0';
  28.         return;
  29.     }

  30.     while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  31.     printf("请问是否接种过第二针疫苗(Y/N):");

  32.     if ((ch = getchar()) == 'Y')
  33.     {
  34.         printf("请输入第二针疫苗接种的日期(yyyy-mm-dd):");
  35.         scanf("%s", pt->two_time);
  36.     }
  37.     else
  38.         pt->two_time[0] = '0';

  39.     printf("\n\n");

  40.     return;
  41. }

  42. int main(void)
  43. {
  44.     struct Ym *pt[255];
  45.     int i = 0;
  46.     char ch;
  47.     printf("请问是否要录入疫苗登记(Y/N):");

  48.     while (1)
  49.     {
  50.         ch = getchar();

  51.         if (ch == 'Y')
  52.         {
  53.             pt[i] = (struct Ym *)malloc(sizeof(struct Ym));
  54.             input(pt[i]);
  55.             i++;
  56.         }
  57.         else
  58.             break;

  59.         while ((ch = getchar()) != '\n' && ch != EOF); // 添加这行以清空输入缓冲区

  60.         printf("当前已经录入%d人疫苗档案\n请问是否要继续录入疫苗登记(Y/N):", i);
  61.     }

  62.     printf("====录入数据完毕(总共%d人如下)====\n\n", i);
  63.     int k;
  64.     for (k = 0; k < i; k++)
  65.     {
  66.         printf("姓名:%s", pt[k]->name);
  67.         printf("年龄:%d\n", pt[k]->age);
  68.         if (pt[k]->one_time[0] != '0')
  69.             printf("第一针疫苗接种日期:%s,", pt[k]->one_time);
  70.         else
  71.         {
  72.             printf("未接种疫苗!");
  73.             continue;
  74.         }
  75.         if (pt[k]->two_time[0] != '0')
  76.             printf("第二针疫苗接种日期:%s\n\n", pt[k]->two_time);
  77.         else
  78.             printf("未接种第二针疫苗!\n\n");
  79.     }

  80.     return 0;
  81. }
复制代码

/b]
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 00:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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