String help?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
ibly31
Chaos Rift Junior
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?

Post 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*...
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
andrew
Chaos Rift Regular
Chaos Rift Regular
Posts: 121
Joined: Mon Dec 08, 2008 2:12 pm

Re: String help?

Post 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++.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: String help?

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
ibly31
Chaos Rift Junior
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?

Post by ibly31 »

well I guess my real question is how to display values in SDL. Anyone?
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
programmerinprogress
Chaos Rift Devotee
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?

Post 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
---------------------------------------------------------------------------------------
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
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: String help?

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
ibly31
Chaos Rift Junior
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?

Post 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...
Last edited by ibly31 on Fri Mar 13, 2009 5:03 pm, edited 1 time in total.
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

Re: String help?

Post 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;
}
User avatar
ibly31
Chaos Rift Junior
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?

Post 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()
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: String help?

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: String help?

Post 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*). ;)
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.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: String help?

Post by MarauderIIC »

Yeah since arrays are pointers,

Code: Select all

char mybuffer[] = "hello";
mystring = mybuffer;
mystring += mybuffer;
, above.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
programmerinprogress
Chaos Rift Devotee
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?

Post 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
---------------------------------------------------------------------------------------
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
User avatar
Kros
Chaos Rift Regular
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?

Post 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. :)
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
User avatar
ibly31
Chaos Rift Junior
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?

Post 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
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
Post Reply