简体   繁体   English

清除输入缓冲区是什么意思?

[英]What does clearing of input buffer mean?

I was going through a code in my school textbook, wherein there is a line who's function is to clear the input buffer (mentioned as a comment in the code). 我正在阅读学校教科书中的代码,其中一行的作用是清除输入缓冲区(在代码中作为注释提及)。

I couldn't quite understand its purpose. 我不太明白它的目的。 It is definitely required as its removal messes up the console input process. 由于将其删除弄乱了控制台输入过程,因此绝对是必需的。

Please explain what its function is, and what is happening when I remove it. 请说明其功能,以及删除它时发生的情况。

I have also tried using cin.ignore(); 我也尝试过使用cin.ignore(); and it works just fine too. 而且也很好用 How is the function used here, is it an exact replacement of cin.ignore()? 这里如何使用函数,它是cin.ignore()的精确替代品吗?

PS In school we are using the older version of C++. PS在学校里,我们使用的是旧版本的C ++。 Hence the ".h" extension, clrscr();, etc. 因此,扩展名为“ .h”,clrscr();等。

#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main(){
clrscr();

ofstream fout("student.txt", ios::out);
char name[30], ch;
float marks = 0.0;

for(int i = 0; i < 5; i++){
    cout << "Student " << (i+1) << ":\tName: ";
    cin.get(name,30);
    cout << "\t\tMarks: ";
    cin >> marks;
    cin.get(ch);  //for clearing input buffer (This thing!)
    fout << name << '\n' << marks << '\n';
}

fout.close();

ifstream fin("student.txt", ios::in);
fin.seekg(0);
cout << "\n";

for(int i = 0; i < 5; i++){
    fin.get(name,30);
    fin.get(ch); //Again
    fin >> marks;
    fin.get(ch); //Same
    cout << "Student Name: " << name;
    cout << "\tMarks: " << marks << "\n";
}

fin.close();
getch();

}

I just want to start with pointing out that you are not using "the older version of C++". 我只想指出您没有使用“旧版本的C ++”。 You are just using some c code (like clrscr()) and c styled programming, and indeed probably not cpp11 or cpp14. 您只是在使用一些C代码(例如clrscr())和c样式的编程,实际上可能不是cpp11或cpp14。 Also header files for C++ are often still just .h. 同样,C ++的头文件通常仍然只是.h。

Now the answer to your question: 现在,您的问题的答案:

cin.get(ch);  

The '\\n' character gets read into ch. '\\ n'字符被读入ch。 '\\n' is one character, a newline character. '\\ n'是一个字符,是换行符。

does indeed do the same thing as 确实做与

cin.ignore();

It is used to clear the input buffer, which means that the empty end of line ('\\n') is being deleted from the input buffer. 它用于清除输入缓冲区,这意味着将从输入缓冲区中删除行的空行(\\ n)。

Why is this done you ask? 您为什么要这样做? This is a good example of why you would do that. 是您为什么要这样做的一个很好的例子。 Hope this helped you! 希望这对您有所帮助!

 cin >> marks; cin.get(ch); //for clearing input buffer (This thing!) 

This is a not-so-robust way of clearing the input buffer. 这是清除输入缓冲区的一种不太可靠的方法。 If you type a number followed by Enter , the first line will consume the number and put the value in marks while the second line will read the newline character and discard it. 如果您键入数字,然后按Enter ,则第一行将使用该数字并将值放入marks而第二行将读取换行符并将其丢弃。

It is not robust since it does not account for spaces a user might have entered after the number. 它不健壮,因为它不考虑用户可能在数字后输入的空格。 A more robust method would be to use istream::ignore . 一种更可靠的方法是使用istream::ignore

cin >> marks;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM