Qt Console Application Problems



  • Hello, I am having difficulties to understand why my program is being executed wrong. When I run the program, the console screen appears and awaits from me to enter a number, and when I enter a number then press enter, it then prints the following:

    Hello World
    Please Enter Your Age
    Your Age is
    56

    Any suggestions as why is this happening?? Thank You,
    @
    #include <QTextStream>

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

    QTextStream output(stdout);
    QTextStream input(stdin);
    
    output << "Hello World\n\n";
    output << "Please Enter Your Age:\n";
    
    input >> age;
    
    output << "Your Age is: \n";
    output << age;
    

    }
    @



  • What about:

    @QFile myStdOut;
    file.open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered);

    QTextSTream output(&myStdOut)
    output << "Hello World\n\n";
    output << "Please Enter Your Age:\n";

    input >> age;@

    --

    Note however:

    The "stdout" and "stderr" streams have their own buffering too! If they are connected directly to a "real" console, this is disabled automatically. But as soon as the output of a console application gets redirect, e.g. your console application is created as a child process via QProcess, you will notice the buffering! That means, regardless of the QTextStream or QFile buffering, you should be doing this, in order to make sure outputs aren't delayed:

    @for(int i = 0; i <= 100; i++)
    {
    //Do some work that takes time
    doSomeWork(i);

    //Write current progress
    fprintf(stdout, "Progress: %d %%\n", i);
    
    //required flushing!
    fflush(stdout);
    

    }@



  • Thank you for all of your replies, the output.flush() worked well.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.