这个getline输入为什么会吞我的字符啊?


int main()
{   
    
    string str[200];
    int x = 0;
    do
    {
        getline(cin, str[x++]);
    } while ((cin.get() !='$') && (x<200));

    cout << "content:" << endl;
    for (int i = 0; i < x; i++)
    {   
        
        cout << str[i] <<'\n';

    }
}

运行:

img


我不明白为什么我输入的第二第三个字符串各自减少了一个字符!

你这个很明显的错误啊,get()读走了一个字符啊

#include <iostream>
#include <string>

int main()
{

    std::string str[200];
    int x = 0;
    while (getline(std::cin, str[x++]))
    {
        if (str[x - 1] == "$" || x == 200)
        {
            break;
        }
    }
    
    //    do
    //    {
    //        getline(std::cin, str[x++]);
    //    } while ((std::cin.get() != '$') && (x < 200));

    std::cout << "content:" << std::endl;

    for (int i = 0; i < x; i++)
    {
        std::cout << str[i] << '\n';
    }

    return 0;
}