简体   繁体   English

C++ - 有什么方法可以将输入插入缓冲区/停止需要输入的 cin.ignore?

[英]C++ - Any way to insert an enter into the buffer/Stop cin.ignore requiring enter?

Pretty much as the title says.和标题说的差不多。 I'm creating a code segment and need to clear the buffer before it in order to start a new input, however, when using cin.ignore() , the buffer is cleared but the user is required to hit another enter afterward.我正在创建一个代码段,需要在它之前清除缓冲区以开始新的输入,但是,当使用cin.ignore() ,缓冲区被清除,但用户需要在之后再次输入。 Is there any way I can insert an enter into the buffer to automatically hit enter for the user, or that I can get cin.ignore to not require another enter.有什么方法可以将输入插入缓冲区以自动为用户点击输入,或者我可以让cin.ignore不需要再次输入。

I am using cin.getline to retrieve the input.我正在使用cin.getline来检索输入。

void Clear(){
    cin.clear();
    cin.ignore(250, '\n');
}

2 issues: 2个问题:

  • The first parameter in the "std::cin.ignore()" that you are using just comes down to a very large number.您正在使用的“std::cin.ignore()”中的第一个参数归结为一个非常大的数字。 This should be the maximum number of characters that the input buffer can hold .这应该是输入缓冲区可以容纳的最大字符数 This number may be different on different systems or even header files for different compilers.这个数字在不同的系统上可能不同,甚至在不同编译器的头文件上也可能不同。

  • You need to press enter twice because there is nothing in the buffer to ignore .您需要按 Enter 两次,因为缓冲区中没有要忽略的内容 It is waiting for something to be entered to ignore.它正在等待输入的东西忽略。 some people will use this to pause the program before the "return 0;".有些人会在“return 0;”之前使用它来暂停程序。

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

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