当$ P $由scanf函数pceded为什么得到()函数跳过("%D")? [英] Why gets() function skips when preceded by scanf("%d")?

查看:125
本文介绍了当$ P $由scanf函数pceded为什么得到()函数跳过("%D")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个程序,采取滚动否,全名作为输入,并只显示它
我的code是。这code通过gets函数跳过n个scaning价值。为什么这个错误发生,如何通过这个来?

 #包括LT&;&stdio.h中GT;
 #包括LT&;&CONIO.H GT;
 无效的主要()
 {
 INT R;
 CHAR N [30];
 的printf(无输入你滚);
 scanf函数(%d个,&安培; R);
 的printf(请输入您的全名);
 得到(N);
 的printf(滚不为%d,R);
 的printf(名称为%s,N);
 残培();
 }

而低于code扫描首先获取价值并跳过第二个。

 #包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;
无效的主要()
{
 INT R;
 CHAR N [30],F [30];
 的printf(无输入你滚);
 scanf函数(%d个,&安培; R);
 的printf(请输入您的全名);
 得到(N);
 的printf(请输入您你父亲的全名);
 获得(F);
 的printf(滚不为%d,R);
 的printf(名称为%s,N);
 的printf(父亲的名字为%s,F);
 残培();
 }


解决方案

有关问题的简单解决方案是增加 fflush(标准输入); 之间的 scanf()的; 获得();

 #包括LT&;&stdio.h中GT;
#包括LT&;&CONIO.H GT;
无效的主要()
{
    INT R;
    CHAR N [30],FN [30];
    clrscr();
    的printf(\\ n输入卷);
    scanf函数(%d个,&安培; R);
    fflush(标准输入);
    的printf(\\ n输入名称);
    得到(N);
    的printf(\\ n输入父亲的名字);
    得到(FN);    的printf(\\ n \\ nRoll%D,R);
    的printf(\\ n名称%S,N);
    的printf(\\ nfather名称%s,FN);
    残培();
}

I want to make a program which take Roll No and Full name as input and simply display it My code is . this code skip scaning value of n through gets function. Why this error occur and how to over come this?

 #include<stdio.h>
 #include<conio.h>
 void main()
 {
 int r;
 char n[30];
 printf("enter your roll no");
 scanf("%d",&r);
 printf("enter your full name");
 gets(n);
 printf("roll no is %d ",r);
 printf("name is %s ",n);
 getch();
 }

while the below code scan the first gets value and skips the second one.

#include<stdio.h>
#include<conio.h>
void main()
{
 int r;
 char n[30], f[30];
 printf("enter your roll no");
 scanf("%d",&r);
 printf("enter your full name");
 gets(n);
 printf("enter your full name of your father ");
 gets(f);
 printf("roll no is %d ",r);
 printf("name is %s ",n);
 printf("father name is %s ",f);
 getch();
 }

解决方案

The simple solution for the problem is to add fflush(stdin); between scanf(); and gets();

#include<stdio.h>
#include<conio.h>
void main()
{
    int r;
    char n[30],fn[30];
    clrscr();
    printf("\nEnter roll ");
    scanf("%d",&r);
    fflush(stdin);
    printf("\nEnter name ");
    gets(n);
    printf("\nEnter father name ");
    gets(fn);

    printf("\n\nRoll %d",r);
    printf("\nname %s",n);
    printf("\nfather name %s",fn);
    getch();
}

这篇关于当$ P $由scanf函数pceded为什么得到()函数跳过(&QUOT;%D&QUOT;)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆