Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. cin does not read input properly
Forum Updated to NodeBB v4.3 + New Features

cin does not read input properly

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tiegert
    wrote on last edited by
    #1

    I am just starting with QT6 (and with QT in general).

    I have tried to do simpe cin/cout operations and it's already troublesome. Somehow cin does not read a line properly, nor does getline.

    Here is my code:

    #include <QCoreApplication>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void do_something()
    {
        string txt = "";
        cout << "Bitte input: ";
        getline(cin, txt);
        cout << "Das war der input " << txt << endl;
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        do_something();
        return a.exec();
    }
    

    I run the program in the terminal not in the QT Creator output!

    My Problem is, that when i remove the endl from the second cout, cin does not read any text and the program is stuck.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You assumption is wrong.
      std::endl forces flushing the output buffer. Without this you may not see what you've typed in because the buffer is not flushed at all. Don't see what this has to do with Qt though.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tiegert
        wrote on last edited by tiegert
        #3

        with your suggestion i have tried to flush the output buffer manually. that did not work.

        In addition i have tried to use just the same code snippet, put it into a .cpp file, compiled it and it just does what i expect it to do

        when i debugg it, it does get stuck in the line of "getline(cin, txt);" and it never reaches the cout in the next line. so why would that have anything to do with the output buffer?

        EDIT:
        actually i have tried to flush the output before the cout command at the end. that did not work.
        but flushing after that did indeed help!

        What i still don't know is, why the debugger cannot go over the "getline.." line.
        And why does flushing the buffer before cout not help?

        Christian EhrlicherC 1 Reply Last reply
        0
        • T tiegert

          I am just starting with QT6 (and with QT in general).

          I have tried to do simpe cin/cout operations and it's already troublesome. Somehow cin does not read a line properly, nor does getline.

          Here is my code:

          #include <QCoreApplication>
          #include <iostream>
          #include <string>
          
          using namespace std;
          
          void do_something()
          {
              string txt = "";
              cout << "Bitte input: ";
              getline(cin, txt);
              cout << "Das war der input " << txt << endl;
          }
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
          
              do_something();
              return a.exec();
          }
          

          I run the program in the terminal not in the QT Creator output!

          My Problem is, that when i remove the endl from the second cout, cin does not read any text and the program is stuck.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #4

          @tiegert said in cin does not read input properly:

          I run the program in the terminal not in the QT Creator output!

          when i debugg it

          How do you debug it if you don't run it from Qt Creator?

          1 Reply Last reply
          0
          • T tiegert

            with your suggestion i have tried to flush the output buffer manually. that did not work.

            In addition i have tried to use just the same code snippet, put it into a .cpp file, compiled it and it just does what i expect it to do

            when i debugg it, it does get stuck in the line of "getline(cin, txt);" and it never reaches the cout in the next line. so why would that have anything to do with the output buffer?

            EDIT:
            actually i have tried to flush the output before the cout command at the end. that did not work.
            but flushing after that did indeed help!

            What i still don't know is, why the debugger cannot go over the "getline.." line.
            And why does flushing the buffer before cout not help?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @tiegert said in cin does not read input properly:

            And why does flushing the buffer before cout not help?

            Why should flushing the buffer before actually writing something to it should help?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tiegert
              wrote on last edited by tiegert
              #6

              @JonB i do run the output in the Terminal, and not in the Qt Creators Output console. There is an option under Project->Run and then set "Run in Terminal"

              @Christian-Ehrlicher At first i had a wrong understanding of flush, but i looked it up.

              For everyone who reads this thread and is wondering what it does:

              When you try to write something to the terminal, the single characters or words are not immediatly written to the terminal.
              They get buffered until it gets decided that the text should now be shown in the terminal.
              The decicion to output the text to the terminal and clear the buffer for new input is made by flush or implicitly by std:endl.

              What confused me a lot in the first place was, that when using cpp outside of Qt i did not have to do that.
              But the Terminal in which i ran my program, always shows some output after my program ran.
              I guess that the Terminal i use flushes the buffer when displaying text and my output also get's shown then.

              D 1 Reply Last reply
              0
              • T tiegert

                @JonB i do run the output in the Terminal, and not in the Qt Creators Output console. There is an option under Project->Run and then set "Run in Terminal"

                @Christian-Ehrlicher At first i had a wrong understanding of flush, but i looked it up.

                For everyone who reads this thread and is wondering what it does:

                When you try to write something to the terminal, the single characters or words are not immediatly written to the terminal.
                They get buffered until it gets decided that the text should now be shown in the terminal.
                The decicion to output the text to the terminal and clear the buffer for new input is made by flush or implicitly by std:endl.

                What confused me a lot in the first place was, that when using cpp outside of Qt i did not have to do that.
                But the Terminal in which i ran my program, always shows some output after my program ran.
                I guess that the Terminal i use flushes the buffer when displaying text and my output also get's shown then.

                D Offline
                D Offline
                dreamfly
                wrote on last edited by
                #7

                @tiegert This is a life saver. Who would have thought we need to check that little "Run in terminal" checkbox after clicking the Projects icon on the left. This should have been the default. Otherwise where do we see the output from our "hello world" app? If we see that in the Application Output, naturally we would attempt to key in our data there. If Application Output for output only then make the window read-only. Still suffering from the first experience....can't even run the app outside of Qt Creator as lots of missing dlls...I guess I have been spoiled by Microsoft....

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved