Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

How would you convert this to C++ from C?

This topic is 5204 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
  {
    printf("Unable to init SDL: %s\n", SDL_GetError());
    exit(1);
  }
 
I was thinking it would be like
  if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
  {
    cout << "cannot initialize" << SDL_GetError()
  }
 
but I get errors, please help.Thanks Learning C++ was hard. Trying to relearn C++ sucks. Hopefully I remember everything and learn it right this time. [edited by - Thrust on February 20, 2004 11:05:27 PM] [edited by - Thrust on February 20, 2004 11:08:54 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
What are the errors?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Check the semicolon on the cout line

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
And how exactly does this relate to graphics programming ?

Share this post


Link to post
Share on other sites
1) This is not a graphics question. This is General Programming. If you had a question specifically about SDL, that would be the OpenGL forum, and if your question was about graphics theory or non-API-specific rendering, that''s for Graphics.

2) printf() is superior to cout. printf can print formatted data, like "%.2f" which prints a number to two decimal places, like "1066.34", or "0x%X" which prints a hex number like "0x1F". The only reason to use cout is that it involves less typing if you don''t care about formatting.

3) You forgot a semicolon at the end of the line that calls cout. Should be:
cout << "Unable to init SDL: " << SDL_GetError() << endl;

4) You forgot to specify "using namespace std" as someone already pointed out. I always forget that too. What makes it more confusing is gcc uses that namespace by default, but VC++ does not.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Post screenshots of your projects. There''s already 134 screenshot posts.

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
1) This is not a graphics question. This is General Programming. If you had a question specifically about SDL, that would be the OpenGL forum, and if your question was about graphics theory or non-API-specific rendering, that''s for Graphics.

2) printf() is superior to cout. printf can print formatted data, like "%.2f" which prints a number to two decimal places, like "1066.34", or "0x%X" which prints a hex number like "0x1F". The only reason to use cout is that it involves less typing if you don''t care about formatting.

3) You forgot a semicolon at the end of the line that calls cout. Should be:
cout << "Unable to init SDL: " << SDL_GetError() << endl;

4) You forgot to specify "using namespace std" as someone already pointed out. I always forget that too. What makes it more confusing is gcc uses that namespace by default, but VC++ does not.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Post screenshots of your projects. There''s already 134 screenshot posts.


You can format with cout too... it is all in the std library.

Share this post


Link to post
Share on other sites
cout << "Decimal " << dec << 0xFF;
cout << "Octal " << oct << 0xFF;
cout << "Hex " << hex << 0xFF;

try swapping 0xFF with 255 in these instances as well

love cout, can''t beat simplicity

Share this post


Link to post
Share on other sites
quote:
Original post by Pigpen
cout << "Decimal " << dec << 0xFF;
cout << "Octal " << oct << 0xFF;
cout << "Hex " << hex << 0xFF;

try swapping 0xFF with 255 in these instances as well

love cout, can't beat simplicity

I don't get it. Are dec/oct/hex constants that are defined somewhere, like endl, that indicate the following value should be in that format? And I still don't see how it can give you all the formatting printf can (decimal places, mainly). I'm sure it can be done, but probably only by using std::string's functions.

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Post screenshots of your projects. There's already 134 screenshot posts.

[edited by - CGameProgrammer on February 21, 2004 1:58:24 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by CGameProgrammer
I don''t get it. Are dec/oct/hex constants that are defined somewhere, like endl, that indicate the following value should be in that format?


They aren''t constants. They''re manipulators. For formatting numbers, in particular decimal places, try looking up the std::setprecision(), std::setw(), std::setfill(), and std::setiosflags() manipulators. This can all be done without touching std::string.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!