0

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;
}
2
Contributors
2
Replies
19
Views
9 Months
Discussion Span
Last Post by rose_2
Featured 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 < … Read More

1

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;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.