-3

I can't figure out why my program crashed on int iNiv = atol(LES_EMPLOYES[i][j + 2]); when i = 9.. everything else works perfectly. It's driving me crazy.

int j;
for (int i = 0; i <= NB_EMPLOYES - 1; i++)
{
    j = 0;
    string sNom = LES_EMPLOYES[i][j];
    j += 1;
    int iNum = atol(LES_EMPLOYES[i][j + 1]);
    j += 1;
    int iNiv = atol(LES_EMPLOYES[i][j + 2]);
    CEmploye* unEmploye = new CEmploye(sNom, iNum, iNiv);
    tabEmployes[i] = unEmploye;
}
const char* LES_EMPLOYES [NB_EMPLOYES] [3] =
{
    { "Kashmir Ducom",      "7301",  "1"},
    { "Zanael Batard",      "7302",  "1"},
    { "Azilis Tapin",       "7303",  "2"},
    { "Mayeul Malfait",     "7304",  "2"},
    { "Alexiam Castorix",   "7305",  "3"},
    { "Zoemy Malapry",      "7306",  "3"},
    { "Capri Lagarce",      "7307",  "1"},
    { "Samsara Gaudiche",   "7308",  "4"},
    { "Ghessy Grommolard",  "7309",  "3"},
    { "Abyalex Fayot",      "7310",  "5"}
};
Sadboy
  • 1
  • 1

1 Answers1

0

You are out of bound when you try to access the third element, which causes an Undefined Behavior:

j = 0;
string sNom = LES_EMPLOYES[i][j]; // Access index 0 first element: OK
j += 1;
int iNum = atol(LES_EMPLOYES[i][j + 1]); // Access index 2, THIRD element: OK? My guess is that you would like to access the second element (index 1)
j += 1;
int iNiv = atol(LES_EMPLOYES[i][j + 2]); // Access index 4, Out of bound problem: Undefined Behavior

You should remove your lines j += 1;

Community
  • 1
  • 1
Jérôme
  • 8,016
  • 4
  • 29
  • 35