clemente aranguiz
clemente aranguiz

Reputation: 39

how can i access an element of a vector of structures

I'm doing this simple exercise where I need to create a list of books and then remove or add them... the problem is that for some reason I can't access the structure that I made. PD: I tried with arrays and it worked perfect, but I need dynamic memory because I don't know how many books they may want to put in the list. I am very noob so forgive me if this sounds stupid.

#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct book{
    string name;
    string autor;
    string type;
};

void addbook(vector<book> x){
    x.push_back(book());
    cout<<"the name of the book"<<endl;
    cin>>x.back().name;
    cout<<"the name of the autor"<<endl;
    cin>>x.back().autor;
    cout<<"the type of the book"<<endl;
    cin>>x.back().type;
}
int main() {;
    vector<book> list;
    addbook(list);
    cout<<list[0].name;
    return 0;
}

Upvotes: 2

Views: 116

Answers (3)

kadina
kadina

Reputation: 5376

There is global variable with name 'lista' and you are using the same name for another vector in main. If you declare a variable with the same name as global variable inside a function, the local variable hides global variable inside the function. So your main() is referring to local variable but LibroALeer() function is referring to global variable. That means you are pushing the values to global vector inside LibroALeer() and you are trying to print local variable inside main() which doesn't have any value inside the vector.

Please refer below code to fix the problem

#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct book{
    string name;
    string autor;
    string type;
};

void addbook(vector<book> &x){
    x.push_back(book());
    cout<<"the name of the book"<<endl;
    cin>>x.back().name;
    cout<<"the name of the autor"<<endl;
    cin>>x.back().autor;
    cout<<"the type of the book"<<endl;
    cin>>x.back().type;
}
int main() {;
    vector<book> list;
    addbook(list);
    cout<<list[0].name;
    return 0;
}

In your code, you are passing the vector using 'pass by value' which means a copy of the vector is created for addbook() function. So you are pushing the contents to the copy not to the original vector. To fix this issue, you need to use 'pass by reference' which means copy won't be created.

void addbook(vector<book> x) // pass by value
void addbook(vector<book> &x) // pass by reference

Upvotes: 2

ObliteratedJillo
ObliteratedJillo

Reputation: 5166

Avoid using global variable. You are traversing multiple times in LibroALeer(), it can be improved like so:

//vector<libro> lista;  //Avoid global variable

void LibroALeer(vector<libro>& lista){
    libro libro1;        
    cout << "Introduzca el nombre del libro"<<endl;
    cin >> libro1.nombre;
    cout << "Introduzca el nombre del autor"<<endl;
    cin >> libro1.autor;
    cout << "Introduzca el nombre del genero del libro"<<endl;
    cin >> libro1.genero;
    lista.push_back(libro1);
}
int main() {
    vector<libro> lista;
    LibroALeer(lista);
    cout<<lista[0].nombre;
    return 0;
}

Upvotes: 2

jkb
jkb

Reputation: 2450

You have global lista and another one local to main. LibroALeer() is using the global one, main is using the local one. If you get rid of the local one, things should work.

Upvotes: 4

Related Questions