It's a problem with the pointers. Here I have a program that gives "high" soccer players, asks the name of the coach, and then asks you to fill some fields about the information of your players.
The problem is that the program stops working when you select that you want to register 3 players, that is, it lets you fill out all the fields, but at the time of printing the data, the program crashes.
I checked it by removing pointers, and it worked. And I honestly can not understand the problem.
This is my code:
using namespace std;
#include<iostream>
#include<cstdlib>
#include <stdio.h>
class Persona
{
private:
string NomComp;
int Edad;
public:
Persona( string NC = " " , int E = 1 );
void Leer( void );
void Imprimir( void );
};
Persona::Persona( string NC , int E ) : NomComp ( NC ) , Edad ( E )
{
}
void Persona::Leer( void )
{
cout<< "\t\tDame el Nombre Completo: " ;
getline( cin, NomComp);
cout<< "\t\tDame la Edad: ";
cin>> Edad;
cout<< endl;
fflush ( stdin );
}
void Persona::Imprimir( void )
{
cout<< "\t\tNombre Completo :" << NomComp << endl;
cout<< "\t\tEdad : " << Edad << endl;
}
class Jugador : public Persona
{
private:
string Posicion;
string Apodo;
string Equipo;
public:
Jugador ( string NC = " " , int E = 1 , string P = " " ,
string A = " " , string Eq = " " );
void Leer( void );
void Imprimir( void ) ;
};
Jugador::Jugador ( string NC , int E , string P , string A , string Eq )
: Persona ( NC , E), Posicion ( P ) , Apodo ( A ) ,
Equipo ( Eq )
{
}
void Jugador::Leer( void )
{
static int n = 1;
cout << "\t\tJugador : " << n << endl;
Persona::Leer();
cout << "\t\tPosicion : ";
getline(cin, Posicion);
cout << "\t\tApodo : ";
getline(cin, Apodo);
cout << "\t\tEquipo : ";
getline(cin, Equipo);
cout << endl;
n++;
}
void Jugador::Imprimir( void )
{
static int n = 1;
cout << "Jugador " << n << endl;
Persona::Imprimir( );
cout << "\t\tPosicion : " << Posicion << endl
<< "\t\tApodo : " << Apodo << endl
<< "\t\tEquipo : " << Equipo
<< endl << endl;
n++;
}
class Seleccion : public Jugador
{
private:
string Pais;
Persona* ApDT;
int NumJug;
Jugador* ArreJug;
public:
Seleccion ( int NJ = 2 , string Pa = " ");
Seleccion( Seleccion& Sel);
void Leer( void );
void Imprimir( void );
Seleccion& operator = ( Seleccion& Sel2 );
~Seleccion( void );
};
Seleccion::Seleccion ( int NJ, string Pa ) : NumJug ( NJ ), Pais ( Pa )
{
ApDT = new Persona;
ArreJug = new Jugador[ NumJug ];
}
Seleccion::Seleccion( Seleccion& Sel)
{
Pais = Sel.Pais;
ApDT = new Persona;
*ApDT = *( Sel.ApDT );
NumJug = Sel.NumJug;
ArreJug = new Jugador[ NumJug ];
for ( int i = 0; i < NumJug; i++)
ArreJug [ i ] = Sel.ArreJug [ i ];
}
void Seleccion:: Leer( void )
{
fflush ( stdin );
cout << "Dame El Pais: ";
getline (cin,Pais);
cout << "Nombre del Director Tecnico" << endl;
ApDT-> Leer ();
for (int i = 0;i < NumJug; i++)
{
ArreJug[i].Leer ();
}
cout << endl;
}
void Seleccion:: Imprimir( void )
{
cout << "Pais: " << Pais << endl;
cout << "Nombre del Director Tecnico: "
<< endl;
ApDT->Imprimir();
cout << "Jugadores " << endl;
for (int i = 0 ; i < NumJug ; i++)
{
ArreJug[i].Imprimir();
}
}
Seleccion& Seleccion::operator = ( Seleccion& Sel2 )
{
if ( this != &Sel2 )
{
if ( NumJug != Sel2.NumJug )
{
delete[] ArreJug;
ArreJug = new Seleccion [Sel2.NumJug];
}
Pais = Sel2.Pais;
*ApDT = *( Sel2.ApDT);
NumJug = Sel2.NumJug;
for ( int i = 0; i < NumJug; i++)
ArreJug [ i ] = Sel2.ArreJug [ i ];
}
return *this;
}
Seleccion::~Seleccion( void )
{
delete ApDT;
delete[] ArreJug;
}
int NumeroJug ( void );
int NumeroJug ( void )
{
int NJ;
cout << "Dame el numero de Jugadores: ";
cin >> NJ;
cout << endl;
return NJ;
}
int main ( void )
{
system( "cls");
int NJ;
NJ = NumeroJug();
Seleccion* AptP;
AptP = new Seleccion ( NJ );
AptP->Leer( );
Seleccion* AptP2 = new Seleccion;
Seleccion* AptP3 = new Seleccion;
*AptP3 = *AptP2 = *AptP;
cout << "ORIGINAL" << endl;
AptP -> Imprimir( );
cout << "ASIGNADO" << endl;
AptP2 -> Imprimir ( );
cout << "ASIGNADO" << endl;
AptP3 -> Imprimir ( );
delete AptP2;
delete AptP3;
delete AptP;
system( "pause ");
return 0;
}