Ridiculous problem with printf

Started by
24 comments, last by Rydinare 15 years, 9 months ago
I'm having a problem with printf functions, they won't work at all for me, I'm sure theres an explanation for why, but I'll be damned if I can work it out. I think the error is likely to be in the makefile, I think I'm missing a library or something silly like that. The offending funcion:
int main(int argc, char* argp[])
{
	if(argc != 2)
	{
		printf("Ussage: 3DWorld.exe [Window Type]\n\n\n[Window Type]\n  0 - Windowed\n  1 - Full Screen");
		system("PAUSE");
		exit(0);
	}

	//handles windowing type, 0 windowed, 1 full screen
	if(strcmp (argp[1], "0") == 0)
	{
		screenType = 0;
	}
	else if(strcmp(argp[1], "1") == 0)
	{
		screenType = 1;
	}
	else
	{
		printf("Error");
		system("PAUSE");
		exit(0);
	}
	WinMain(0, 0, 0, 0); //Nulls sent to WinMain same as CMD appears to send
}
All that happens, at best, is a new CMD window opens with the "Press any key to continue . . ." but no output (even when ran from an instance of CMD). The libraries linked in are "-mwindows -lopengl32 -lglu32 -lstdc++" I know that peice of code itself works fine as I've compiled it working in Code::Blocks without any other functions or libraries. I've had this problem a couple of days, and have taken a wizz on Google to see if I can find anything, but nothing yet, so just wondering if anyone here can help me out? Thanks. -Aura
Advertisement
Well, since the printf will only execute if argc != 2 I'm assuming that argc accidentally is equal to two (hint: your usage shows argc == 3... the 3DWorld.exe counts).

Have you tried to set a breakpoint on that line? Have you tried to move it outside of the if statement? What have you tried which didn't work, before asking us?
if(argc != 2)

Your program clearly expects one command line argument. So argc should be equal to two in the working case, therefore you should see no print out.

As a hint for future problems of this type, if you think printf doesn't work, try something like this before assuming that it is the printf api that has the problem. [grin]

#include <stdio.h>void main(){  printf("Testing 1, 2, 3...");}
Nope, I know that the name counts as the first argument so I've just been running 3DWorld.exe, and not 3DWorld.exe 1 or whatever, and besides, to hit a ' system("PAUSE"); ' I have to have the printf first, which just doesn't happen.

Thanks again.
-Aura

EDIT:
@smitty1276:
Already tried it, printf isn't working. I'm not blaming the API because I'm almost certain is a fault of my own (I've even compiled from the MinGW in the Code::Blocks folder to make sure).
OK, sorry if that came across as insulting. It was meant to be funny. :-) So, what are you passing in for your cmd line args?

EDIT: Also, I misread the usage statement and thought I saw 2 arguments, but it was [window type] and then its description later.
Quote:Original post by smitty1276
OK, sorry if that came across as insulting. It was meant to be funny. :-) So, what are you passing in for your cmd line args?

EDIT: Also, I misread the usage statement and thought I saw 2 arguments, but it was [window type] and then its description later.


In the future, let's have you be a little more thoughtful when making your attempts at humor.

Could an officer please close this thread?
Everyone hates #1.That's why a lot of idiots complain about WoW, the current president, and why they all loved google so much when it was new.Forget the fact that WoW is the greatest game ever created, our president rocks and the brainless buffons of America care more about how articulate you are than your decision making skills, and that google supports adware, spyware, and communism.
Thats alright, I'm just a bit tired, ignore my edginess.

I've tried various things:
3D_World.exe //argc = 1
3D_World.exe 2 //argp[1] != 0 || 1
3D_World.exe 0 0 // argc = 3
3D_World.exe 0 // argc + argp correct

the only one which works as expected is 3D_World.exe 0, which gives me a windowed loading screen, all the others close as they should do, but don't give me an output.

-Aura
Weird. A couple of things to try:

  • fprintf(stderr, "Hello");
    This will print to the stderr stream instead of stdout. They're usually the same but maybe your system is redirecting one of them for some reason

  • fflush(0);
    This will flush all output streams, sending the data to the screen if it's buffered.



I know I'm reaching, but I can't see what your problem would be.
Quote:Original post by Auraomega
The libraries linked in are "-mwindows -lopengl32 -lglu32 -lstdc++"


It's been a while since I last compiled a Win32 app, but I think the -mwindows flag makes your program use the Win32 (i.e. not Console) subsystem. That means your process is not given a console when it starts -- probably executing system("pause"); makes the system allocate a console for your app -- but at the point at which the printf call is made, there is no console yet and so the output goes nowhere.

Try dropping the -mwindows flag -- or allocate yourself a console.

This topic is closed to new replies.

Advertisement