[英]Class holding vector of classes
I am learning OOP in C++ so I tried to practice a little. 我正在用C ++学习OOP,所以我尝试了一些练习。 I want to create a "database" (using objects) of class and its students.
我想创建班级及其学生的“数据库”(使用对象)。 I created a class for a student.
我为一个学生开设了一堂课。 Each have a name and age.
每个人都有名字和年龄。
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;
}
And I created a class of a Classroom
, each is represented by its name , teacher and students ( vector of objects ) 我创建了一个
Classroom
,每个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;
}
and I am just calling its methods in main
: 我只是在
main
调用其方法:
int main() {
Trieda newClass("4A","Ms Foster");
newClass.fillStudents();
newClass.getNamesOfStudents();
return 0;
}
My problem here is the method fillStudents()
output starts pretty nice: 我的问题是方法
fillStudents()
输出开始时非常漂亮:
Name: // insert name
Age : // insert age
but the second iteration looks worse: 但是第二次迭代看起来更糟:
Name:Age: // insert something
and the third iteration is an infinite loop printing Name:Age:
till the end of the world. 第三次迭代是无限循环打印
Name:Age:
直到世界尽头。
I tried to just use cin >> name
instead of getline
, eg: 我试图只使用
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();
}
}
It works. 有用。 But it works like this:
但是它是这样的:
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
What causes this? 是什么原因造成的? I suspect it could be something with buffer not being cleared but I tried
cin.clear()
and it happened too. 我怀疑这可能是缓冲区未清除的问题,但是我尝试了
cin.clear()
,它也发生了。 How can I implement it with getline
(I wanted full name as name input eg John Wicked). 如何使用
getline
来实现它(我想要全名作为名称输入,例如John Wicked)。
I am trying to figure it out but I'm just a beginner with C++ so my knowledge is limited. 我试图弄清楚,但我只是C ++的初学者,所以我的知识有限。
Edit 编辑
I fixed it using cin.get()
as advised from question posted in comments, now my function looks like this: 我按照评论中发布的问题使用
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();
}
}
What still makes me uncomfortable is the output with cout
. 仍然让我感到不舒服的是
cout
的输出。 It produces this: 它产生此:
Name: John
Age: 15
Name: Lia
Age: 25
Name: Age: hey John
hey Lia
The Name: Age:
still gets printed after eof
, I tried to put cin.eof()
test at the start of the while loop but it messed up all of the output. Name: Age:
在eof
之后仍然打印,我试图在while循环的开始处放置cin.eof()
测试,但是它弄乱了所有输出。 How can I modify the code without it showing the cout
after eof
? 我怎么能擅自修改它显示的代码
cout
后eof
?
You need to check for eof
( Ctrl+z
( ^Z
)) after name
has been entered and after age
has been entered. 输入
name
后,输入age
后,您需要检查eof
( Ctrl+z
( ^Z
))。 If you enter Ctrl+z
for name
then the eofbit
flag will be set on cin
but not caught until after outputting Age:
. 如果输入
Ctrl+z
作为name
则eofbit
标志将在cin
上设置,但直到输出Age:
时才被捕获。 The reason it doesn't pause for age
input is because: 它不会因
age
输入而暂停的原因是:
The extraction also stops if the end of file is reached in [the
istream
] or if some other error occurs during the input operation.如果在[
istream
]中到达文件末尾或在输入操作期间发生其他错误,提取也将停止。 - C++ Reference - getline-C ++参考-getline
Note: The
istream
in the quote above iscin
in your code.注意:上面引用中的
istream
在您的代码中为cin
。
Your code should resemble: 您的代码应类似于:
// 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
Output: 输出:
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.