#### Archived

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

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

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

## 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 on other sites
What are the errors?

##### Share on other sites
Check the semicolon on the cout line

##### Share on other sites
You will probable need this:

#include <iostream>using namespace std;

[edited by - PlayGGY on February 20, 2004 11:15:31 PM]

##### Share on other sites
And how exactly does this relate to graphics programming ?

##### 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( );

##### 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( );

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

##### 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 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( );

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

##### 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 on other sites
yep, but do
printf("% 02.2f")
with cout and maybe add a few more numbers with different formatting. yes, cout can do it.. in a line thats about half a mile long and definitely beat in terms of simplicity (not in terms of readability of course ,-) ). guess you can tell i prefer printf in 99% of all cases, because all the "text" << number << "more test" << another number << endl is driving me crazy ,-)

edit: at the same time, having a dozen variables in one printf might make you loose track of which belongs where.

[edited by - Trienco on February 21, 2004 2:25:02 AM]

##### Share on other sites
quote:
Original post by CGameProgrammer
And I still don''t see how it can give you all the formatting printf can (decimal places, mainly).

You can use setw and setprecision for that.

A lot of smart people thought about the iostreams library for at LEAST a good couple of hours before they went ahead and coded it. The chances that they''d miss something like that are not good.

"Sneftel is correct, if rather vulgar." --Flarelocke

##### 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? 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.
Ahem. In the future, RTFM. Especially before making pronouncements about what can or can''t be done.

##### Share on other sites
quote:
Original post by Trienco
...maybe add a few more numbers with different formatting. yes, cout can do it.. in a line thats about half a mile long and definitely beat in terms of simplicity...
If you absolutely must, boost::format.

##### Share on other sites
One last thing. With printf''s format specifiers, the onus is on you to match the specifier to the variable you''re actually passing. If you get it wrong, it''s not a compiler error, so you will simply see aberrant behavior at runtime. With cout & co, an insertion operator is overloaded for each type making it typesafe. Furthermore, inserting an unexpected type (one for which no insertion operator exists) will generate a compiler error, and inserting the wrong value will generate results consistent with that formatting (several digits of precision if a float is substituted for an int; a letter if a char is substituted for a float, etc).

##### Share on other sites
Moved to General Programming.

##### Share on other sites
I got it to work, thanks.

Learning C++ was hard. Trying to relearn C++ sucks. Hopefully I remember everything and learn it right this time.