Obtaining user input without interrupting a timer cycle

I am writing a class which consists of member functions activated by True mod operations using lapTime below.  I have a member function of my class calling itself and I want the screen to update a menu choice which allows the user to interact with the class.  The problem is using cin or get waits for user input which throws off the execution of my functions when the time is right for them to do so.

an example:
void evaluate()
{
prompt_user();
if int(myTimer.lapTime()%20==0)
        eat();
evaluate();
}

how can I write code to grab input if it is present in the input stream but ignore the cin (or other appropriate command) until input is actually present?  

I have included the public members of the systimer class I am using as a reference. (David Levine, Chris Nevison wrote systimer.h and systimer.cpp)


class SysTimer
{
  public:
    SysTimer();             // constructor
    void start();           // begin timing
    void stop();            // stop timing
    void reset();           // reset timer to 0; stops watch
                                if running
    bool isRunning()const;  // true iff stopwatch is                                            running
    double lapTime()const;  // number of microseconds since start
    double elapsedTime()const;  // microseconds between start
                                      and stop
    double cumulativeTime() const;  // returns time since last reset
    double granularity()    const;
tenetjaxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

tenetjaxAuthor Commented:
Edited text of question.
0
tenetjaxAuthor Commented:
Edited text of question.
0
tenetjaxAuthor Commented:
Edited text of question.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

nietodCommented:
>> can I write code to grab input if it
>> is present in the input stream but
>> ignore the cin (or other appropriate
>> command) until input is actually
>> present?
Not in standard C++.  In standard C++ all I/O is buffered and is perfomed blocking.  This means the input can not be obtained until after the user has pressed the enter key and that any attempt to read input will wait until the input is read.

Now almost all operating systems provide ways to obtain input that does not have these restrictions.  But use of these techniques will mean that your code cannot be proted to other operating systems.  For example in Windows you can use the console API functions Like ReadConsole().

what Os are you using?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tenetjaxAuthor Commented:
I am using Windows 95 and this assignment is one I am doing with my computer science class.  I am trying to teach them how to use classes and thus our programs will not be ported to another os.  if you could give me more information on readconsole() or similar process in windows I would appreciate it, and do you have an alternate method of facilitating user interaction which will not interupt a timer?
0
nietodCommented:
Here a a few usefule procedures using the windows console API functions, they should get you started.

If you search in the help, you will find them well documented and you will find others.
*****************************

#include <stdio.h>
      #include <string.h>
      #include <stdlib.h>
      #include <windows.h>
      FILE*fp;

enum Color
{
   Red,
   Green,
   Blue,
   Yellow,
   Purple,
   Cyan,  
   White,
   Black
};

void GotoXY(int X,int Y)
{
      HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
      COORD Coord = {X,Y};
      SetConsoleCursorPosition(StdOut,Coord);
}
void OutputStr(const char *S)
{
      HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
      int StrLen = strlen(S);
      DWORD LenWrt;
      WriteConsole(StdOut,S,StrLen,&LenWrt,NULL);
}
void OutputStr(int X,int Y,const char *S)
{
      GotoXY(X,Y);
      OutputStr(S);
}

void ClearScreen()
{
   HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   CONSOLE_SCREEN_BUFFER_INFO BufInf;
   COORD Origin = {0,0};
   DWORD LenWrt;

   GetConsoleScreenBufferInfo(StdOut,&BufInf); // Get screen rows and columns.
 
   int ChrCnt = BufInf.dwSize.X * BufInf.dwSize.Y; // Number of chars on screen.
   
   FillConsoleOutputAttribute(StdOut,0,ChrCnt,Origin,&LenWrt);
   FillConsoleOutputCharacter(StdOut,' ',ChrCnt,Origin,&LenWrt);
}
void SetColor(Color TxtCol,Color BckCol)
{
   HANDLE StdOut = GetStdHandle(STD_OUTPUT_HANDLE);
   WORD   Col    = 0;

   switch (TxtCol)
   {
   case Red:    Col |= FOREGROUND_RED;   break;
   case Green:  Col |= FOREGROUND_GREEN; break;
   case Blue:   Col |= FOREGROUND_BLUE;  break;
   case Yellow: Col |= FOREGROUND_RED | FOREGROUND_GREEN;   break;
   case Purple: Col |= FOREGROUND_RED | FOREGROUND_BLUE;    break;
   case Cyan:   Col |= FOREGROUND_GREEN | FOREGROUND_BLUE;  break;
   case White:  Col |= FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; break;
   }
   switch (BckCol)
   {
   case Red:    Col |= BACKGROUND_RED;   break;
   case Green:  Col |= BACKGROUND_GREEN; break;
   case Blue:   Col |= BACKGROUND_BLUE;  break;
   case Yellow: Col |= BACKGROUND_RED | BACKGROUND_GREEN;   break;
   case Purple: Col |= BACKGROUND_RED | BACKGROUND_BLUE;    break;
   case Cyan:   Col |= BACKGROUND_GREEN | BACKGROUND_BLUE;  break;
   case White:  Col |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE; break;
   }
   SetConsoleTextAttribute(StdOut,Col);
}


*****************************
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.