永发信息网

这是一个数据结构图的问题,在44行和46两个printf 中间scanf语句被跳过去了,不知道是什么原因

答案:1  悬赏:0  手机版
解决时间 2021-03-28 22:30
  • 提问者网友:最美的风景
  • 2021-03-28 10:48
这是一个数据结构图的问题,在44行和46两个printf 中间scanf语句被跳过去了,不知道是什么原因
最佳答案
  • 五星知识达人网友:一叶十三刺
  • 2021-03-28 11:00
#include
#include
#define MAX_VERTEX_NUM 20
#define INFINITY 10000
#define elemtype1 char
#define elemtype2 int
typedef struct simple
{
int kind;
int vexnum;
elemtype1 verx[MAX_VERTEX_NUM];
int actnum;
elemtype2 arcs[MAX_VERTEX_NUM][MAX_VERTEX_NUM];
}tu;
tu* creat()
{

int kind,i,weight,m,n;

int arcsnum;
char c1,c2;
tu* p;
p=(tu*)malloc(sizeof(struct simple));
printf("有向图=0,有向网=1\n无向图=2,无向图=3\n");
printf("please scanf the kind of the tu\n");
scanf("%d",&p->kind);
printf("please scanf the vexnum\n");
scanf("%d",&(p->vexnum));

for(i=0;i<(p->vexnum);i++)
{
fflush(stdin);
scanf("%c",&(p->verx)[i]);

}
printf("please scanf the arcsnum");
scanf("%d",&arcsnum);
for(i=0;i {

printf("please scanf the start point:");//就在这里为什么下面的SCANF语句被跳过了???????????????
scanf("%c",&c1);
getchar(); //这是因为回车键的原因,你可以通过这个来吸收回车键。
printf("please scanf the end point:");
scanf("%c",&c2);
getchar(); //你试试!
printf("please scanf the weight:");
scanf("%d",&weight);
getchar();
m=c1-65;
n=c2-65;
(p->arcs)[m][n]=weight;
}
return p;
}
void main()
{
tu* p;
p=creat();
}追问3Q very much 我想继续问一下 为什么会出现这样的问题 为什么不用PUTCHAR就不行了呢 为什么会被跳过去呢?追答那时因为回车键的原因,当你输入完一个字符后,按下回车,scanf将这个字符赋值给相应的变量后,回车键依然停留在缓冲区内,所以当你再获得一个字符的时候,会将缓冲区内的回车键赋值给这个字符(回车键也是字符),所以就不会等待你的输入了。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯