[SOLVED] QTextStream nonblocking peek and CLEAR '\n' from internal QTextStream buffer



  • Hi there,

    so I've started giving Qt a try.

    How well designed is QTextStream? Where is the fitting peek function?

    Here's what I want to do. Using the well-designed Standard C++ library, it works perfectly:
    @
    #include <iostream>
    #include <string>

    int main()
    {
    using namespace std;

    cout << "Enter a number: ";
    int i;
    cin >> i;
    if (!cin) {
    cerr << "err\n";
    return 1;
    }

    cout << "Getline: [enter for default "a b c"] ";
    string line;
    if (cin.peek() == '\n') {
    cin.ignore(1, '\n');
    }
    getline(cin, line);
    if (!cin) {
    cerr << "err\n";
    return 1;
    }

    if (line.size() == 0)
    line = "a b c";

    cout << "\n\n";
    cout << "Got number: " << i << " and the following line:\n"
    << line << '\n';

    return 0;
    }
    @

    But here's an attempt using Qt's QTextStream. It fails miserably. Please help.
    @
    #include <QTextStream>

    QTextStream cout_qt(stdout);
    QTextStream cin_qt(stdin);
    QTextStream cerr_qt(stderr);

    int main()
    {
    cout_qt << "Enter a number: " << flush;
    int i;
    cin_qt >> i;
    if (cin_qt.status() != QTextStream::Ok) {
    cerr_qt << "err\n";
    return 1;
    }

    cout_qt << "Getline: [enter for default "a b c"] ";
    QString line;

    /*
    HOW DOES ONE CLEAR '\n' FROM cin's BUFFER HERE???
    ============================
    */

    line = cin_qt.readLine();
    if (cin_qt.status() != QTextStream::Ok) {
    cerr_qt << "err\n";
    return 1;
    }

    cout_qt << "\n\n";
    cout_qt << "Got number: " << i << " and the following line:\n"
    << line << endl;

    return 0;
    }
    @

    I've already tried all kinds of things. cin_qt.skipWhiteSpace(), cin_qt.device()->peek(1), cin_qt.device()->bytesAvailable
    All to no avail...

    Is it even possible? Is the design flawed?

    Thanks.



  • Hi

    You could try this....

    @
    #include <QTextStream>

    int main()
    {
    QTextStream cout_Qt(stdout);
    QTextStream cin_Qt(stdin);
    QTextStream cerr_Qt(stderr);
    QString string;
    qint32 number;

    cout_Qt << "Enter a Number: " << flush;
    number = cin_Qt.readLine().toInt();
    cout_Qt << "You entered : " << number << flush << "\n";
    
    cout_Qt << "Enter a string: " << flush;
    string = cin_Qt.readLine().toLatin1();
    cout_Qt << "You Entered : " << string << "\n" << flush;
    

    }
    @

    You may want to place the inputs into a loop until the parameters are met, such as
    @
    while((number < 1) && (number > 10)) {
    get your number code;
    }
    @

    Or this post may be of some help to you as well.. http://stackoverflow.com/questions/21401954/peek-on-qtextstream

    Oh, and don't quote me, Its been a while, but to get rid of the "\n" or any thing, I believe you can do

    @
    string = string.remove("\n");
    @

    Thanks



  • OK I guess that after every use of...

    @
    cin_qt >> i;
    @

    ...I just have to remember to call
    @
    cin_qt.readLine(); // need this to eat the '\n' remaining in cin_qt's buffer
    @

    Here's the complete code

    @
    #include <QTextStream>

    QTextStream cout_qt(stdout);
    QTextStream cin_qt(stdin);
    QTextStream cerr_qt(stderr);

    int main()
    {
    cout_qt << "Enter a number: " << flush;
    int i;
    cin_qt >> i;
    if (cin_qt.status() != QTextStream::Ok) {
    cerr_qt << "err\n";
    return 1;
    }

    cin_qt.readLine();     // need this to eat the '\n' remaining in cin_qt's buffer
    
    cout_qt << "Getline: [enter for default \"a b c\"] " << flush;
    QString line;
    line = cin_qt.readLine();
    if (cin_qt.status() != QTextStream::Ok) {
        cerr_qt << "err\n";
        return 1;
    }
    
    if (line.size() == 0)
        line = "a b c";
    
    cout_qt << "\n\n";
    cout_qt << "Got number: " << i << " and the following line:\n"
            << line << endl;
    
    return 0;
    

    }
    @



  • Hi,

    That is one way to do it, however, I still recommend using @i = cin_qt.readLine();@

    as if for some reason
    @cin_qt >> i;@

    somehow manages to clear the buffer your program will be waiting for the user to hit the enter key with the next call to the blank readLine(). But I'm sure you got the idea now.

    Don't forget to edit your original post and put [SOLVED] in front of it so others will know.

    Thanks.


Log in to reply
 

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