| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 503 人关注过本帖
标题:为什么gets函数无效了
只看楼主 加入收藏
goto09
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-12-22
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
为什么gets函数无效了
为什么下面insert函数中的gets函数无效啊, 运行的时候像没有那个语句一样,直接进行下个语句了。
我是菜鸟啊,学校的作业,谢谢大家了

#include"stdio.h"
#include"string.h"
typedef struct node
{
        char name[20];
        int num;
        int math,eng,phy;
        struct node *next;
}linklist,*list;

list insert(list L)
{
  int x;
  list p,r,b;   r=L;
  printf("please input the Number:");
  scanf("%d",&x);
 while(x!=-1){
  p=(list)malloc(sizeof(linklist));
  printf("please input the name:");
  gets(p->name);
  printf("please input the math:");
  scanf("%d",&p->math);
  printf("please input the eng:");
  scanf("%d",&p->eng);
  printf("please input the phy:");
  scanf("%d",&p->phy);
  p->num=x;  r->next=p; r=r->next; r->next=NULL;
  printf("please input the Number:");
  scanf("%d",&x);
  }
  return(L);
}

list creatlist()
{ list L;
  L=(list)malloc(sizeof(linklist));
  L->num=-1;
  L->next=NULL;
  L=insert(L);
  return(L);
}

list output(list L)
{
  list r;  r=L->next;
  if(r==NULL)  return L;
  while(r!=NULL)
  {  printf("the num is:%d\n",r->num);
     printf("name:%s\n",r->name);
     printf("math:%d\n",r->math);
     printf("eng:%d\n",r->eng);
     printf("phy:%d\n",r->phy);

     printf("----------------------\n");
     r=r->next;                          }
     return L;
}


void main()
{    list L;
     L=creatlist();
     printf("done\n");
     output(L);
     getch();
}

搜索更多相关主题的帖子: 学校 include please insert 
2011-03-06 16:08
njzhangyuhao
Rank: 2
等 级:论坛游民
帖 子:197
专家分:35
注 册:2010-11-20
收藏
得分:10 
是不是上面scanf里面的回车残留在缓存里了,,,我刚问这个问题的
2011-03-06 16:13
huangapple
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:545
专家分:1790
注 册:2010-12-30
收藏
得分:10 
scanf("%d",&x);

getchar();//在这边加个这个就解决了

while(x!=-1){
  p=(list)malloc(sizeof(linklist));
  printf("please input the name:");
  gets(p->name);

勤能补拙,熟能生巧!
2011-03-06 16:16
goto09
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2010-12-22
收藏
得分:0 
其实我刚问完就百度到了   好吧这次是我懒惰了
谢谢大家哈
2011-03-06 16:29
快速回复:为什么gets函数无效了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014139 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved