repeat until any key

This is a discussion on repeat until any key within the C++ Programming forums, part of the General Programming Boards category; Hey, I have a really simple problem, but for some reason can't work it out! I just want a loop ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    1

    repeat until any key

    Hey,

    I have a really simple problem, but for some reason can't work it out!

    I just want a loop to keep repeating itself with no user input forever, or until the user presses enter, or enters another key.

    Any suggestions?

    Thanks

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    I believe there's a stream function that peaks at a stream to see if there is anything in the buffer. You could then in an loop check if there is a character in cin, as you print one character at a time to cout. Just be sure that autoflush is on, or that you flush after each character is printed.

    EDIT: found it: streambuf::in_avail. You'll need to call cin.rdbuf() to get the buffer.
    Last edited by King Mir; 01-23-2008 at 07:36 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    if you are just waiting for the user to press enter, this is a bad idea, because your program will be using 100% cpu in the empty loop. Block for input instead. That way processor time is yielded to other processes.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    The next-best solution is just to sleep 20 or 30 ms or so between each check to minimize cpu use.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    It still won't work, because in_avail() only checks the stream's internal buffer in most implementations. Even when the user gives input and hits enter, this buffer won't change until an actual read is attempted.

    You need platform-specific functionality for this.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

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