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

Started by
15 comments, last by Thrust 20 years, 1 month ago

  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]
Mark St. Jean - OwnerWastedInkVwmaggotwV@Yahoo.com
Advertisement
What are the errors?
Check the semicolon on the cout line
You will probable need this:

#include <iostream>using namespace std;


[edited by - PlayGGY on February 20, 2004 11:15:31 PM]
And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
And how exactly does this relate to graphics programming ?
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.
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
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.

And the rockets' red glare, the bombs bursting in air,gave proof through the fight that our flag was still there.Oh say, does that star-spangled banner yet waveover the land of the free and the home of the brave?
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
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]
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
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.

This topic is closed to new replies.

Advertisement