I created a vector out of a struct to store multiple types of values. However, I can’t get input to work.
#include "std_lib_facilities.h"
struct People{
string name;
int age;
};
int main()
{
vector<People>nameage;
cout << "Enter name then age until done. Press enter, 0, enter to continue.:\n";
People name;
People age;
while(name != "0"){
cin >> name;
nameage.push_back(name);
cin >> age;
nameage.push_back(age);}
vector<People>::iterator i = (nameage.end()-1);
nameage.erase(i);
}
I’ve also tried having the name and age variables in the main function be string/int types, and while that fixes the operator issue, it leads to an issue with function calling in the push_back line.
P.S. Is it possible to push_back multiple inputs such as…
cin >> name >> age;
nameage.push_back(name,age);
?
Why not do:
You can’t just
cin >>p, asistreamdoesn’t understand how to input a “People” object. So you can either defineoperator>>forPeople, or you can just read in the individual fields into a People object.Also, note, you need to
push_backan object of typePeople, as that is what yourvectoris — it is aPeoplecontainer.