简体   繁体   English

为什么 getline() 之前的 ignore() 少输入一个字符?

[英]Why does ignore() before getline() take one less character input?

I am using the getline() function in C++, but there is a problem that the input starts from the second character.我在C++中使用了getline() function,但是出现了从第二个字符开始输入的问题。 I used the ignore() function to erase what remains in the buffer first, emptying the buffer and receiving input.我使用ignore() function 首先擦除缓冲区中剩余的内容,清空缓冲区并接收输入。 How can I empty the buffer and receive input properly?如何清空缓冲区并正确接收输入?

图片

Above is the execution result.以上是执行结果。 I previously used the ignore() function and the getline() function to empty the buffer and receive input because there may be some leftovers in the buffer before.我之前使用ignore() function 和getline() function 来清空缓冲区并接收输入,因为之前缓冲区中可能有一些剩余。

In other programs that write like that, it also receives integer input before.在其他这样写的程序中,它之前也接收到 integer 输入。

void InputValue(string *st) {
    //cin.clear();
    cin.ignore();
    getline(cin, *st);
}

int main(void) {
    string str;
    InputValue(&str);

    cout << str;
    return 0;
}

In the beginning, immediately after input, stdin (your input buffer) contains:一开始,在输入之后, stdin (您的输入缓冲区)立即包含:

abcd

Those character are waiting to be extracted in whatever manner you choose.这些角色正在等待以您选择的任何方式提取。 Since you are reading from stdin (using std::cin ) using getline() from the stream into a std::string , getline() will consume all characters in the line, reading and discarding the trailing '\n' with all characters stored in your std::string .由于您正在使用stdin中的getline()从标准输入(使用std::cin )读取到std::string ,因此getline()将消耗该行中的所有字符,读取并丢弃尾随'\n'和所有字符存储在您的std::string中。 There are no characters left in stdin that need to be extracted using std::cin.ignore() stdin中没有剩余字符需要使用std::cin.ignore()提取

When you call std::cin.ignore() before getline() you are reading (and discarding) the first character waiting to be read in stdin .当您在getline() )之前调用std::cin.ignore()时,您正在读取(并丢弃)等待在stdin中读取的第一个字符。 With the example characters above, after calling std::cin.ignore() , and extracting and discarding the character, the following is left in stdin :对于上面的示例字符,在调用std::cin.ignore()并提取和丢弃字符后,以下内容留在stdin中:

bcd

So now when you do call getline (std::cin, *st);所以现在当你调用getline (std::cin, *st); "bcd" are the only characters that are available. "bcd"是唯一可用的字符。 That is the reason why you miss the first character.这就是您错过第一个角色的原因。

While you can std::cin.ignore() one character, the .ignore() member function is usually used to read/discard all remaining characters in a line using the form虽然您可以std::cin.ignore()一个字符,但.ignore()成员 function 通常用于使用以下形式读取/丢弃一行中的所有剩余字符

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

Where #include <limits> provides the constants representing the maximum number of the type specified, above <std::streamsize>::max() (eg the maximum value that size_t can represent).其中#include <limits>提供表示指定类型的最大数量的常量,在<std::streamsize>::max()之上(例如size_t可以表示的最大值)。 A large constant is used to ensure the remainder of the line, no matter how long, is consumed and discarded leaving std::cin either empty or with the first character of the next line (in a multi-line buffer) waiting to be read.一个大常量用于确保该行的剩余部分,无论多长,都被消耗和丢弃,留下std::cin为空或下一行(在多行缓冲区中)的第一个字符等待读取.

std::basic_istream::ignore can also read up to a delimiter character reading/discarding less than the whole line. std::basic_istream::ignore也可以读取最多一个定界符,读取/丢弃少于整行。 That can be useful when reading separated fields, or in cases where you use a delimiter with getline() to read less than an entire line.这在读取分隔的字段时很有用,或者在您将定界符与getline()一起使用以读取少于整行的情况下。

You either perform your read operation first and then call ignore() to discard unwanted characters to the end of the line, or you can call ignore() first with either a specified number of characters (default 1 ) or a delimiter to discard the beginning portion of a line up to the first character you want to began your read with.您可以先执行读取操作,然后调用ignore()将不需要的字符丢弃到行尾,或者您可以先使用指定数量的字符(默认1 )或分隔符调用ignore()以丢弃开头一行的一部分,直到您想要开始阅读的第一个字符。

As you have used it above, it will simply chop off the first character of whatever the user enters -- which does not seem to be what you want.正如您在上面使用的那样,它只会截断用户输入的任何内容的第一个字符——这似乎不是您想要的。 In fact, ignore() isn't even needed above.事实上,上面甚至不需要ignore() Remove it and see if the missing character problem doesn't go away.去掉看看go丢字符的问题是不是消失了。

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

相关问题 为什么同时使用两个 getline function,第二个不带 \n 字符? - Why does using two getline function together, second one doesn't take the \n character? 为什么 cin.ignore() 忽略我的 getline 输入的第一个字符? - Why is cin.ignore() ignoring the first character of my getline input? getline 删除第一个字符; 删除 cin.ignore(); 没有解决 - getline removes first character; removal of cin.ignore(); does not resolve 为什么 getline() 会切断 CSV 输入? - Why does getline() cut off CSV Input? 为什么 getline 在 switch 中不起作用,而 cin 没有适当地获取数据? - Why getline does not work in a switch, and cin does not take the data appropriately? "<i>why cursor not go to next line?<\/i>为什么光标不转到下一行?<\/b> <i>When getline() take &quot;\\n&quot; as an input<\/i>当 getline() 将 &quot;\\n&quot; 作为输入<\/b>" - why cursor not go to next line? When getline() take "\n" as an input 为什么getline在使用in循环时会忽略输入的第一个字符? - why getline ignoring first character of input while using in loop? getline之后如何获取输入 - How to take input after getline 为什么cin.getline分配给字符数组,但不使用&#39;=&#39;? - Why does cin.getline assign to character array but using '=' will not? 为什么getline不接受输入 - Why getline not accepting input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM