鱼C论坛

 找回密码
 立即注册
查看: 4245|回复: 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:36:06 From FishC Mobile | 显示全部楼层
isdkz 发表于 2023-4-7 00:25
首先,我注意到您的 input 函数的原型和定义中包含了返回值类型 struct Ym*,但是在函数内部没有返回任何值 ...

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

修改后的 ...

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-11-7 03:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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