Vector using structure datatype

Im having an extremely hard time finding a solution to this, I've looked on youtube and google but have not found a solution. With the followign code I have been unable to do the following:

1. Add information to a vector using the person datatype.
2. Creating a new element within the vector (pushback) once the information has been entered.

Thanks in advanced for your help.

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
  #include <iostream>
#include <vector>
#include <string>
#include <vector>

using namespace std;

struct person
{
    string firstname;
    string lastname;
    string email;
    string id;

};


int main()
{
    vector<person> p;


    cout << "First name: ";
   cin >> p.firstname;
   cout << "\n";
   cout << "Last Name: ";
    cin >> p.lastname;
     cout << "\n";
    cout << "Email: ";
    cin >> p.email;
     cout << "\n";
    cout << "ID: ";
    cin >> p.id;
     cout << "\n";
    p.push_back({p});


    for (int i =0; i < 5; i++)
    {
        cout << p[i] << endl;
    }

}



The error I am getting is std::vector<person> has no member named 'firstname', 'lastname' and so on.
Hello yup2

line 20 vector<person> p;
you're defining and empty vector, it equates to an empty array, you have to push back a member into the vector before you can call it.

You can never use p.firstname you have to specify which member of the vector you're referring to with []

in your case you could do that:

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
#include <iostream>
#include <vector>
#include <string>
#include <vector>

using namespace std;

struct person
{
    string firstname;
    string lastname;
    string email;
    string id;

};


int main()
{
    vector<person> p;

//hold temporary data to push back to p vector
    person tempPerson;

    cout << "First name: ";
    cin >> tempPerson.firstname;
    cout << "Last Name: ";
    cin >> tempPerson.lastname;
    cout << "Email: ";
    cin >> tempPerson.email;
    cout << "ID: ";
    cin >> tempPerson.id;
    
    p.push_back(tempPerson);


    for (int i =0; i < p.size(); i++)
    {
       cout << "\n\nFirst name: " << p[i].firstname
       << "\nLast name: " << p[i].lastname
       << "\nEmail: " << p[i].email
       << "\nID: " << p[i].id;
    }
}


Note that line 40 cout << p[i] << endl; p doesn't have an overloaded operator <<, so if you want you can either make a function to do so in the struct or like I did, display each of the variable one after the other.

Hope this helps

EDIT: also, line 38: for (int i =0; i < 5; i++), this is going to crash your program if your vector doesn't have at least 5 members, which in your case doesn't. You can use .size() for vectors to return the size of the vector, therefore not attempting to access data that isn't allocated
Last edited on
Thanks a ton, you just cleared up a concept that I was looking into for hours. It work's perfectly.
My pleasure! glad to have helped!
Topic archived. No new replies allowed.