使用 getline() 读取空格不起作用

Reading whitespace with getline() doesn't work

本文关键字:空格 不起作用 读取 getline 使用      更新时间:2023-10-16
#include <iostream>
#include <stdlib.h>
using namespace std;
class sheet
{
    public:
        void setname();
        string getname();
    private:
        string name;
};
void sheet::setname()
{
        cout << ">" ;
        getline(cin,name);
        return ;
}

我有一个像^这样的代码。在main函数中我有

sheet Personel;
Personel.setname();

但是当我运行我的代码,它打印'>',但getline不工作。有人能帮我吗?

啊,我解决了。它读取之前main中剩余的'n'。