繁体   English   中英

类的类持有向量

[英]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 ,每个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

Name: Age:eof之后仍然打印,我试图在while循环的开始处放置cin.eof()测试,但是它弄乱了所有输出。 我怎么能擅自修改它显示的代码couteof

输入name后,输入age后,您需要检查eofCtrl+z^Z ))。 如果输入Ctrl+z作为nameeofbit标志将在cin上设置,但直到输出Age:时才被捕获。 它不会因age输入而暂停的原因是:

如果在[ istream ]中到达文件末尾或在输入操作期间发生其他错误,提取也将停止。 -C ++参考-getline

注意:上面引用中的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.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM