cout with SDL

Started by
14 comments, last by Ezbez 16 years, 3 months ago
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?
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.
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.
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)"
Eric Richards
sdl overloads the << operator so that it writes to a file not the console window so that doesn't work.
It doesn't actually overload the << operator, it just redirects the standard output to a file.
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.
Yes, it can.
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.
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.

Richard "Superpig" Fine - saving pigs from untimely fates - Microsoft DirectX MVP 2006/2007/2008/2009
"Shaders are not meant to do everything. Of course you can try to use it for everything, but it's like playing football using cabbage." - MickeyMouse

This topic is closed to new replies.

Advertisement