2

I am learning OOP in C++ so I tried to practice a little. 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 )

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:

int main() {
    Trieda newClass("4A","Ms Foster");

    newClass.fillStudents();
    newClass.getNamesOfStudents();

    return 0;
}

My problem here is the method fillStudents() output starts pretty nice:

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.

I tried to just use cin >> name instead of getline, e.g:

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. How can I implement it with getline (I wanted full name as name input e.g John Wicked).

I am trying to figure it out but I'm just a beginner with C++ so my knowledge is limited.

Edit

I fixed it using cin.get() as advised from question posted in comments, now my function looks like this:

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. 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. How can I modify the code without it showing the cout after eof?

Jonny Henly
  • 3,725
  • 4
  • 23
  • 41

1 Answers1

0

You need to check for eof (Ctrl+z(^Z)) after name has been entered and after age has been entered. If you enter Ctrl+z for name then the eofbit flag will be set on cin but not caught until after outputting Age:. The reason it doesn't pause for age input is because:

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.

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.
Jonny Henly
  • 3,725
  • 4
  • 23
  • 41