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
Forum Updated to NodeBB v4.3 + New Features

Qt Creator inner terminal autoinputs 0 when there is cin stream

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 387 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 last edited by JacobNovitsky
    #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

    jsulmJ 1 Reply Last reply
    0
    • J JacobNovitsky

      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

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on 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

      JonBJ 1 Reply Last reply
      0
      • J JacobNovitsky has marked this topic as solved on
      • jsulmJ jsulm

        @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?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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()?

        jsulmJ 1 Reply Last reply
        0
        • JonBJ JonB

          @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()?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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

          JonBJ 1 Reply Last reply
          2
          • jsulmJ jsulm

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #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

            • Login

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