类的类保持向量
Class holding vector of classes
我正在C++学习OOP,所以我试着练习一下。我想创建一个班级及其学生的"数据库"(使用对象)。我为学生创建了一个课程。每个人都有名字和年龄。
class Ziak {
public:
Ziak(int,string);
int getAge() {
return age;
}
string getName() {
return name;
}
private:
int age;
string name;
};
Ziak::Ziak(int age,string name) {
this->age=age;
this->name=name;
}
并且我创建了一个Classroom
类,每个类都由它的名字、老师和学生(对象的向量)表示
class Trieda {
public:
Trieda(string,string);
void fillStudents() {
while(1) {
int age;
string name;
cout << "Name: ";
getline(cin,name);
cout << "Age: ";
cin >> age;
Ziak newStudent(age,name);
n.push_back(newStudent);
if(cin.eof()) {
break;
}
}
}
string getTeacher() {
return ucitel;
}
string getNamesOfStudents() {
for(unsigned i = 0; i < n.size(); i++) {
cout << "hey " << n[i].getName() << endl;
}
}
protected:
vector<Ziak> n;
string nazov;
string ucitel;
};
Trieda::Trieda(string nazov,string ucitel) {
this->nazov = nazov;
this->ucitel = ucitel;
}
我只是在main
中调用它的方法:
int main() {
Trieda newClass("4A","Ms Foster");
newClass.fillStudents();
newClass.getNamesOfStudents();
return 0;
}
我的问题是输出开始的方法fillStudents()
非常好:
Name: // insert name
Age : // insert age
但第二次迭代看起来更糟:
Name:Age: // insert something
第三次迭代是无限循环打印Name:Age:
直到世界末日。
我尝试只使用cin >> name
而不是getline
,例如:
void fillStudents() {
while(1) {
int age;
string name;
cout << "Name: ";
cin >> name;
/*getline(cin,name);*/
cout << "Age: ";
cin >> age;
if(cin.eof()) {
break;
} else {
Ziak newStudent(age,name);
n.push_back(newStudent);
}
cin.clear();
}
}
它有效。但它的工作原理是这样的:
Name: // insert name
Age : // insert age
// it works till the last one when i press ctrl+Z as eof (currently on windows)
// it outputs Age: Hey + first name ....
Hey name 2 // and so on , then it crashes
这是什么原因造成的?我怀疑这可能是缓冲区未清除的东西,但我尝试了cin.clear()
,它也发生了。我怎样才能用getline
实现它(我想要全名作为名称输入,例如 John Wicked)。
我正在努力弄清楚,但我只是一个C++初学者,所以我的知识有限,所以我的知识有限。
编辑
我按照评论中发布的问题的建议使用cin.get()
修复了它,现在我的函数如下所示:
void fillStudents() {
while(1) {
int age;
string name;
cout << "Name: ";
//cin >> name;
getline(cin,name);
cout << "Age: ";
cin >> age;
if(cin.eof()) {
break;
} else {
if(cin.fail()) {
cout<< "chyba ";
break;
}
Ziak newStudent(age,name);
n.push_back(newStudent);
}
cin.get();
}
}
仍然让我不舒服的是cout
的输出.它产生这个:
Name: John
Age: 15
Name: Lia
Age: 25
Name: Age: hey John
hey Lia
eof
后Name: Age:
仍然被打印出来,我试图cin.eof()
while 循环的开头进行测试,但它弄乱了所有输出。如何在不显示eof
后cout
的情况下修改代码?
您需要在输入name
和输入age
后检查eof
(Ctrl+z
(^Z
))。如果您输入 Ctrl+z
for name
则 eofbit
标志将设置为cin
,但直到输出 Age:
后才会被捕获。它不会暂停age
输入的原因是:
如果在 [
istream
] 中到达文件末尾或在输入操作期间发生其他错误,则提取也会停止。 - C++ 参考资料 - 获取线注意:上面引用中的
istream
cin
在您的代码中。
您的代码应类似于:
// above code removed for brevity
cout << "Name: ";
getline(cin,name);
if(cin.eof()) {
break;
}
cout << "Age: ";
cin >> age;
if(cin.eof()) {
break;
} else {
// below code removed for brevity
输出:
Tested on Windows with CodeBlocks and GCC.
Name: ^Z[Enter]
Process returned 0 (0x0) execution time : 1.685 s
Press any key to continue.
Name: John
Age: ^Z [Enter]
Process returned 0 (0x0) execution time : 1.685 s
Press any key to continue.
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- 一对向量构造函数:初始值设定项列表与显式构造
- 将结构向量排序为子组
- 在C++中调整向量中的索引
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么