tags:

views:

37

answers:

1

Compiling this code with netbeans 6.8 C++ (mingw)

    for(int i=0;i<100;i++)
    {
        printf("\r\ntest");
    }

    getchar();

It don't print one hundred times the "test" word..

It just execute the getchar() prior the printf loop

It's a netbeans problem, because it executes trough c:\msys\1.0\bin\sh.exe

obviously if I try it with command line it works right

why? anyone knows how to solve it? thanks

+1  A: 

I'm going to move the answer from comments to answer, but as a Community Wiki. I wouldn't want to write some "pseudo-definitive" answer which was really a stab in the dark, as this is.

Based on the symptom of output not appearing on the terminal, I suggested that you add fflush(stdout) prior to the getchar() call. The fflush() is a standard C library function which "flushes" pending output or input.

I just guessed that your printf() output was going into a buffer and that calling fflush(stdout) would cause that buffer to be sent to the terminal, solving your symptom.

You could possibly use the following macro and variadic function to allow that you will fflush after every printf:

/*
 * If the environment supplies vfprintf():
 */
#define printf    my_printf

int my_printf(const char * fmt, ...)
{
    int ret_val;
    va_list var_args;

    va_start(var_args, fmt);
    ret_val = vfprintf(stdout, fmt, var_args);
    va_end(var_args);

    fflush(stdout);

    return ret_val;
}

otherwise maybe:

/* If the environment lacks vfprintf() */
#define my_printf(args)  ((void)printf args, (void)fflush(stdout))

/*
 * Usage:
 * 
 * my_printf(("Format %s\r\n", "string"));
 *
 */
Heath Hunnicutt
that solves it, but I was waiting some solution from netbeans IDE side, or something, change all my source is the last thing to try =)
Hernán Eche