DEV-C ++跳过输入 [英] Dev-C++ Input skipped

查看:1437
本文介绍了DEV-C ++跳过输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>
main()
{
      int i;
      char c, text[30];
      float f;
      printf("\nEnter Integer : ");
      scanf("%d",&i);
      printf("\nEnter Character : ");
      c = getch();
      printf("\nEnter String:");
      gets(text);
      printf("\nEnter Float:");
      scanf("%f",&f);
      printf("\nInteger : %d",i);
      printf("\nCharacter : %c8",c);
      printf("\nString : %s",text);
      printf("\nFloat : %f",f);
      getch();
}

为什么这个简单的程序无法读取使用获得()功能字符串?还有什么我应该使用纠正它吗?那么它是在工作的Turbo C在我的旧的32位PC而不是在这里...

Why is this simple program not able to read a string using the gets() function? What else should I use to correct it? Well it it worked in Turbo C in my old 32-bit PC but not here...

推荐答案

通过一些小小的调查,我想,这个问题带有 scanf()的 scanf()的不读行字符的末尾行的'\\ n'这似乎停留在缓冲区和下一条语句居然红了。

With some little research, I guess that the problem comes with scanf(). scanf() reads a line without the end of line character '\n' which seems to stay in the buffer and actually red by the next statement.

另外,你可以使用与fgets()和的sscanf()如下:

alternatively you can use fgets() and sscanf() as follows:

要读我使用的字符:

fgets(text,sizeof(text),stdin);
sscanf(text,"%c",&c); /* or: c = text[0]; */

阅读我用一个整数

to read an integer I have used

fgets(text,sizeof(text),stdin);
sscanf(text,"%d",&i);

我和gets()函数在C当然,我有(到DEVC ++)的一大难题被告知作为一个编译器。不过,我完全记得我没有按照建议和事实证明,与fgets()的行为也依赖编译器。

I had a major problem with gets() in a C course I had (to which DevC++) was advised as a compiler. However, I totally recall I didn't follow the advice and it turned out that the behavior of fgets() is also compiler dependent.

该名男子页获得()有这样:

BUGS

不要使用gets()函数。因为它是不可能告诉不知道提前多少个字符获取数据()将读取,而且因为得到()将继续存储字符过去的缓冲区的末尾,这是非常危险的使用。它已被用于破坏计算机的安全性。使用与fgets()代替。

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead.

这篇关于DEV-C ++跳过输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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