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. Qt Creator inner terminal autoinputs 0 when there is cin stream
QtWS25 Last Chance

Qt Creator inner terminal autoinputs 0 when there is cin stream

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 373 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.
  • J Offline
    J Offline
    JacobNovitsky
    wrote on 5 Jan 2024, 08:28 last edited by JacobNovitsky 1 May 2024, 08:33
    #1

    How to solve it?

    #include <iostream>
    #include <limits>  // for std::numeric_limits
    
    int main() {
        std::string key;
    
        std::cout << "Enter a key: ";
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // Clear input buffer
        std::getline(std::cin, key);
    
        // Output the first character's ASCII value
        if (!key.empty()) {
            std::cout << "Key: " << int(key[0]) << std::endl;
        } else {
            std::cout << "Key is empty." << std::endl;
        }
    
        return 0;
    }
    

    if change to gnome-terminal then back it does the trick

    J 1 Reply Last reply 5 Jan 2024, 08:45
    0
    • J JacobNovitsky
      5 Jan 2024, 08:28

      How to solve it?

      #include <iostream>
      #include <limits>  // for std::numeric_limits
      
      int main() {
          std::string key;
      
          std::cout << "Enter a key: ";
          std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // Clear input buffer
          std::getline(std::cin, key);
      
          // Output the first character's ASCII value
          if (!key.empty()) {
              std::cout << "Key: " << int(key[0]) << std::endl;
          } else {
              std::cout << "Key is empty." << std::endl;
          }
      
          return 0;
      }
      

      if change to gnome-terminal then back it does the trick

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 5 Jan 2024, 08:45 last edited by
      #2

      @JacobNovitsky said in Qt Creator inner terminal autoinputs 0 when there is cin stream:

      std::cin.ignore(std::numeric_limitsstd::streamsize::max(), '\n');

      This line is the problem - first enter press is ignored.
      Why don't you use std::cin.clear(); instead?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply 5 Jan 2024, 08:55
      0
      • J JacobNovitsky has marked this topic as solved on 5 Jan 2024, 08:46
      • J jsulm
        5 Jan 2024, 08:45

        @JacobNovitsky said in Qt Creator inner terminal autoinputs 0 when there is cin stream:

        std::cin.ignore(std::numeric_limitsstd::streamsize::max(), '\n');

        This line is the problem - first enter press is ignored.
        Why don't you use std::cin.clear(); instead?

        J Online
        J Online
        JonB
        wrote on 5 Jan 2024, 08:55 last edited by
        #3

        @jsulm
        It comes from https://en.cppreference.com/w/cpp/string/basic_string/getline. I thought that line prior to the getline() just flushes anything which may be sitting in the buffer from before, does not affect the following getline()?

        J 1 Reply Last reply 5 Jan 2024, 08:56
        0
        • J JonB
          5 Jan 2024, 08:55

          @jsulm
          It comes from https://en.cppreference.com/w/cpp/string/basic_string/getline. I thought that line prior to the getline() just flushes anything which may be sitting in the buffer from before, does not affect the following getline()?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 5 Jan 2024, 08:56 last edited by
          #4

          @JonB For me it does affect getline - I enter something, press enter, nothing happens, second enter then works.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply 5 Jan 2024, 09:03
          2
          • J jsulm
            5 Jan 2024, 08:56

            @JonB For me it does affect getline - I enter something, press enter, nothing happens, second enter then works.

            J Online
            J Online
            JonB
            wrote on 5 Jan 2024, 09:03 last edited by JonB 1 May 2024, 09:05
            #5

            @jsulm
            I think then that careful reading of that item must mean you are only supposed to use it if you know there is something already sitting there left over from before and you want to flush it. It must mean ignore everything up to the next newline in the buffer, whenever that is. The OP should certainly try removing it. I don't know what the OP's "autoinputs 0" means, nor why it apparently has different effect in same situation from gnome-terminal.

            1 Reply Last reply
            2

            1/5

            5 Jan 2024, 08:28

            • Login

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