adding new line to string literal

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



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
Show the code of the function.
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
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
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
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
Topic archived. No new replies allowed.