realtime cin

This is a discussion on realtime cin within the C++ Programming forums, part of the General Programming Boards category; Can anyone help me figure out how to create a cin statement that does not halt a program from placing ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    1

    realtime cin

    Can anyone help me figure out how to create a cin statement that does not halt a program from placing output on the screen?

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    I dont quite get the question you are asking, could you elaborate a little?

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Try looking into cin.peek( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Can anyone help me figure out how to create a cin statement that does not halt a program from placing output on the screen?
    cin cannot do this. What you want is a second process/thread of execution. Or you could perform some kind of non-blocking test and read to get some of that functionality:
    Code:
    #include <iostream>
    #include <string>
    
    // Nonstandard
    #include <windows.h> // For sleep
    #include <conio.h>   // For kbhit
    
    void next_bar()
    {
      static size_t pos;
      static std::string spin ( "|\\-/" );
    
      std::cout<< spin[pos++ % spin.length()];
      std::cout<<'\b'<<std::flush;
    
      Sleep ( 100 ); // Slow the bar down a bit
    }
    
    int main()
    {
      std::string name;
      bool done = false;
    
      std::cout<<"Enter your name: "<<std::flush;
    
      while ( !done ) {
        if ( kbhit() ) {
          std::getline ( std::cin, name );
          done = true;
        }
        else
          next_bar();
      }
    
      std::cout<<"Hello, "<< name <<'!'<<std::endl;
    }
    But that isn't nearly as powerful as a second process, and can get cumbersome.

    -Prelude
    My best code is written with the delete key.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Forgot to return 0.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by XSquared
    Forgot to return 0.
    I presume you're referencing Prelude's code? If so, return 0 is not required for a return from main() in C++, as it is the guaranteed default.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >I presume you're referencing Prelude's code?
    Indeed.

    >If so, return 0 is not required for a return from main() in C++, as it is the guaranteed default.
    I didn't know that. VC++ always spits out an error whenever I forget to.

    [edit]
    I guess I was wrong. Checked Stroustrup's site and it does mention that. Cool
    [/edit]
    Last edited by XSquared; 03-17-2003 at 02:59 PM.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin problem
    By mikahell in forum C++ Programming
    Replies: 12
    Last Post: 08-22-2006, 11:14 AM
  2. Check if a cin is null
    By HumbuckeR in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2006, 08:16 AM
  3. getline(function)??
    By dac in forum C++ Programming
    Replies: 7
    Last Post: 03-10-2006, 11:42 AM
  4. cin not allowing input after first use of function
    By Peter5897 in forum C++ Programming
    Replies: 5
    Last Post: 01-31-2006, 05:29 PM
  5. Overriding Cin with Cout
    By Tainted in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2005, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21