   SDL_Event
  i have this problem that i use SDL_Event to navigate trough my games various screens.but my problem is that when i navigate back and forward few times the program eventually closes, and i don't understand why. I hope you can help me. thx in advance. here's the source:while(quit == 0)    { if(SDL_PollEvent(&event)) {  if(event.type == SDL_MOUSEBUTTONDOWN)  {  if(SDL_GetMouseState(&checkx, &checky))  {    if((checkx > 230) && (checkx < 415) && (checky > 361) && (checky < 394))   {quit = 1;}   if ((checkx > 230) && (checkx < 415) && (checky > 236) &&(checky < 269))   {quit = 2;}  }  }  if(event.type == SDL_QUIT)  {quit = 1;}  }    }    switch (quit)    { case 1: {clean_up(); TTF_CloseFont(font); TTF_Quit(); SDL_Quit(); return 0;} case 2: {clean_up();return true;}    }
  You need to put the SDL_PollEvent in a while loop.  This is so all SDL Events for that frame can be processed.  I think in your case you are only processing the first.  This may cause the program logic to screw up and cause a crash.  Try changing the if statement to a while statement and see if that solves the problem.Like so:game = 1;while( game ){    while( SDL_PollEvent( &event ) )    {   if( event.type == SDL_QUIT )   { //Quit the program  game = 0;   }    }};  thx for the replie that really helped. But by this time i have the next SDL input problem. I am so close to finishing my game but, this thing still holds me back.i am trying to get input from the user, usually by input i use a code where i take the input into a string, but in my game i need the key input into a integer(numbers only in the input. So maybe you guys know how to handle it. i usually use this code, but this doesnt work with numbers:bool nameEntered = false;    string user = "";    SDL_EnableUNICODE( SDL_ENABLE );    while(nameEntered == false)    {  string temp = user;  if(SDL_PollEvent(&event))  {if(event.type == SDL_KEYDOWN)   {   if( user.length() <= 10 )   {  if(event.key.keysym.unicode == (Uint16)' ' )  {  user += (char)event.key.keysym.unicode;  }  else if( (event.key.keysym.unicode >= (Uint16) '0' ) && (event.key.keysym.unicode <= (Uint16)'9' ))  {  user += (char)event.key.keysym.unicode;  }  else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )   {  user += (char)event.key.keysym.unicode;  }  else if(( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )  {  user  += (char)event.key.keysym.unicode;  }}    }if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( user.length() != 0 ) ){user.erase( user.length() - 1 );SDL_FreeSurface(text2);text2 = TTF_RenderText_Solid(font, user.c_str(), textColor);apply_surface(303,147,text2,screen);SDL_Flip(screen);}if( user != temp ){SDL_FreeSurface(text2);text2 = TTF_RenderText_Solid( font, user.c_str(), textColor );apply_surface(303,147,text2,screen);SDL_Flip(screen);} if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_RETURN ) ){nameEntered = true;} }   };Quote:I am so close to finishing my game but, this thing still holds me back.i am trying to get input from the user, usually by input i use a code where i take the input into a string, but in my game i need the key input into a integer(numbers only in the input.The easiest approach would probably be to read the number into a string (just as you do with the character name), and then convert the string to an integer.Once you have the string, you can convert it to an integer using the stringstream class:std::stringstream stream;int value;stream << string_entered_by_user;stream >> value;You'll probably want to filter the input for characters that don't make sense (i.e. anything other than [0-9], assuming only non-negative values are allowed). An additional safeguard would be to check the state of the stream to determine if the extraction was successful.Boost includes a nice function template for performing these sorts of conversions, lexical_cast(). It takes care of the details for you, and will inform you (by throwing an exception) if the conversion fails.   ;  oh my god., you are a lifesaver, thank you so much.   ;  For handling numeric input, the implementation appears to be pretty obvious: ignore input that isn't numeric. When the user completes the number (e.g. by pressing return) you can convert the string to an integer by using boost::lexical_cast<> or std::stringstream.You might also want to start to separate the code a little. For example, the TTF rendering could be handled by a class not unlike I wrote here.You could remove some of the repetition by moving some of the conditionals together. You can reduce the processor load by using SDL_WaitEvent() rather tahn SDL_PollEvent(). We can write some reusable code this way.bool acceptable(Uint16 c){    return std::isalnum(c) || c == ' ';}std::string readString(int x, int y, int maxLength, const SDL_Colour &colour, TTF_Font *font){    SDL_EnableUNICODE( SDL_ENABLE );    Text text(result, colour, font);    // You don't have to explicitly initialise std::string objects.    // They can do that themselves.    std::string result;SDL_Surface *screen = SDL_GetVideoSurface();    SDL_Event event;    while(SDL_WaitEvent(&event))    {   std::string temp = result;   if(event.type == SDL_KEYDOWN)   {  // Use std::string::empty() in preference to comparing size to 0  if(event.key.keysym.sym == SDLK_BACKSPACE ) && !user.empty() )  { result.erase( result.length() - 1 );  }  else if(event.key.keysym.sym == SDLK_RETURN)  { // You may want to ensure that the string isn't empty before returning return result;  }  else if(user.length() <= maxLength && acceptable(event.key.keysym.unicode))  { result += static_cast<char>(event.key.keysym.unicode);  }}if(temp != result){    text.updateText(result);    // don't forget to clear the area under the text    text.render(x, y, screen);    SDL_Flip(screen);}}You could get even fancier and have an additional parameter to readString() which determines which "acceptable" function to use. This means that you could make ensuring only an integer is returned:bool acceptable(Uint16 c){    return std::isalnum(c) || c == ' ';}// This bit makes an alias for a pointer to a functiontypedef bool (*AcceptFunctionPtr)(Uint16);// Include a default argument for the "acceptable" function.std::string readString(int x, int y, int maxLength, const SDL_Colour &colour, TTF_Font *font, AcceptFunctionPtr function = &acceptable){    SDL_EnableUNICODE( SDL_ENABLE );    Text text(result, colour, font);    // You don't have to explicitly initialise std::string objects.    // They can do that themselves.    std::string result;SDL_Surface *screen = SDL_GetVideoSurface();    SDL_Event event;    while(SDL_WaitEvent(&event))    {   std::string temp = result;   if(event.type == SDL_KEYDOWN)   {  // Use std::string::empty() in preference to comparing size to 0  if(event.key.keysym.sym == SDLK_BACKSPACE ) && !user.empty() )  { result.erase( result.length() - 1 );  }  else if(event.key.keysym.sym == SDLK_RETURN)  { return result;  }  else if(user.length() <= maxLength && function(event.key.keysym.unicode))  { result += static_cast<char>(event.key.keysym.unicode);  }}if(temp != result){    text.updateText(result);    // don't forget to clear the area under the text    text.render(x, y, screen);    SDL_Flip(screen);}}bool acceptsIntegers(Uint16 c){   return std::isnum(c);}int readInteger(int x, int y, int maxLength, const SDL_Colour &colour, TTF_Font *font){    std::string string = readString(x, y, maxLength, colour, font, &acceptsIntegers);    std::stringstream stream(string);    int result;    if(stream >> result)    {    return result;    }    else    {    // error handling    }};  I don't get it i tried jyk-s example and wrote: std::stringstream stream;    int shield;    stream << shi;    stream >> shield;(where shi is a string) but when i compile it throws at me the error"aggerate 'std::stringstream'has incomplete type and cannot be defined"hope you can help me   ;Quote:Original post by sander242I don't get it i tried jyk-s example and wrote: std::stringstream stream;    int shield;    stream << shi;    stream >> shield;(where shi is a string) but when i compile it throws at me the error"aggerate 'std::stringstream'has incomplete type and cannot be defined"hope you can help meHave you tried putting the error message into Google? (I took the liberty of correcting the error message. You should use copy & paste for that kind of job).   ;  Did you include the header sstream?If not, remember that you must include the appropriate headers in order to use classes and functions from the standard library (or any library).   ;  ooh sry, i forgot to include that, anyway included it and everything works like a charm thx a lot   