Console Application: unexpected QTextStream(stdin) behavior



  • @
    QTextStream qtout(stdout);
    QTextStream qterr(stderr);
    QTextStream qtin(stdin);

    QString getFileName() {

    QString fileName = "";
    do {
        qtout << "Enter a filename: " << flush;
        fileName = qtin.readLine();
        qtout << "You entered: \"" << fileName << "\"" << endl << endl;
    } while (fileName.isEmpty()); // the user must enter a filename
    
    //check to see if the file exists
    QFile data(fileName);
    if(data.exists()) { //this the file exists
        qtout << "A file with that name already exists. " << endl
              << "If you continue it will be over-written." << endl
              << "Do you wish to continue? " << endl
              << "1) yes \n2) no \n3) to change filename" << endl
              << endl;
    
        int choice = 0;
        do {
            qtout << "Enter 1, 2 or 3: " << flush;
            qtin >> choice;
            qtout << endl << flush;
            if(qtin.status() != QTextStream::Ok) {
                qtin.reset();
                qtin.resetStatus();
                QString garbage = "";
                garbage = qtin.readLine();
                qtout << "You must enter a number!" << endl;
            }
        } while (!(choice == 1 or choice == 2 or choice == 3));
    
        if (choice == 1) {
            return fileName;  //overwrite the file
        } else if (choice == 2) {
            return QString(); //user does not wish to continue
        } else {
            return getFileName(); // recursively attempt to get a good filename
        }
    }
    return fileName;  //file did not exist (hopefully)
    

    }@

    Output:
    @Enter a filename: vitr
    You entered: "vitr"

    A file with that name already exists.
    If you continue it will be over-written.
    Do you wish to continue?

    1. yes
    2. no
    3. to change filename

    Enter 1, 2 or 3: 3

    Enter a filename: You entered: ""

    Enter a filename: vitry
    You entered: "vitry"

    A file with that name already exists.
    If you continue it will be over-written.
    Do you wish to continue?

    1. yes
    2. no
    3. to change filename

    Enter 1, 2 or 3: 3

    Enter a filename: You entered: ""

    Enter a filename: vitryu
    You entered: "vitryu"

    Read data from the file - watch for errors.@

    Notice how on line 13 and on line 27 of the output the program doesn't wait for input. Yet on the initial call of the function it waits properly. I don't understand why it doesn't stop and wait for input on line 6 when the function is called recursively. Can anyone help me understand why it's doing this or help me fix it?



  • Hi, when you press 1, 2 or 3, what you have pressed is

    @
    1\n, 2\n or 3\n
    @

    And you forget to discard the \n before you call readLine()



  • Thank you!

    That makes a lot of sense. It just seems like that trailing '\n' should be discarded from the stream. How would you suggest I discard the '\n'?

    readLine()?

    This works, I was just wondering what you would suggest?


Log in to reply
 

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