鱼C论坛

 找回密码
 立即注册
查看: 2859|回复: 6

[已解决]有没有大佬帮孩子看一下,在主函数free(a)前面的那个printf为什么会打印出两次

[复制链接]
发表于 2022-11-26 18:01:35 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 zwj.123 于 2022-11-26 18:06 编辑

   
  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #pragma pack(1)
  5. typedef struct Link{
  6.    int id;
  7.    char name[30];
  8.    char gender[20];
  9.    char role[50];
  10.    int age;
  11.    char skill[50];

  12. }Node;
  13. void _input(Node *,int n);
  14. Node* _add(Node *,int n,int *);
  15. void _print(Node a[],int n);
  16. void _alter(Node *);
  17. Node * _delete(Node *,int n);
  18. void _quit();
  19. int main()
  20. {
  21.     int i=0,n=0,m=0;
  22.     int *p = &m;
  23.     char order;
  24.     Node *a;

  25.     printf("Now you can do these things\n");
  26.     printf("if you want to add ,input the order   :a\n");
  27.     printf("if you want to look,input the order   :l\n");
  28.     printf("if you want to alter,input the order  :e\n");
  29.     printf("if you want to delete,input the order :d\n");
  30.     printf("if you want to quit,input the order   :q\n");
  31.    
  32.     order=getchar();
  33. begin:
  34.     switch(order)
  35.     {
  36.       case 'a': a= _add(a,n,p);      break;
  37.       case 'l': n=n+*p;  _print(a,n);break;
  38.       case 'e': _alter(a);           break;
  39.       case 'd':a= _delete(a,n);     break;
  40.       case 'q': _quit();        break;
  41.       default : printf("you enter the wrong order\n");break;
  42.     }
  43.     if(order!='q')
  44.     {   
  45.        printf("You not input the q,so enter the order again:\n");//第一次提问,有很多不会,如果做的不好希望见谅,抱歉,
  46.       fflush(stdin);
  47.       order=getchar();
  48.       fflush(stdin);
  49.       goto begin;
  50.     }

  51.     free(a);
  52.    
  53.    
  54. return 0;
  55. }

  56. Node * _add(Node *a,int n,int *p)
  57. {
  58.     int i;
  59.    printf("how much do you want to add? Please input the number:\n");
  60.    scanf("%d",p);
  61.    Node * b=(Node*)malloc((*p+n)*sizeof(Node));
  62.    for(i=0;i<n;i++)
  63.    {
  64.     b[i].id=a[i].id;
  65.     b[i].age=a[i].age;
  66.     strcpy(b[i].name,a[i].name);
  67.     strcpy(b[i].gender,a[i].gender);
  68.     strcpy(b[i].skill,a[i].skill);

  69.    }
  70.     printf("Please input the people's information again\n");
  71.     printf("including id,name,gender,role,age,skill\n");
  72.     for(i=n;i<*p+n;i++)
  73.     {
  74.      scanf("%d %s %s %s %d %s",&b[i].id,b[i].name,b[i].gender,b[i].role,&b[i].age,b[i].skill);
  75.     }
  76.    
  77. return b;
  78. }




  79. }
复制代码
最佳答案
2022-11-26 18:25:03
order=getchar(); // 你输入1个字符,其实有2个,(1个可见字符,1个回车)
getchar(); // 加getchar() 吃掉回车
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-11-26 18:25:03 | 显示全部楼层    本楼为最佳答案   
order=getchar(); // 你输入1个字符,其实有2个,(1个可见字符,1个回车)
getchar(); // 加getchar() 吃掉回车
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-26 18:29:27 | 显示全部楼层
ba21 发表于 2022-11-26 18:25
order=getchar(); // 你输入1个字符,其实有2个,(1个可见字符,1个回车)
getchar(); // 加getchar() 吃 ...

我试试,哈哈
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-26 18:32:39 | 显示全部楼层
ba21 发表于 2022-11-26 18:25
order=getchar(); // 你输入1个字符,其实有2个,(1个可见字符,1个回车)
getchar(); // 加getchar() 吃 ...

请问下用scanf可行吗,我试了试还是老样子
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-26 18:38:29 | 显示全部楼层
zwj.123 发表于 2022-11-26 18:32
请问下用scanf可行吗,我试了试还是老样子

你不看看你有几个地方使用了getchar()
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-26 18:43:11 | 显示全部楼层
ba21 发表于 2022-11-26 18:38
你不看看你有几个地方使用了getchar()

我把全部getchar 改成了scanf发现没有变化,然后我在出问题的句子后面加上了getchar(),发现问题解决了,这就令我疑惑的是scanf它也接受前面留下的回车吗?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-26 18:58:27 | 显示全部楼层
本帖最后由 zwj.123 于 2022-11-26 18:59 编辑
ba21 发表于 2022-11-26 18:38
你不看看你有几个地方使用了getchar()


噢噢,知道错误了捏,谢谢麻烦您了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 02:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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