C++ application fails - no errors -
i have started learn c++ , causing me headache. have written simple person "database" application , reason fails when listing persons.
string search; cout << "give name: "; cin >> search; vector<person> foundpersons; for(vector<person>::iterator = persons.begin(); != persons.end(); it++) { person p = *it; if(search == p.getfirstname() || search == p.getsurname()) { foundpersons.push_back(p); } } if(!foundpersons.empty()) { cout << "found " << foundpersons.size() << " person(s).\n\n"; cout << "firstname\tsurname\t\tbirth year\n"; } else { cout << "no matches."; } for(vector<person>::iterator = foundpersons.begin(); != persons.end(); it++) { person p = *it; cout << p.getfirstname() << "\t\t" << p.getsurname() << "\t" << p.getbirthyear() << "\n"; } cout << "\n";
persons
type of vector<person>
. go through entries , compare name against given search value. if found, add person in foundpersons
vector. print either no matches
or number of found persons , table header. next go through found persons , print them in console.
if add 2 persons, eg "jack example" , "john example" , search "jack", find "jack" , print it. program stops. windows says "the program has stopped working". no errors shown during compile or when program stops.
what's wrong?
you're loop isn't quite right, looks you've made typo , referencing iterators 2 different lists. change:
for(vector<person>::iterator = foundpersons.begin(); != persons.end(); it++) {
to
for(vector<person>::iterator = foundpersons.begin(); != foundpersons.end(); it++) {
Comments
Post a Comment