Can't figure out why this is crashing

bool loadPlayerImages(const char* dir, const char* ext, int num_sprites)
{

for (i=0;i<=num_sprites;i+=1)
{

    using namespace std;
    string file = ToString( dir ) + ToString( i ) + ToString( ext );
    const char* pszConstString = file.c_str ();
    MessageBox(0, pszConstString, "ERROR", MB_OK);
    player_image[i] = SOIL_load_OGL_texture
    (
        pszConstString,
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_MIPMAPS | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    );
    glGetTexLevelParameteriv(GL_TEXTURE_2D, miplevel, GL_TEXTURE_WIDTH, &width[i]);
    glGetTexLevelParameteriv(GL_TEXTURE_2D, miplevel, GL_TEXTURE_HEIGHT, &height[i]);
};

}

Its suppose to take dir, i, and ext and add them together to get a const char* string thats looks like this:

img/1.bmp

For some reason it crashes the program. What am I doing wrong?

Thanks,

~Justin123

Hi,

Are you getting the desired output from pszConstString? If not try replacing:

string file = ToString( dir ) + ToString( i ) + ToString( ext );

with:

string file;
char* temp;
file+=dir;
sprintf(temp,"%i",i);
file+=temp;
file+=ext;

Also, is the variable i globally declared? You don’t declare it in the function (does your compiler allow this?)

Finally, your for loop goes from 0 to num_sprites, so it actually evaluates everything num_sprites+1 times. Do you actually have num_sprites+1 files to open? (no insult intended here, I’ve made this mistake many times.)

Hope this is helpful,
David