EDIT: Again, after searching for some time and not finding any answers then asking here and going back to experiment a bit with my code, I figured out the answer just by trial and error :|

Hi!
My current homework assignment requires me to read some info from a file, store the info in structures, store the structures into a vector, sort the info, and let the user do some searches.

Now, I believe I have the input part ready and I'm stuck in the outputting/sorting part. After I store a structure into a vector, how do I read its stored data?

Lets say I have a structure like the following:

struct Info{
    string name;
    double telNum;
    string street;
    string city;
    string state;
    string zipC;
};

and I store the structure into a vector in the following way:

//...
Info person;
vector<Info> People
// ... store data in the structure ...
People.push_back(Person) // Is this the right way 
                         //to store a strc in a vector?
//...

How would I then ouput the structure to the screen?
Should be something easy :)

Thanks

Here is one easy method:

for(int i=0; i<People.size(); i++)
{
    if(People[i].name == target)
    {
          cout << "Person Found:  " << People[i].name 
               <<  People[i].street << People[i].city 
               <<  People[i].zip ;
    }
}

It might be worthwhile to post the solution you arrived at.

I don't know if this is jumping ahead too much, but my suggestion would have been to write

friend std::ostream& operator<< (std::ostream &o, const Info &i)

That way you can output a record much like any primitive. And down the road you can also take advantage of nifty algorithms available for use with the container classes.

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

struct Info
{
   std::string name;
   double telNum;
   std::string street;
   std::string city;
   std::string state;
   std::string zipC;
   Info(std::string name_, double telNum_, std::string street_,
        std::string city_, std::string state_, std::string zipC_)
   : name(name_), telNum(telNum_), street(street_),
     city(city_), state(state_),   zipC(zipC_)
   {
   }
   friend std::ostream& operator<< (std::ostream &o, const Info &i)
   {
      return o << "name   = " << i.name   << '\n'
               << "telNum = " << i.telNum << '\n'
               << "street = " << i.street << '\n'
               << "city   = " << i.city   << '\n'
               << "state  = " << i.state  << '\n'
               << "zipC   = " << i.zipC   << '\n';
   }
};

int main()
{
   const Info Dave("Dave",1234.5678,"Main St.","Center City","MN","12345");
   const Info Jenny("Jenny",867.5309,"Broadway","Spatula City","WY","90210");

   // Show each individually:
   std::cout << Dave  << '\n';
   std::cout << Jenny << '\n';

   // Fill a vector...
   std::vector<Info> data;
   data.push_back(Dave);
   data.push_back(Jenny);
   // ... and output all vector elements:
   std::copy(data.begin(), data.end(), 
             std::ostream_iterator<Info>(std::cout, "\n"));
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.