Repeated results in C ++

3

I have created a code in which I register a certain number of soccer players and at the end they print their data, but these are repeated according to the last registered player. This is my code:

#include <iostream>
#include <cstdlib>
#include <windows.h>

using namespace std;

int main(int argc, char** argv) {

system("color E0");

int CantidadJugadores = 0;
char NombresJugadores[50], PosicionJugadores[50], ExpulsionesJugadores[10], NumeroJugadores[10], GolesJugadores[10], FaltasJugadores[10];
cout<<"///////////////////INICIO DEL REGISTRO DE JUGADORES///////////////////////\n";
cout<<"Inserta la cantidad de jugadores \n";
cin>>CantidadJugadores;
char* registros[CantidadJugadores][6]; // VECTOR DE JUGADORES
for(int indice=0; indice < CantidadJugadores; indice++)
{
        cin.ignore();
        cout<<"\tJugador Numero "<<indice+1;
        cout<<"\nInserta el nombre del jugador \n";
        cin>>NombresJugadores;
        registros[ indice ][0] = NombresJugadores;

        cin.ignore();
        cout<<"Inserta el numero del jugador \n";
        cin>>NumeroJugadores;
        registros[ indice ][1] = NumeroJugadores;

        cin.ignore();
        cout<<"Inserta la posicion del jugador \n";
        cin>>PosicionJugadores;
        registros[ indice ][2] = PosicionJugadores;

        cin.ignore();
        cout<<"Inserta la cantidad de goles del jugador \n";
        cin>>GolesJugadores;
        registros[ indice ][3] = GolesJugadores;

        cin.ignore();
        cout<<"Inserta la cantidad de faltas del jugador \n";
        cin>>FaltasJugadores;
        registros[ indice ][4] = FaltasJugadores;

        cin.ignore();
        cout<<"Inserta la cantidad de expulsiones del jugador \n";
        cin>>ExpulsionesJugadores;
        registros[ indice ][5] = ExpulsionesJugadores;

        cout<<"\n===================Jugador: "<<indice+1<<"===================\n";
}
cout<<"\n///////////////////FIN DEL REGISTRO DE JUGADORES////////////////////////\n";
system("PAUSE");
system("cls");

system("color 1F");
cout<<"/////////////////// Estos Son Los Datos /////////////////// \n"<<endl;
int indice=0;
while( indice < CantidadJugadores ){ 
    cout<<"\tJugador Numero "<<indice+1<<endl;
    cout<<"\nNombre: "<< registros[indice][0] <<endl;
    cout<<"Numero de Jugador: "<<registros[indice][1] <<endl;
    cout<<"Posicion: "<< registros[indice][2] <<endl;
    cout<<"Goles: "<< registros[indice][3] <<endl;
    cout<<"Faltas: "<< registros[indice][4] <<endl;
    cout<<"Expulsiones: "<< registros[indice][5] <<endl;
    cout<<"============================================="<<endl;
    indice++;
}
return 0;
}

    
asked by Orlando 26.11.2018 в 03:26
source

1 answer

3

In C ++ it is not legal to use variables to declare arrays:

int CantidadJugadores = 0;
cin>>CantidadJugadores;
char* registros[CantidadJugadores][6];

This feature is known as VLA (Variable Length Array) and no is a standard feature. Although some compilers support it, its use is not convenient since, apart from not being portable, its functionality is not guaranteed in all cases.

To manage this type of arrays you have to go to the dynamic memory or use a container of the STL.

On the other hand, your vector of players:

char* registros[CantidadJugadores][6]; // VECTOR DE JUGADORES

It is a matrix of pointers. That is, each position (for example registros[0][0] ), is a pointer. Since when you make the assignments you use the same variable all the time, what you get is that the different registers point to the same memory position. Said graphically:

registros[0][0] = NombresJugadores;
registros[1][0] = NombresJugadores;
registros[2][0] = NombresJugadores;

The simplest solution here would be to use std::string instead of pointers to char :

std::string registros[01][6];

Although I would personally suggest you create a data structure:

struct Jugador
{
  std::string nombre;
  int numero;
  std::string posicion;
  int goles;
  int faltas;
  int expulsiones;
};

cin>>CantidadJugadores;
Jugador * jugadores = new Jugador[CantidadJugadores];

// ...

cout << "\tJugador Numero "<<indice+1;
cout << "\nInserta el nombre del jugador \n";
cin >> registros[indice].nombre;

cout << "Inserta el numero del jugador \n";
cin >> registros[indice].numero;

cout << "Inserta la posicion del jugador \n";
cin >> registros[indice].posicion;

// ...

delete[] jugadores;
    
answered by 26.11.2018 в 07:45