Q:

Can't open a window with SDL/C++

i stuck in a big bug with my code which i can't open the first window of my game after running it! it just open blank for a few secondes and then it closes automatically. I doon't know what wrong with my code beacause i'm following series of tutorials and my code seems like the code in the tutorials. here's my code:
#include "stdafx.h"
#include "FirstClass.h"

FirstClass::FirstClass(void){

bool quit = false;
window = NULL;
window = SDL_CreateWindow("Snaykie v1.0",350, 150, 800, 500, SDL_WINDOW_SHOWN);
if (window == NULL){
    std::cout << "Can not open the game!" << std::endl;
}
renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
evt = new SDL_Event();
//background image
txt = NULL;
txt = IMG_LoadTexture(renderer, "mainview.bmp");

rct.x = 0 ;
rct.y = 0;
rct.h = 500;
rct.w = 800;
//button start 
sbutt = NULL;
sbutt = IMG_LoadTexture(renderer, "startbutton.bmp");

startrct.x = 0 ;
startrct.y = 0;
startrct.h = 100;
startrct.w = 100;
}

FirstClass::~FirstClass(void)
{
SDL_DestroyTexture(txt);
SDL_DestroyTexture(sbutt);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete evt;
} 
//the code below is the loops that let the window stucks forever to keep      opened
void FirstClass ::  GameStart(void){
while (!quit && evt->type != SDL_QUIT){

    SDL_PollEvent(evt);
   SDL_RenderClear(renderer);
   SDL_RenderCopy(renderer, txt, NULL, &rct);
   SDL_RenderCopy(renderer, sbutt, NULL, &startrct);
   SDL_RenderPresent(renderer);

}
}

Please help me and thanks i'm really in a big problem.

A:

You are handling the evt variable in a bad way. It is allocated but not initialized, and you compare the uninitialized value to SDL_QUIT. You should reorder the code inside FirstClass::GameStart and preferably use a local variable (since there is no actual need to use a dynamically allocated one) and remove the allocation and deletion of the existing evt from constructor and destructor. Here is a better version of FirstClass::GameStart:
void FirstClass::GameStart(void){
    SDL_Event evt;
    while (SDL_PollEvent(&evt)){
        if (evt.type == SDL_QUIT) {
            // a user requested termination has been received, exit the loop
            break;
        }
        else {
            // filter the event?
            SDL_RenderClear(renderer);
            SDL_RenderCopy(renderer, txt, NULL, &rct);
            SDL_RenderCopy(renderer, sbutt, NULL, &startrct);
            SDL_RenderPresent(renderer);
        }
    }
}

Also ensure that you are actually invoking the FirstClass::GameStart funcion from main, maybe you are simply not running the event loop at all.

