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;
}

Recommended Answers

All 2 Replies

I think there shouldn't be a for loop in Read(). Also there is an error in get_smallest()

#include <cassert>
#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)
{
    assert(v.size());
    int smallest = v.at(0).age;
    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 main(int argc, char* argv[])
{
    people p;
    vector<people> v;
    cout << "enter names & ages" << endl;
    v = getData(p);
    print(v);
    cout << "the smallest one" << endl;
    cout << getSmallest(v) << endl;

    return 0;
}

Oh !! I got it .. thanks , it helps alot

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.