Я пишу программу на C++, чтобы продемонстрировать концепцию Aggregation, приведенную в качестве примера в книге. Код состоит из 4 классов (в файле) и основной функции (в другом файле). классы являютсяgetline и cin, используемые один за другим, пропускают следующие входы
#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 функцию() (выделено жирным шрифтом). Я прилагаю выход на консоли
[email protected]:~/Dropbox/c++/lafore/inhertiance$ g++ -g aggregation.cpp -o aggregation
[email protected]:~/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 to 1: cin 2: cin.ignore() 3: getline и работает. Но может ли кто-нибудь сказать мне, как? Почему это не сработало по-прежнему.
EDIT 2: Я забыл поместить cin.ignore() после cin >> number в input_data() класса Employee. Это заработало. Хотя глупая ошибка.
все еще не работает. Кстати, проблема начинается сразу после ввода «title», в заданном вопросе «sr manager», – vvs14