String help?
Moderator: Coders of Rage
- ibly31
- Chaos Rift Junior
- Posts: 312
- Joined: Thu Feb 19, 2009 8:47 pm
- Current Project: Like... seven different ones
- Favorite Gaming Platforms: Xbox 360, Gamecube
- Programming Language of Choice: C++, ObjC
- Location: New Jersey.
String help?
How do you use STD::string? I always thought it was
Int numbah=31;
STD::string mystring
Mystring << "N=" << numbah;
But it's giving errors. And how do you display values in SDl? I can't figure out how to convert int to char*...
Int numbah=31;
STD::string mystring
Mystring << "N=" << numbah;
But it's giving errors. And how do you display values in SDl? I can't figure out how to convert int to char*...
Website/Tumblr
My Projects
The best thing about UDP jokes is that I don’t care if you get them or not.
Re: String help?
Code: Select all
#include <iostream>
#include <string>
int main()
{
std::string bob = "example";
return 0;
}
Code: Select all
]#include <iostream>
#include <string>
using namespace std;
int main()
{
string bob = "example";
return 0;
}
Re: String help?
cin >> mystring.How do you use STD::string? I always thought it was
Int numbah=31;
STD::string mystring
Mystring << "N=" << numbah;
But it's giving errors. And how do you display values in SDl? I can't figure out how to convert int to char*...
i prefer
char *str = (char*)malloc(sizeof(char));
gets(str);
you can also you scanf, works well for numbers.
look at atoi, iota, atof... and so on...
you know google knows more that eveyone combined on this forum... i think we are just being a little lazy.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- ibly31
- Chaos Rift Junior
- Posts: 312
- Joined: Thu Feb 19, 2009 8:47 pm
- Current Project: Like... seven different ones
- Favorite Gaming Platforms: Xbox 360, Gamecube
- Programming Language of Choice: C++, ObjC
- Location: New Jersey.
Re: String help?
well I guess my real question is how to display values in SDL. Anyone?
Website/Tumblr
My Projects
The best thing about UDP jokes is that I don’t care if you get them or not.
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: String help?
if you're reffering to getting console values while running SDL, then you can't, they disable the console.
If you mean, how do I output text to the screen in an SDL Window, you use the SDL_ttf library.
this is done by firstly declaring a TTF_Font*, calling TTF_OpenFont("fontanme.txt", int Pointsize) and then getting a surface and calling TTF_RenderText_Solid(TTF_Font*, char* Text, SDL_Color Colour)
The SDL DocWiki goes into much detail about the SDL_ttf library : http://www.libsdl.org/cgi/docwiki.cgi/SDL_ttf
and if you have any trouble with passing those C++ strings, you can just add .c_str() to the end of your string, to pass it like it was a char*
Hope this was of some help :D
If you mean, how do I output text to the screen in an SDL Window, you use the SDL_ttf library.
this is done by firstly declaring a TTF_Font*, calling TTF_OpenFont("fontanme.txt", int Pointsize) and then getting a surface and calling TTF_RenderText_Solid(TTF_Font*, char* Text, SDL_Color Colour)
Code: Select all
#include "SDL.h" // just change this to your SDL directory location, I just made this one up
#include "SDL_ttf.h"
SDL_Surface* Screen = NULL;
SDL_Surface* TextSurface = NULL;
SDL_Color Black = {0,0,0};
TTF_Font* Arial = NULL
int main(int argc, char** argv)
{
// set up screen, plus other startup crap in your program
Arial = TTF_OpenFont("Arial.ttf", 16); // load from ttf file, and choose size
TextSurface = TTF_RenderText_Solid(Arial, "This is Arial", Black) // use the chosen font for selected text
while(!Quit)
}
Apply_Surface(10,10,TextSurface, Screen); // draw text to screen
SDL_Flip(Screen);
}
return 0;
}
and if you have any trouble with passing those C++ strings, you can just add .c_str() to the end of your string, to pass it like it was a char*
Hope this was of some help :D
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
Re: String help?
programmerinprogress wrote:if you're reffering to getting console values while running SDL, then you can't, they disable the console.
If you mean, how do I output text to the screen in an SDL Window, you use the SDL_ttf library.
this is done by firstly declaring a TTF_Font*, calling TTF_OpenFont("fontanme.txt", int Pointsize) and then getting a surface and calling TTF_RenderText_Solid(TTF_Font*, char* Text, SDL_Color Colour)
The SDL DocWiki goes into much detail about the SDL_ttf library : http://www.libsdl.org/cgi/docwiki.cgi/SDL_ttfCode: Select all
#include "SDL.h" // just change this to your SDL directory location, I just made this one up #include "SDL_ttf.h" SDL_Surface* Screen = NULL; SDL_Surface* TextSurface = NULL; SDL_Color Black = {0,0,0}; TTF_Font* Arial = NULL int main(int argc, char** argv) { // set up screen, plus other startup crap in your program Arial = TTF_OpenFont("Arial.ttf", 16); // load from ttf file, and choose size TextSurface = TTF_RenderText_Solid(Arial, "This is Arial", Black) // use the chosen font for selected text while(!Quit) } Apply_Surface(10,10,TextSurface, Screen); // draw text to screen SDL_Flip(Screen); } return 0; }
and if you have any trouble with passing those C++ strings, you can just add .c_str() to the end of your string, to pass it like it was a char*
Hope this was of some help :D
ummm. i dont know how they would disable the console. the onsole is still available, its just a compiler option.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- ibly31
- Chaos Rift Junior
- Posts: 312
- Joined: Thu Feb 19, 2009 8:47 pm
- Current Project: Like... seven different ones
- Favorite Gaming Platforms: Xbox 360, Gamecube
- Programming Language of Choice: C++, ObjC
- Location: New Jersey.
Re: String help?
Thank you! THAT helped soo much. The c_str() bit! Now that I understand conversion from a std::String to a char[], how do you write to strings?
...hmm like... how do i explain,
"X Value = " + xvalue + ", Y value = " + yvalue;
How do you do this with std::strings?
EDIT: Oh, and is there a way to convert a char[] back to a string? Because all the functions i see to convert ints to strings... They input and ouput char[]s...
...hmm like... how do i explain,
"X Value = " + xvalue + ", Y value = " + yvalue;
How do you do this with std::strings?
EDIT: Oh, and is there a way to convert a char[] back to a string? Because all the functions i see to convert ints to strings... They input and ouput char[]s...
Last edited by ibly31 on Fri Mar 13, 2009 5:03 pm, edited 1 time in total.
Website/Tumblr
My Projects
The best thing about UDP jokes is that I don’t care if you get them or not.
Re: String help?
As for your first question, you'd be doing something like this
Code: Select all
#include <iostream>
#include <sstream>
using namespace std;
int main () {
int numbah=31;
ostringstream mystringstream;
mystringstream << "N=" << numbah;
cout << mystringstream.str() << endl;
return 0;
}
- ibly31
- Chaos Rift Junior
- Posts: 312
- Joined: Thu Feb 19, 2009 8:47 pm
- Current Project: Like... seven different ones
- Favorite Gaming Platforms: Xbox 360, Gamecube
- Programming Language of Choice: C++, ObjC
- Location: New Jersey.
Re: String help?
I don't wanna make a whole new string and a new header... Then there'd be the problem of concertng that back to a STD::string and them to a c_str()Scoody wrote:As for your first question, you'd be doing something like thisCode: Select all
#include <iostream> #include <sstream> using namespace std; int main () { int numbah=31; ostringstream mystringstream; mystringstream << "N=" << numbah; cout << mystringstream.str() << endl; return 0; }
Website/Tumblr
My Projects
The best thing about UDP jokes is that I don’t care if you get them or not.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: String help?
Code: Select all
#include <string>
#include <iostream>
using namespace std;
...
string mystring = "abc";
string mystring2 = "abc";
int somenum = 4;
/* This won't compile:
mystring2 = "def" + "ghi" + "jkl";
But this will: */
mystring += "def";
mystring += "ghi";
mystring += mystring2 + "def" + "ghi"; //to use + on multiple rvals, one of them must be a std::string
mystring += somenum;
cout << mystring << endl;
char mybuffer[] = "hello";
mystring = mybuffer;
mystring += mybuffer;
cout << mystring << endl;
And it's not STD::string. It's std::string.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- Ginto8
- ES Beta Backer
- Posts: 1064
- Joined: Tue Jan 06, 2009 4:12 pm
- Programming Language of Choice: C/C++, Java
Re: String help?
To convert a char* (or const char*, I think) just do this:
I know there's a std::string::operator=(char*).
Code: Select all
char *cstring = "I'm a C string!";
std::string cplusplus_string = cstring;
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: String help?
Yeah since arrays are pointers,, above.
Code: Select all
char mybuffer[] = "hello";
mystring = mybuffer;
mystring += mybuffer;
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: String help?
Seriously, try cout << or cin in an SDL Application, after you initialise SDL, the console ceases to function, instead, you have to send your output text to either cerr, clog, or any other file where you store your data.avansc wrote: ummm. i dont know how they would disable the console. the onsole is still available, its just a compiler option.
The console itself is a compiler option (which you can choose to hide or make appear), but control over the console is locked in SDL
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- Kros
- Chaos Rift Regular
- Posts: 136
- Joined: Tue Feb 24, 2009 4:01 pm
- Current Project: N/A
- Favorite Gaming Platforms: PC, Playstation/2/3
- Programming Language of Choice: C++
- Location: Oregon,USA
- Contact:
Re: String help?
I've used cout and cin with an SDL application. Dunno what you're talking about.programmerinprogress wrote:Seriously, try cout << or cin in an SDL Application, after you initialise SDL, the console ceases to function, instead, you have to send your output text to either cerr, clog, or any other file where you store your data.avansc wrote: ummm. i dont know how they would disable the console. the onsole is still available, its just a compiler option.
The console itself is a compiler option (which you can choose to hide or make appear), but control over the console is locked in SDL
YouTube ChannelIsaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
- ibly31
- Chaos Rift Junior
- Posts: 312
- Joined: Thu Feb 19, 2009 8:47 pm
- Current Project: Like... seven different ones
- Favorite Gaming Platforms: Xbox 360, Gamecube
- Programming Language of Choice: C++, ObjC
- Location: New Jersey.
Re: String help?
Why would I use them? Cin is just for input, and that wouldn't be good cuz I can't control where it is, and Cout is just displaying text which I can do with SDL TTF extention
Website/Tumblr
My Projects
The best thing about UDP jokes is that I don’t care if you get them or not.