Jump to content
  • Advertisement
Sign in to follow this  
Mr_Threepwood

cout with SDL

This topic is 3940 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

Ok this is a stupid error that seems fine to me but for some reason isn't working. Here's my code:
#include "SDL/SDL.h"

#include <cstdlib>
#include <iostream>

int main(int argc, char *argv[])
{

    std::cout << "Program starting";

    system ("PAUSE");

    return 0;
}
 
The thing that is really vexing me is that the above code doesn't print to the console, but if I comment out the SDL header it does. I need to be able to print stuff to the console while I'm using SDL so that I can debug parts the program that I'm making (connect 4). Does anyone know why this would be happening?

Share this post


Link to post
Share on other sites
Advertisement
Two reasons:
- With SDL the console is disabled. SDL uses windows, not consoles
- SDL redirects the standard output (both cout and printf()) to a file named... oh I can't remember now but it's like stdout.txt or something like that. It should be in the .exe's directory. You can, of course, change this using the standard library.

And finally, learn to use a good debugger. Printing values out is nice and all, but it pales in comparison to breakpoints, stepping through code, call stacks, and all the goodies that come with a debugger. Investing the time to learn how to use a debugger is truly well worth it.

Share this post


Link to post
Share on other sites
if you really want to use the console to display information about your program as its running you can look at AllocConsole and its functions to create a console window and write text to it.

Share this post


Link to post
Share on other sites
If you're using Visual Studio, you can go into the project properties and change one of the settings so that a console window does pop up. It's Linker -> System -> SubsSystem, and you make sure it's set to "Console (/SUBSYSTEM:CONSOLE)"

Share this post


Link to post
Share on other sites
I didn't think that SDL compiled with the subsystem set to CONSOLE. Anyone care to confirm/deny that? I'm pretty sure it has to be windows.

Share this post


Link to post
Share on other sites
The prefered way to include the SDL headers is by #include "SDL.h".

In MSVC you can simply set the subsystem in the linker settings to CONSOLE and you get a console window where cout will output.

If you are using MinGW/GCC you can simply write #undef main before the main function and SDLs init code which redirects output to a file won't be called.
This way cout also writes to the console. Doing this does not cause any problems afaik.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ezbez
And finally, learn to use a good debugger. Printing values out is nice and all, but it pales in comparison to breakpoints, stepping through code, call stacks, and all the goodies that come with a debugger. Investing the time to learn how to use a debugger is truly well worth it.
Seconded.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!