C++ Getline after Cin

前端 未结 3 1924
忘掉有多难
忘掉有多难 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

    getline() reads whitespaces, if you want to ignore the leading whitespaces try:

    cin.ignore();
    getline(cin, b);
    

    EDIT: Sorry, this indeed reads 1 character, this is another solution for you:

        getline(cin, b);
        string noLeadingWS = b.substr(b.find_first_not_of(' '),b.length()-b.find_first_not_of(' '));
        cout << a << ": " << noLeadingWS<< std::endl;
    

提交回复
热议问题