Creating a list

Hi,
I'm trying to create a list where the user inputs a first name and a last name to the list. After the user feels satisfied with the number of persons in the list the program will go back to the menu. And now comes the problem that I have, when I choose to "Write out the list" the list is completely empty regardless of the number of persons I put in. Maybe the only way to do this is to save all the names to a file?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream> // cout, cin
#include <iomanip> // setw, left, right
#include <string> // strängar
#include <vector> // std::vector
using namespace std;

bool yesOrNo(string str);

struct Person
{
    string firstname;
    string lastname;
};

void addname();
void coutlist();


int main()
{
    bool again = true;
    char ch;
    do
    {
        cout << endl << "Programmenu" << endl << endl;
        cout << "1 Add name" << endl;
        cout << "2 Write out the list" << endl;
        cout << "3 End" << endl;
        cin >> ch;
        cin.clear();
        cin.ignore(1000, '\n');
        
        
        switch(ch)
        {
            case '1':
                addname();
                break;
            case '2':
                coutlist();
                break;
            case '3':
                again = false;
            default:
                break;
        }
    }while(again);
    
    return 0;
}

//----------------------------------------------------------------------------
//Write out list
//----------------------------------------------------------------------------

void coutlist()
{
    vector<Person> persons;
    
    cout << endl << "First name, Last name" << endl<<endl;
    for(auto e : persons)
    {
        cout << left << setw(20) << e.firstname;
        cout << setw(20) << e.lastname;
    }
    cout << endl;
}

//----------------------------------------------------------------------------
//Add name
//----------------------------------------------------------------------------

void addname()
{
    vector<Person> persons;

    do
    {
        Person tmpPerson;
        cout << endl;
        cout << "Input data for a person!" << endl;
        cout << "Name : ";
        getline(cin,tmpPerson.firstname);
        cout << "Lastname : ";
        getline(cin,tmpPerson.lastname);
        
        
    }while(yesOrNo("One more person? (y/n): "));
    
    return;
}

bool yesOrNo(string str)
{
    cout << str;
    char ch;
    do
    {
        cin >> ch;
        cin.get();
        ch=toupper(ch);
    }while(!(ch=='Y'||ch=='N'));
    return (ch=='Y');
};
Currently the vector<Person> in coutlist() and the vector<Person> in addname() don't know about each other because they are both defined within their respective functions and go out of scope when these functions return.

You need to declare a common vector<Person> in main() and push_back this vector with each person created (which becomes the addname() function) and then pass this vector<Person> to coutlist() when it comes to printing the Person objects.

Incidentally, line 61, you should pass by reference and line 31, instead of magic numbers use:
1
2
#include <limits>
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
@gunnerfunner how do I get it to push back with each person created, in the main function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream> // cout, cin
#include <iomanip> // setw, left, right
#include <string> // strängar
#include <vector> // std::vector
using namespace std;

bool yesOrNo(string str);

struct Person
{
    string firstname;
    string lastname;
};

void addname(vector<Person>& persons);
void coutlist(vector<Person> persons);


int main()
{
    vector<Person> persons;
    bool again = true;
    char ch;
    do
    {
        cout << endl << "Programmenu" << endl << endl;
        cout << "1 Add name" << endl;
        cout << "2 Write out the list" << endl;
        cout << "3 End" << endl;
        cin >> ch;
        cin.clear();
        cin.ignore(1000, '\n');


        switch(ch)
        {
            case '1':
                addname(persons);
                break;
            case '2':
                coutlist(persons);
                break;
            case '3':
                again = false;
            default:
                break;
        }
    }while(again);

    return 0;
}

//----------------------------------------------------------------------------
//Write out list
//----------------------------------------------------------------------------

void coutlist(vector<Person> persons)
{
    cout << endl << "First name, Last name" << endl<<endl;
    for(auto& e : persons)
    {
        cout << left << setw(20) << e.firstname;
        cout << setw(20) << e.lastname;
    }
    cout << endl;
}

//----------------------------------------------------------------------------
//Add name
//----------------------------------------------------------------------------

void addname(vector<Person>& persons)
{
    do
    {
        Person tmpPerson;
        cout << endl;
        cout << "Input data for a person!" << endl;
        cout << "Name : ";
        getline(cin,tmpPerson.firstname);
        cout << "Lastname : ";
        getline(cin,tmpPerson.lastname);
        persons.push_back(move(tmpPerson));


    }while(yesOrNo("One more person? (y/n): "));

    return;
}

bool yesOrNo(string str)
{
    cout << str;
    char ch;
    do
    {
        cin >> ch;
        cin.get();
        ch=toupper(ch);
    }while(!(ch=='Y'||ch=='N'));
    return (ch=='Y');
};

Last edited on
main thing is to pass the vector by reference to adddname() otherwise it just populates a copy of persons and when you go back to print out persons there'd be nothing in there of course
Thank you @gunnerfunner !
I have one more question @gunnerfunner

If I want to search for a specific name or ID in the list there are a few different ways
http://stackoverflow.com/questions/14225932/search-for-a-struct-item-in-a-vector-by-member-data

I'm trying to implement it to my code but I can't quite figure it out here is my try,

1
2
3
4
5
6
7
8
9
10
11
12
13
void search(vector<Person> persons)
{
    string searchName;
    
    cout << "Enter a name you want to search for: ";
    cin >> searchName;
    
    auto pred = [searchName](const Person & item) 
    {
        return item.firstname == searchName;
    };
    std::find_if(std::begin(persons), std::end(persons), pred) != std::end(persons);
}


It seems like the pred works a bit like a if statement here that returns the name. Could I add else then? Something like this,
1
2
3
4
else 
{
     cout << "Name not in the list!"; 
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void searchperson(const vector<Person>& persons)
{
    string searchName;
    cout << "Enter first name to search by\n";
    cin >> searchName;
    auto pred = [searchName](const Person & item)
    {
        return item.firstname == searchName;
    };

    auto itr = std::find_if(std::begin(persons), std::end(persons), pred);
    if( itr!= std::end(persons))
    {
        cout << "Search item found, person first-name: " << searchName << " has last name: " << itr->lastname <<'\n';
    }
    else
    {
        cout << "Search item not found \n";
    }
}
Topic archived. No new replies allowed.