清除C/C中的输入缓冲区++

什么是缓冲区? 临时存储区称为缓冲区。所有标准的输入和输出设备都包含一个输入和输出缓冲区。在标准C/C++中,流被缓冲,例如在标准输入的情况下,当我们按下键盘上的键时,它不会被发送到程序,而是由操作系统缓冲,直到时间分配给该程序。

null

它如何影响编程? 在各种情况下,您可能需要清除不需要的缓冲区,以便在所需容器中获得下一个输入,而不是在前一个变量的缓冲区中。例如,在遇到“SCANFE()”的情况下,如果需要输入字符数组或字符,在C++的情况下,在遇到“CIN”语句后,我们需要输入字符数组或字符串,需要清除输入缓冲区,否则所需的输入由前一个变量的缓冲区占用,而不是由所需的容器占用。在第一次输入后,在输出屏幕上按“回车”(回车),因为前一个变量的缓冲区是新容器的空间(因为我们没有清除它),程序跳过容器的后续输入。

案例 C语言编程

C

// C Code to explain why not
// clearing the input buffer
// causes undesired outputs
#include<stdio.h>
int main()
{
char str[80], ch;
// Scan input from user -
// GeeksforGeeks for example
scanf ( "%s" , str);
// Scan character from user-
// 'a' for example
ch = getchar ();
// Printing character array,
// prints “GeeksforGeeks”)
printf ( "%s" , str);
// This does not print
// character 'a'
printf ( "%c" , ch);
return 0;
}


输入:

GeeksforGeeksa

输出:

GeeksforGeeks

案例 C++

C++

// C++ Code to explain why
// not clearing the input
// buffer causes undesired
// outputs
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int a;
char ch[80];
// Enter input from user
// - 4 for example
cin >> a;
// Get input from user -
// "GeeksforGeeks" for example
cin.getline(ch,80);
// Prints 4
cout << a << endl;
// Printing string : This does
// not print string
cout << ch << endl;
return 0;
}


输入:

4GeeksforGeeks

输出:

4

在上述两种代码中,输出未按要求打印。原因是缓冲区被占用。”“字符将保留在缓冲区中,并作为下一个输入读取。

如何解决?

C的情况:

1.使用“while((getchar())!=”’); ” :键入“while((getchar())!=”’);” 读取缓冲区字符直到结束,丢弃它们(包括换行符),并在“scanf()”语句清除输入缓冲区并允许在所需容器中输入后使用它。

C

// C Code to explain why adding
// "while ( (getchar()) != '');"
// after "scanf()" statement
// flushes the input buffer
#include<stdio.h>
int main()
{
char str[80], ch;
// scan input from user -
// GeeksforGeeks for example
scanf ( "%s" , str);
// flushes the standard input
// (clears the input buffer)
while (( getchar ()) != '' );
// scan character from user -
// 'a' for example
ch = getchar ();
// Printing character array,
// prints “GeeksforGeeks”)
printf ( "%s" , str);
// Printing character a: It
// will print 'a' this time
printf ( "%c" , ch);
return 0;
}


输入:

GeeksforGeeksa

输出:

GeeksforGeeksa

2.使用“fflush(stdin)” :在“scanf()”语句之后键入“fflush(stdin)”也会清除输入缓冲区,但会避免使用它,根据C++11标准,输入流被称为“未定义”。

C++案例:

1.使用“cin.ignore(numeric_limits::max(),”’); ” :-输入“cin.ignore(数值限制::max(),”’);” 在“cin”语句之后,将丢弃输入流中的所有内容,包括换行符。

C++

// C++ Code to explain how
// "cin.ignore(numeric_limits
// <streamsize>::max(),'');"
// discards the input buffer
#include<iostream>
// for <streamsize>
#include<ios>
// for numeric_limits
#include<limits>
using namespace std;
int main()
{
int a;
char str[80];
// Enter input from user
// - 4 for example
cin >> a;
// discards the input buffer
cin.ignore(numeric_limits<streamsize>::max(), '' );
// Get input from user -
// GeeksforGeeks for example
cin.getline(str, 80);
// Prints 4
cout << a << endl;
// Printing string : This
// will print string now
cout << str << endl;
return 0;
}


输入:

4GeeksforGeeks

输出:

4GeeksforGeeks

2.使用“cin.sync()”: 在“cin”语句之后键入“cin.sync()”将丢弃缓冲区中剩余的所有内容。虽然“cin.sync()” 不起作用 在所有实现中(根据C++11及以上标准)。

C++

// C++ Code to explain how " cin.sync();"
// discards the input buffer
#include<iostream>
#include<ios>
#include<limits>
using namespace std;
int main()
{
int a;
char str[80];
// Enter input from user
// - 4 for example
cin >> a;
// Discards the input buffer
cin.sync();
// Get input from user -
// GeeksforGeeks for example
cin.getline(str, 80);
// Prints 4
cout << a << endl;
// Printing string - this
// will print string now
cout << str << endl;
return 0;
}


输入:

4GeeksforGeeks

输出:

4

3.使用“cin>>ws”: 在“cin”语句之后键入“cin>>ws”会告诉编译器忽略缓冲区,并丢弃字符串或字符数组实际内容之前的所有空格。

C++

// C++ Code to explain how "cin >> ws"
// discards the input buffer along with
// initial white spaces of string
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int a;
string s;
// Enter input from user -
// 4 for example
cin >> a;
// Discards the input buffer and
// initial white spaces of string
cin >> ws;
// Get input from user -
// GeeksforGeeks for example
getline(cin, s);
// Prints 4 and GeeksforGeeks :
// will execute print a and s
cout << a << endl;
cout << s << endl;
return 0;
}


输入:

4GeeksforGeeks

输出:

4GeeksforGeeks

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以写一篇文章,然后将文章邮寄给评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享