C++ Getline after Cin

前端 未结 3 1851
忘掉有多难
忘掉有多难 2021-01-21 22:28

I am trying to write a program which gets user\'s input in a specific way. First, I input a word which contains no space; Then, I input another word which may contains space; An

3条回答
  •  被撕碎了的回忆
    2021-01-21 23:10

    The std::getline function does not skip whitespace like the normal input operator >> does. You have to remove leading (and possible trailing?) whitespace yourself.

    Removing the leading whitespace can be done by first finding the first non-whitespace character (with e.g. std::find_if) and then get a substring from that position to the rest (with std::string::substr).


    Or as dyp suggests, use std::ws. The linked reference have a very good example how to use it.

提交回复
热议问题