Garmaine Staff asked 1 year ago

I'm working through an exercise in which I have to create a Name_pairs class consisting of a vector name and vector age, and populate these based on input from the user.

My main() is as follows:

'''

int main() {
    Name_pairs np;
    np.read_names();
    np.read_ages();
    np.print();

        return 0;
}

'''

So far my book has taught me to do a use a for-loop for this, and to exit by hitting ctrl+z. This is the code I have so far

'''

void Name_pairs::read_names(){
    std::cout <<"Enter a list of names: ";

    for(std::string nl; std::cin>>nl;)
        name.push_back(nl);
}

'''

I would then type my names in (ex. Bob, Linda, Gene), enter key, then ctrl+z to escape the loop. What I expected to happen was that I would return to main(), then my next function read_ages() would be called, with similar code

'''

void Name_pairs::read_ages(){
    std::cout<<"Enter a list of ages: ";
    for(double uages; std::cin>>uages;)
        age.push_back(uages);
}

''' However, my program seems to terminate upon ctrl+z. My questions really are: 1. Is this type of for-loop good practice? Or is it just being presented to get other points across for a beginner? 2. Why does my program seem to terminate once ctrl+z is entered?