为什么getline()函数在这里不起作用?

时间:2020-04-24 06:09:34

标签: c++

我是编程的初学者。在学习的同时尝试编写下面的代码。问题是,当我尝试获取 gamar的姓氏时,它不起作用,只是跳过了 getline()所在的行。但是,当我评论 getline()上方的行时,它可以工作。我在哪里犯错?

#include <iostream>
#include <string>
#include "class.h"
using std::cout;
using std::endl;
using std::cin;
using std::string;

int main(){
    Person gamar;
    cout<<"Enter Gamar's height: ";
    cin>>gamar.height;
    cout<<"Enter Gamar's weight: ";
    cin>>gamar.weight;
    cout<<"Enter Gamar's surname: ";

    getline(cin,gamar.surname );


    cout<<"Gamar's surname: "<<gamar.surname<<endl;
    cout<<"Gamar's height: "<<gamar.height<<endl;
    cout<<"Gamar's weight: "<<gamar.weight<<endl;  


}

这是头文件:

using std::string;


class Person{
    public:
        string surname;
        int weight;
        int height;
};

0 个答案:

没有答案
相关问题