Outputting an Integer and Manipulating it.
So I'm making a lottery game, I got the three slots and all. But all I have left, is how to set a certain amount of credit, say 1500 and decrease everytime the user clicks "insert credit". How would I decrease it ?
We need way more information (like language and libraries used) if you want detailed help, but this is basically going to be a case of
- having a 'money' variable stored in an appropriate scope
- when the user clicks the button, decrement this value then update the display
- having a 'money' variable stored in an appropriate scope
- when the user clicks the button, decrement this value then update the display
One way is to convert the integer to a string (using the itoa or sprintf functions) then using the SDL_ttf library to write the resulting string to the screen.
I take it you're making a graphical user interface. Rather to us an integer or a float depends on if you're going to allow someone to insert fractions of a credit. If you allow only 1 credit at a time, which seems to be most sensible, us an integer. Once you detect that the player clicked insert credit, credits = credits - X (X being number of credits inserted) or credits-- to decrease by one.
As to converting the number, in C++ you can use std::stringstream like so
As to converting the number, in C++ you can use std::stringstream like so
std::stringstream ss;
ss << credits;
std::string creditString = ss.str(); // returns string representation of credits.
|error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'SDL_Surface*' for argument '3' to 'void apply_surface(int, int, SDL_Surface*, SDL_Surface*, SDL_Rect*)'| I did what you said but all I keep getting this error, How would I show it on the screen ?
As I said, input the string to the SDL_ttf library (it has a function called TTF_WriteText or something like that which takes a character string and produces a SDL_Surface with the string in graphical form). If you are using stringstream, you need to transform the std::string into a char*, which should be possible using a function called c_str() (or something like that)
That's because apply_surface is expecting a pointer to an SDL_Surface struct and you're trying to pass it an std::string object. The streamstring only converts the integer into a string. As RulerOfNothing said, you can then use the SDL_ttf library to convert the string into a struct that you can feed to apply_surface.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement