adding new line to string literal

Aug 23, 2012 at 8:11pm
Hello, sorry if it was asked before, but I tryed everything and it didn't work.
No new line inserted, the whole line in a row is dislayed instead.
The string must be passed as a const char * parameter to a function.

I intended: string s = "hello \n word!"

SO: Ubuntu 12.04
gcc versión 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)

Thanks and regards.

rossig



Aug 23, 2012 at 8:40pm
1
2
3
4
5
6
7
8
#include <string>
#include <iostream>

int main()
{
  std::string s = "hello \n world!";
  std::cout << s << std::endl;
}


Works fine for me. http://ideone.com/1bJaE
Aug 23, 2012 at 8:41pm
Show the code of the function.
Aug 23, 2012 at 10:55pm
Hi thank for your reply, it didn't work for me, this is the code:

1
2
3
4
5
string s = "To begin the game press SPACE";
cout << s << endl;
s += "Good luck!";

message = TTF_RenderText_Solid(font, s.c_str(), textColor );


I want to display:
To begin the game press SPACE
Good luck!


Here's the header of the function where I render the text:

SDL_Surface *TTF_RenderText_Solid(TTF_Font *font, const char *text, SDL_Color fg)

thanks
Last edited on Aug 23, 2012 at 11:01pm
Aug 23, 2012 at 11:00pm
You did not show your code so it is not clear what is the problem. If to consider the code from your previous message then it is simply to do what you want

1
2
3
4
5
string s = "Hello world";

s += '\n';
s += "here we go";
cout << s << endl;


And it would be more simply to initialize the string with the complete phrase

1
2
3
string s = "Hello world\nhere we go";

cout << s << endl;

Last edited on Aug 23, 2012 at 11:03pm
Aug 23, 2012 at 11:37pm
Sorry, it was a delay between I re edited my post and your response.

I did what you suggested, it didn't work though. Maybe it has to do with the funtion I mentioned before:
SDL_Surface *TTF_RenderText_Solid

here's the "conflictive" part of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 string s = "Hello world\nhere we go";
  cout << s << endl;

  mainscreen->apply_surface(0,0, mainscreen->getBackground(), mainscreen->getScreen());

  message = TTF_RenderText_Solid(font, s.c_str(), textColor );
  
  if (message == NULL)
  {
            SDL_FreeSurface(message);
            message = NULL;
  }

  mainscreen->apply_surface(280, 140, message, mainscreen->getScreen());

  SDL_UpdateRect(mainscreen->getScreen(), 0, 0, 0, 0);


thanks


Last edited on Aug 24, 2012 at 1:45am
Aug 25, 2012 at 5:17am
Hello, the code all of you suggested works :), but not with SDL the render text function, wich doesn't recognize new lines. Here was the issue.

So, thanks so much four your help, I learnt something new.

Regards

rossig
Last edited on Aug 25, 2012 at 5:18am
Topic archived. No new replies allowed.