错误接收输入 [英] Error receiving inputs

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

问题描述

我经常看到情况下,在一系列的printf声明,要求输入,某些
声明不接受输入,即

  printf_statement1:printf_statement2:/ *在此处输入输入* /

即。用户输入的输入用于前<青霉>语句1 ,语句2的被印刷,其允许用户输入的输入,然后重复相同的同的statement3 的&安培;的即statement4

是什么促使这种情况?

我encounterd这一点,但是现在不是了,在

 的printf(\\ n \\ n \\ t \\ TNAME:);
 scanf函数(%S,名); //最初使用得到
 的printf(\\ n \\ n \\ t \\ Af - Ag型:);
 scanf函数(%C,&安培;型);
 的printf(\\ n \\ n \\ t \\ tAddress:); //最初使用得到
 scanf函数(%S,地址);
 的printf(\\ n \\ n \\ t \\ tDate - 的 - 诞生:);
 scanf函数(%S,DOB); //最初使用得到

输出:

 名称:
类型:我输入的东西!
地址:
日期 - 的 - 诞生:进入我的东西!


解决方案

有时候在这样的情况下,是有帮助的呼叫的 fflush() 上的 标准输入 从中读取之前:

 字符名称[40 + 1];
的printf(\\ n \\ TNAME:);
fflush(标准输入);
scanf函数(%40多岁,名);

根据不同的情况,有可能是导致你的第一个的 scanf函数 得到跳过(未给出输入)。

另外请注意,您应该指定缓冲区的 scanf函数 避免缓冲区溢出的。

格式说明scanf的用于看起来是这样的:

 %[*] [宽度] [修饰符]类型


  

    

宽度:指定在当前读取操作可读取字符的最大数量


  

因此​​, 40年代%规定只有40个字符可到字符串读取。请注意,缓冲区41个字符,留有余地空终止符。

修改:一些消息来源(如的)说,你应该避免调用 fflush(标准输入),这是不正确。的然而的,在 Linux手册页状态:


  

    

有关输入流,fflush()丢弃已经从底层文件取出所有缓冲的数据,但还没有得到应用所消耗


    
    

顺应C89,C99,POSIX.1-2001,POSIX.1-2008。
    该标准不指定输入流的行为。大多数其他实现行为相同的Linux。


  

此外 MSDN 只字未提调用 fflush(标准输入)比下面其他:


  

    

如果该流是开放的输入,fflush清除缓冲区中的内容。


  

有人会认为,如果调用 fflush(标准输入)是这样一个问题,关于 fflush 就不得不提到这一点。

I have often seen cases where in a series of printf statements that ask for an input, certain statements do not accept an input, i.e.

printf_statement1:

printf_statement2: /*Enter input here*/

i.e. before the user enters an input for statement1, statement2 is printed, that allows the user to enter an input, and then repeats the same with statement3 & statement4.

What prompts such a case?

I encounterd this, but not any more, in:

 printf("\n\n\t\tName: ");
 scanf("%s", name); //initially used gets
 printf("\n\n\t\tType: ");     
 scanf("%c", &type);
 printf("\n\n\t\tAddress: ");      //initially used gets
 scanf("%s", address);
 printf("\n\n\t\tDate - of - Birth: ");
 scanf("%s", dob); //initially used gets

Output:

Name:
Type: I enter something!
Address:
Date - of - Birth: I enter something!

解决方案

Sometimes in cases like these, it is helpful to call fflush() on stdin before reading from it:

char name[40+1];
printf("\n\tName: ");
fflush(stdin);
scanf("%40s", name);

Depending on the circumstances, there may be a newline in the input buffer that causes your first scanf to get skipped (given no input).

Also note that you should specify the size of the buffer in scanf as to avoid buffer overflows.

A format specifier for scanf looks like this:

%[*][width][modifiers]type

width: Specifies the maximum number of characters to be read in the current reading operation

Thus, the %40s specifies that only 40 characters may be read in to the string. Note that the buffer is 41 characters, leaving room for the NULL terminator.

Edit: Some sources (like this and this) say that you should avoid calling fflush(stdin) and it is incorrect. However, the Linux man page states:

For input streams, fflush() discards any buffered data that has been fetched from the underlying file, but has not been consumed by the application.

Conforming to C89, C99, POSIX.1-2001, POSIX.1-2008. The standards do not specify the behavior for input streams. Most other implementations behave the same as Linux.

Additionally: MSDN says nothing about calling fflush(stdin) other than the following:

If the stream is open for input, fflush clears the contents of the buffer.

One would think that if calling fflush(stdin) was such a problem, one of the two largest sources of documentation on fflush would have mentioned this.

这篇关于错误接收输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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