Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to iterate through a returned custom class and print it's members

I don’t know how to print a returned value of a custom object

#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <string>

class Person{
public: 
    string name;
    int age; 
    Person(string name, int age){
       this->name = name; 
       this->age = age; 
    }
};


class listOfPeople{
private:
    vector <Person> myList ;
public:
    void fillTheList(Person p){
        myList.push_back(p)
    }
Person findPerson(string name){
        for(Person p : myList){
            if(strcasecmp(p.name.c_str(), name.c_str()) == 0){
                return p; // returns a person
            }
        }
        return {};
    }
};


int main(){
   Person p1("Vitalik Buterin", 30); 
   Person p2("Elon musk", 50); 
   ListOfPeople L; 
   L.fillTheList(p1); 
   L.fillTheList(p2); 
   Person p = L.findPerson("Vitalik"); //  I don't know what to do here( I want to print   Vitalik's information, the full name and age)
   return o; 
}


I don’t know how to print the informations of the returned value. I hope my question is clear. I

tried different things but can’t get the right logic

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you just need

 std::cout << p.name << " is " << p.age <<'/n';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading