简体   繁体   中英

".exe has stopped working" when i try to cout a string

I can't seem to get the code running. Iwould appreciate if someone could explain why is this happening.

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

struct Jugador{
     int codigo;
     string nombre;
     float tiempo;
     int tamano;
     int cuad_magico[][3];
   };

int main()
{
    const int N=7;
    Jugador Jugadores[N];
    string J[N]={"HUGO","PACO","LUIS","DONALD","PLUTO","MICKEY","GOOFY"};

    Jugadores[0].nombre = J[0];
    Jugadores[1].nombre = J[1];
    Jugadores[2].nombre = J[2];
    Jugadores[3].nombre = J[3];
    Jugadores[4].nombre = J[4];
    Jugadores[5].nombre = J[5];
    Jugadores[6].nombre = J[6];

    Jugadores[0].codigo=0;
    Jugadores[0].tamano=3;
    Jugadores[0].tiempo=3.5;
    Jugadores[0].cuad_magico[0][0]=4;
    Jugadores[0].cuad_magico[0][1]=9;
    Jugadores[0].cuad_magico[0][2]=2;
    Jugadores[0].cuad_magico[1][0]=3;
    Jugadores[0].cuad_magico[1][1]=5;
    Jugadores[0].cuad_magico[1][2]=7;
    Jugadores[0].cuad_magico[2][0]=8;
    Jugadores[0].cuad_magico[2][1]=1;
    Jugadores[0].cuad_magico[2][2]=6;

    Jugadores[1].codigo=1;
    Jugadores[1].tamano=3;
    Jugadores[1].tiempo=6.2;
    Jugadores[1].cuad_magico[0][0]=8;
    Jugadores[1].cuad_magico[0][1]=3;
    Jugadores[1].cuad_magico[0][2]=4;
    Jugadores[1].cuad_magico[1][0]=1;
    Jugadores[1].cuad_magico[1][1]=5;
    Jugadores[1].cuad_magico[1][2]=9;
    Jugadores[1].cuad_magico[2][0]=6;
    Jugadores[1].cuad_magico[2][1]=7;
    Jugadores[1].cuad_magico[2][2]=2;

    Jugadores[2].codigo=2;
    Jugadores[2].tamano=3;
    Jugadores[2].tiempo=4.3;
    Jugadores[2].cuad_magico[0][0]=2;
    Jugadores[2].cuad_magico[0][1]=9;
    Jugadores[2].cuad_magico[0][2]=4;
    Jugadores[2].cuad_magico[1][0]=7;
    Jugadores[2].cuad_magico[1][1]=5;
    Jugadores[2].cuad_magico[1][2]=3;
    Jugadores[2].cuad_magico[2][0]=6;
    Jugadores[2].cuad_magico[2][1]=1;
    Jugadores[2].cuad_magico[2][2]=8;

It gets stuck here, am I doing something wrong?

    cout <<  Jugadores[1].nombre  << endl;
}

int cuad_magico[][3];

This does not allocate an array for cuad_magico .You only defined a pointer to array of 3 integers . From reading your code, it seems that you wanted to say:

int cuad_magico[3][3];

Without this, you have undefined behavior when you write Jugadores[1].cuad_magico[2][2]=2 or similar operations, because the array is not allocated.

ps: since you are allowed to use stl (bc you use string ), why not use std::vector ? It is much better to use stl arrays and vectors than native C-style arrays.

struct Jugador{
     int codigo;
     string nombre;
     float tiempo;
     int tamano;
     int cuad_magico[3][3];
//___________________^

   };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM