Page 1 of 2

String help?

Posted: Thu Mar 12, 2009 9:50 pm
by ibly31
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*...

Re: String help?

Posted: Thu Mar 12, 2009 11:15 pm
by andrew

Code: Select all

#include <iostream>
#include <string>

int main()
{
    std::string bob = "example";
    return 0;
}
Alternatively you could use it like this:

Code: Select all

]#include <iostream>
#include <string>

using namespace std;

int main()
{
    string bob = "example";
    return 0;
}
Have a look at this link: C++ Reference Material Strings in C and C++.

Re: String help?

Posted: Thu Mar 12, 2009 11:16 pm
by avansc
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*...
cin >> mystring.

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.

Re: String help?

Posted: Fri Mar 13, 2009 3:54 pm
by ibly31
well I guess my real question is how to display values in SDL. Anyone?

Re: String help?

Posted: Fri Mar 13, 2009 4:35 pm
by programmerinprogress
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)

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; 
}
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

Re: String help?

Posted: Fri Mar 13, 2009 4:42 pm
by avansc
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)

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; 
}
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

ummm. i dont know how they would disable the console. the onsole is still available, its just a compiler option.

Re: String help?

Posted: Fri Mar 13, 2009 4:51 pm
by ibly31
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...

Re: String help?

Posted: Fri Mar 13, 2009 4:59 pm
by Scoody
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;
}

Re: String help?

Posted: Fri Mar 13, 2009 5:35 pm
by ibly31
Scoody wrote: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;
}
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()

Re: String help?

Posted: Fri Mar 13, 2009 7:33 pm
by MarauderIIC

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;
mystring.c_str() converts to const char*, not char*. Essentially that's read-only.

And it's not STD::string. It's std::string.

Re: String help?

Posted: Fri Mar 13, 2009 8:46 pm
by Ginto8
To convert a char* (or const char*, I think) just do this:

Code: Select all

char *cstring = "I'm a C string!";
std::string cplusplus_string = cstring;
I know there's a std::string::operator=(char*). ;)

Re: String help?

Posted: Fri Mar 13, 2009 11:34 pm
by MarauderIIC
Yeah since arrays are pointers,

Code: Select all

char mybuffer[] = "hello";
mystring = mybuffer;
mystring += mybuffer;
, above.

Re: String help?

Posted: Sat Mar 14, 2009 6:32 am
by programmerinprogress
avansc wrote: ummm. i dont know how they would disable the console. the onsole is still available, its just a compiler option.
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.

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

Re: String help?

Posted: Sat Mar 14, 2009 8:22 am
by Kros
programmerinprogress wrote:
avansc wrote: ummm. i dont know how they would disable the console. the onsole is still available, its just a compiler option.
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.

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've used cout and cin with an SDL application. Dunno what you're talking about. :)

Re: String help?

Posted: Sat Mar 14, 2009 8:24 am
by ibly31
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