【问题标题】:getline and cin used one after another skips next inputsgetline 和 cin 一个接一个使用跳过下一个输入
【发布时间】:2015-08-31 10:22:57
【问题描述】:

我正在编写一个 C++ 程序来演示聚合概念,并在书中作为示例给出。该代码由 4 个类(在一个文件中)和一个主函数(在另一个文件中)组成。 类如下

#include <iostream>
#include <string>
/*#define  SYNC_CLEAR {\
                    cin.clear();\
                    cin.sync();\
                }
*/
using std::string;
using std::cout;
using std::cin;

class Student
{
private:
    string school;
    string degree;
public:
    void input_data()
    {
        cout<<"Enter School name and degree name \n";
        getline(cin,school);
        getline(cin,degree);
    }
    void show_data() const
    {
        cout<<"School :"<<school<<",Degree: "<<degree<<"\n";
    }

};

class Employee
{
private:
    string name;
    unsigned int number;
public:
    void input_data()
    {
        string str;
        cout<<"Enter Name and number \n";
        getline(cin,name);
        cin>>number;
    }
    void show_data() const
    {
        cout<<"Name :"<<name<<",number: "<<number<<"\n";
    }
};


/************************Aggregation starts from here**********************************/
class Manager
{
private:
    string title;
    double dues;
    Employee emp;   // object of one class as an attribute to other class
    Student stu;    // object of one class as an attribute to other class
public:
    **void input_data()
    {
        emp.input_data();   // call by object of Employee class
        cout<<"Enter Title and Dues \n";
        /*********Problem comes here*****************/
        getline(cin,title);
        cin>>dues;
        /**************************/
        stu.input_data();   // call by object of Student class
    }**
    void show_data() const
    {
        emp.show_data();
        cout<<"Title :"<<title<<",Dues: "<<dues<<"\n";
        stu.show_data();
    }   
};

class Scientist
{
private:
    int pubs;
    Employee emp;   // object of one class as an attribute to other class
    Student stu;    // object of one class as an attribute to other class
public:
    void input_data()
    {
        emp.input_data();   // call by object of Employee class
        cout<<"Enter no of pubs \n";
        cin>>pubs;
        stu.input_data();   // call by object of Student class
    }
    void show_data() const
    {
        emp.show_data();
        cout<<" pubs :"<<pubs<<"\n";
        stu.show_data();
    }   
};

主要功能如下:

#include <iostream>
#include "aggregation.h"

using std::cout;
using std::cin;

int main(int argc, char const *argv[])
{
    Manager m1;
    Scientist s1,s2;
    cout<<"Data for Manager\n";
    m1.input_data();

    cout<<"Data for Sci 1\n";
    s1.input_data();

    cout<<"Data for Sci 2\n";
    s2.input_data();

    cout<<"Data for Manager\n";
    m1.show_data();

    cout<<"Data for Sci 1\n";
    s1.show_data();

    cout<<"Data for Sci 2\n";
    s2.show_data();

    return 0;
}

问题出现在 input_data() 函数中的类管理器中(以粗体突出显示)。我在控制台附加输出

hduser@M-1939:~/Dropbox/c++/lafore/inhertiance$ g++ -g aggregation.cpp -o aggregation
hduser@M-1939:~/Dropbox/c++/lafore/inhertiance$ ./aggregation 
Data for Manager
Enter Name and number 
vivek 
100
***Enter Title and Dues 
sr manager***
Enter School name and degree name 
Data for Sci 1
Enter Name and number 
Enter no of pubs 
Enter School name and degree name 
Data for Sci 2
Enter Name and number 
Enter no of pubs 
Enter School name and degree name 
Data for Manager
Name :vivek,number: 100
Title :,Dues: 0
School :,Degree: 
Data for Sci 1
Name :,number: 6299824
 pubs :6299824
School :,Degree: 
Data for Sci 2
Name :,number: 2092888488
 pubs :6299112
School :,Degree: 

我查看了一些使用 cin.clear() 和 cin.sync() 的链接,但效果不佳。有问题,不是这样,不同但仍然是类似的问题。 有什么线索吗?

EDIT 1 : 我使用将输入序列从 1 : getline 2: cin 更改为 1:cin 2: cin.ignore() 3: getline 并且它正在工作。但是谁能告诉我怎么做?为什么它不能以以前的方式工作。

编辑 2:我忘记在 Employee 类的 input_data() 中的 cin>>number 之后放置 cin.ignore() 。有没有让它工作。虽然是个愚蠢的错误。

【问题讨论】:

    标签: c++ inheritance aggregation cin getline


    【解决方案1】:

    用途:

    cin.ignore(256,'\n');
    

    在每次 cin 调用后清理缓冲区。

    256 是要忽略的字符数,'\n' 是分隔符。

    为了安全起见,可以使用

    std::numeric_limits<std::streamsize>::max()
    

    作为字符的限制数量;我只是喜欢 256。

    http://www.cplusplus.com/reference/istream/istream/ignore/

    【讨论】:

      【解决方案2】:

      您需要在cin 之后添加cin.ignore()
      这就是发生的事情:
      当您使用std::getline 时,它将获取该行上的任何内容,直到换行符为止,但cin 在执行读取后实际上并没有删除换行符,因此您的std::getline 只会得到一个空字符串。

      【讨论】:

      • 还是不行。顺便说一句,问题在我输入“title”后就开始了,在给定的问题中,“sr manager”
      猜你喜欢
      • 2014-04-01
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 2013-04-12
      • 1970-01-01
      • 2015-07-08
      相关资源
      最近更新 更多