c++ - 类的类持有 vector

标签 c++ oop c++11

我正在学习 C++ 中的 OOP,所以我试着练习了一下。我想创建一个类(class)及其学生的“数据库”(使用对象)。我为学生创建了一个类(class)。每个人都有姓名和年龄。

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 类,每个类都由其名称、教师和学生(对象 vector )表示

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

Name: Age: 仍然在 eof 之后打印,我试图在开始时放置 cin.eof() 测试while 循环但它搞砸了所有的输出。如何在 eof 之后不显示 cout 的情况下修改代码?

最佳答案

name 后,您需要检查 eof (Ctrl+z(^Z))输入后 age 已输入。如果您为 name 输入 Ctrl+z,则 eofbit 标志将在 cin 上设置,但直到之后才会被捕获输出 Age:。它不为 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. - C++ Reference - getline

Note: The istream in the quote above is cin in your code.

您的代码应该类似于:

// 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.

关于c++ - 类的类持有 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35757422/

相关文章:

c++ - 在多个线程中使用 std::cout

c++ - 对于构造函数,{} 是否与 =default 相同?

php - 如何安全地将数据库连接变量传递到新页面?

java - 函数应该以最小范围声明吗?

c++ - std::rethrow_exception 因嵌套线程而失败

c++ - 使用信号和槽更新指针

java - 为什么 switch 或 if else 语句不被认为是多态的?

linux - 找不到 AX_CXX_COMPILE_STDCXX_11 宏

c++ - C++ 中接口(interface)的有效替代

c++ - 当 InProcServer32 键定义 DLL 的完整路径时,为什么 PATH 环境变量中需要 COM DLL 的路径?