Problem with class template vector c ++ [duplicated]

0

Good evening I have a mess with a C ++ code use the Vector class template to enter the number of data dao by the user, what happens is that you use one function to enter the data and another to show data, the error or the exception I get when calling the function showData () the following error

This is the exception that comes out

and this is part of the code

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;

void ingresoDatos(vector <string> nombreEquipo,
                    vector <string> nombreJugador,
                    vector <string> cedula,
                    vector <string> telefono,
                    vector <string> direccion,
                    vector <int> edad,
                    vector <double> estatura,
                    vector <double> costoJugador, int numDatos)//argumentos
{
    string nEquipo;
    string nJugador;
    string ci;
    string tlf;
    string direcc;
    int ed;
    double altura, costo;

    for (int i = 0; i < numDatos; i++)
    {
        cout << setw(20) << " JUGADOR NUMERO" << i + 1 << " : "<<endl;
        cout << endl;
        cin.get();
        cout << "\tNOMBRE DEL EQUIPO : ";
        getline(cin, nEquipo);
        nombreEquipo.push_back(nEquipo);
        cout << endl;
        cout << "\tNOMBRE DEL JUGADOR : ";
        getline(cin, nJugador);
        nombreJugador.push_back(nJugador);
        cout << endl;
        cout << "\tEDAD : ";
        cin >> ed;
        edad.push_back(ed);
        cout << endl;
        cout << "\tESTATURA : ";
        cin >> altura;
        estatura.push_back(altura);
        cout << endl;
        cin.get();
        cout << "\tNUMERO DE IDENTIFICACION : ";
        getline(cin, ci);
        cedula.push_back(ci);
        cout << endl;
        cout << "\tNUMERO DE TELEFONO MOVIL: ";
        getline(cin, tlf);
        telefono.push_back(tlf);
        cout << endl;
        cout << "\tDIRECCION : ";
        getline(cin, direcc);
        direccion.push_back(direcc);
        cout << endl;
        cout << "\tCOSTO DEL JUGADOR (dolares) : ";
        cin >> costo;
        costoJugador.push_back(costo);
        cout << endl;
    }


}

void mostrarDatos(vector <string> nombreEquipo,
    vector <string> nombreJugador,
    vector <string> cedula,
    vector <string> telefono,
    vector <string> direccion,
    vector <int> edad,
    vector <double> estatura,
    vector <double> costoJugador, int numDatos)
{
    cout << numDatos;
    cin.get();
    for (int i = 0; i < numDatos; i++)
    {
        cout << setw(20) << " JUGADOR NUMERO" << i + 1 << " : " << endl;
        cout << "NOMBRE DEL EQUIPO : " << nombreEquipo.at(i) << endl;
        cout << "NOMBRE DEL JUGADOR : " << nombreJugador.at(i) << endl;
        cout << "EDAD : " << edad.at(i) << endl;
        cout << "ESTATURA : " << estatura.at(i) << endl;
        cout << "IDENTIFICACION : " << cedula.at(i) << endl;
        cout << "TELEFONO : " << telefono.at(i) << endl;
        cout << "DIRECCION : " << direccion.at(i) << endl;
        cout << "VALOR EN EL MERCADO : " << costoJugador.at(i) << endl;
        cout << endl;
    }
}



int main()
{
    vector <string> nombreEquipo;
    vector <string> nombreJugador;
    vector <string> cedula;
    vector <string> telefono;
    vector <string> direccion;
    vector <int> edad;
    vector <double> estatura;
    vector <double> costoJugador;
    int numDatos;

    cout << "Cuantos datos va a ingresar" << endl;
    cin >> numDatos;
    cout << endl;
    ingresoDatos(nombreEquipo, nombreJugador, cedula, telefono,
                direccion, edad, estatura, costoJugador, numDatos);
    cout << endl;
    mostrarDatos(nombreEquipo, nombreJugador, cedula, telefono,
                direccion, edad, estatura, costoJugador, numDatos);
    _getch();
    return 0;
}
    
asked by Erik Andres 27.07.2018 в 02:52
source

1 answer

1

The main error you have is caused by not understanding that the ingresoDatos() function is passing you the vectors by value, that is, the vectors that are in the main are different from the vectors that are in this function, a form of To solve could be to pass them by reference but in this case I see it unnecessary. For example, your vectors in main have size 0, when you finish executing the function DataIngress () they will continue to have size 0 since you have filled other vectors, so when printing these assuming they have a size other than 0 accessing non-reserved memory and generating that problem.

Another problem is that you are creating many vectors, and this is a design problem, the idea is that you create a struct that stores the information of a single element, and then you create a vector of those elements.

Finally to know the size of a vector you must use the function size() .

A better approach is that the data input function () only has the data number as input, and as output the vector, the showData () function must have the vector as input and it is not necessary to use data as it is necessary to do so. will use the size () function:

#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <vector>
#include <string>
#include <iomanip>

struct Datos{
    std::string nombreEquipo;
    std::string nombreJugador;
    std::string cedula;
    std::string telefono;
    std::string direccion;
    int edad;
    double estatura;
    double costoJugador;
};

std::vector<Datos> ingresoDatos(size_t numDatos)//argumentos
{
    std::vector<Datos> datos;
    std::string nEquipo, nJugador,ci, tlf, direcc;
    int ed;
    double altura, costo;
    for (size_t i = 0; i < numDatos; i++)
    {
        Datos dato;
        std::cout << std::setw(20) << " JUGADOR NUMERO" << i + 1 << " : "<<"\n";
        std::cout << "\n";
        std::cin.get();
        std::cout << "\tNOMBRE DEL EQUIPO : ";
        getline(std::cin, nEquipo);
        dato.nombreEquipo = nEquipo;
        std::cout << "\n";
        std::cout << "\tNOMBRE DEL JUGADOR : ";
        getline(std::cin, nJugador);
        dato.nombreJugador = nJugador;
        std::cout << "\n";
        std::cout << "\tEDAD : ";
        std::cin >> ed;
        dato.edad = ed;
        std::cout << "\n";
        std::cout << "\tESTATURA : ";
        std::cin >> altura;
        dato.estatura = altura;
        std::cout << "\n";
        std::cin.get();
        std::cout << "\tNUMERO DE IDENTIFICACION : ";
        getline(std::cin, ci);
        dato.cedula = ci;
        std::cout << "\n";
        std::cout << "\tNUMERO DE TELEFONO MOVIL: ";
        getline(std::cin, tlf);
        dato.telefono = tlf;
        std::cout << "\n";
        std::cout << "\tDIRECCION : ";
        getline(std::cin, direcc);
        dato.direccion = direcc;
        std::cout << "\n";
        std::cout << "\tCOSTO DEL JUGADOR (dolares) : ";
        std::cin >> costo;
        dato.costoJugador = costo;
        std::cout << "\n";
        datos.push_back(dato);
    }
    return datos;
}

void mostrarDatos(std::vector <Datos> datos)
{
    std::cin.get();
    for (size_t i = 0; i < datos.size(); i++)
    {
        Datos dato = datos.at(i);
        std::cout << std::setw(20) << " JUGADOR NUMERO" << i + 1 << " : " << "\n";
        std::cout << "NOMBRE DEL EQUIPO : " << dato.nombreEquipo << "\n";
        std::cout << "NOMBRE DEL JUGADOR : " << dato.nombreJugador << "\n";
        std::cout << "EDAD : " << dato.edad << "\n";
        std::cout << "ESTATURA : " << dato.estatura << "\n";
        std::cout << "IDENTIFICACION : " << dato.cedula << "\n";
        std::cout << "TELEFONO : " << dato.telefono << "\n";
        std::cout << "DIRECCION : " << dato.direccion << "\n";
        std::cout << "VALOR EN EL MERCADO : " << dato.costoJugador << "\n";
        std::cout << "\n";
    }
}

int main()
{
    size_t numDatos;
    std::cout << "Cuantos datos va a ingresar" << "\n";
    std::cin >> numDatos;
    std::cout << "\n";
    std::vector<Datos> datos = ingresoDatos(numDatos);
    std::cout << "\n";
    mostrarDatos(datos);
    _getch();
    return 0;
}

Update:

If you do not want to use structs then you should pass the values by reference and for that change your initial code to:

void ingresoDatos(vector<string> & nombreEquipo,
                  vector<string> & nombreJugador,
                  vector<string> & cedula,
                  vector<string> & telefono,
                  vector<string> & direccion,
                  vector<int> & edad,
                  vector<double> & estatura,
                  vector<double> & costoJugador, int numDatos)//argumentos
    
answered by 27.07.2018 / 04:32
source