I tried here in this code to insert struct information then assign to vector and get the smallest age .. i want to do that with vector to learn more about it .. can anyone help me to fix it .. and explain to me what's wrong in my thought .. thanks
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
struct people
{
string name;
int age;
};
people Read(people p)
{
for (unsigned int i = 0; i < 5; i++){
cout << "name: ";
cin >> p.name;
cout << "age: ";
cin >> p.age;
}
return p;
}
vector<people> getData(people person)
{
vector<people> v;
for (unsigned int i = 0; i < 5; i++){
person = Read(person);
v.push_back(person);
}
return v;
}
int getSmallest(vector<people> v)
{
int smallest = 0;
for (unsigned int i = 0; i < v.size(); i++)
{
if (v.at(i).age < smallest)
smallest = v.at(i).age;
}
return smallest;
}
void print(vector<people> v){
cout << endl;
for (unsigned int i = 0; i < v.size(); i++){
cout << "name: " << v.at(i).name << endl;
cout << "age: " << v.at(i).age << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
people p;
vector<people> v;
cout << "enter names & ages" << endl;
v = getData(p);
print(v);
cout << "the smallest one" << endl;
getSmallest(v);
return 0;
}