Solved

C++ INPUT COMMAND

Posted on 2000-02-19
7
228 Views
Last Modified: 2008-01-16
I'm trying to write a program that simulates a HDLC (high-level data-link control) flag. The program will received characters from the keyboard until it detects the flag. Upon detection of the first flag, the program will continue to receive characters from the keyboard until a flag is detected again. The output of the program will be the data between the two flags. The constraint for this program is the person has to enter from the keyboard WITHOUT hitting the "enter" key. That means as soon as he or she types a character, the program will start to process the data. I have all the different loops required and I'm using array strings to compare with the flag and save the output to a different array for output. But I can't figure out the C++ command that allows the person to enter characters WITHOUT having to hit the "enter" key. I thought cin.get() will do the trick but this command still requires the user to hit the "enter" key.
0
Comment
Question by:romyd
  • 5
  • 2
7 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2539043
Sorry, but in standard C++ this is impossible to do.  In standard C++ all input is line buffered, that means that the input is not actually made available to your program until the user finsihes the line (by pressing enter).  In other words, there are no standard C++ functions or techniques to do what you want.

However, there are OS-specific and sometimes compiler-specific methods to do this.  But if you use these methods, your program probably cannot be proted to other OSes or other compilers, at lest not without changes.

So what OS and what compiler are you using?

Note for windows you can use ReadConsoleInput() and for UNIX you can use the curses library to acheive this.
0
 

Author Comment

by:romyd
ID: 2539348
The fact that standard C++ can't do what I want is not the answer that I was hoping for but it looks like a valid answer. I'm using MICROSOFT VISUAL C++ 5.0. Is ReadConsoleInput() used in Visual C++? Thanks for the quick reply.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2539621
>> is not the answer that I was hoping for
Really? you are the only one person that has ever epressed anoyance at that--today.  Its to maintain pertability since its impossible to do this on some computers, like mainframes.

>> Is ReadConsoleInput() used in Visual C++?
It is part of the windows API, so it is available from any programing language on windows.  (I.e. you can use it from pascal or assembly too.)

I have soem functions that may be of help to you in getting started using the windows console functions (this is one of the consiole functions).
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 22

Expert Comment

by:nietod
ID: 2539622
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
 
LVL 22

Expert Comment

by:nietod
ID: 2539634
These functions are closer to what you need (althogu the ones above might be very handy) but I had to extract them from a library and change the code so ti works without the library.  The results are untested, so you may have small compile problems.  But they should be easy to fix.


//------------------------------------------------------------------------------------------------//
// PROC:   PUBLIC                                                                                 //
// TYPE:   BASCON                                                                                 //
// TITLE:  Set Wait Mode.                                                                         //
//      This procedure places the console in "wait"/"non-wait" mode.                              //
//                                                                                                //
//      When the console is in wait mode, input typed by the user is automatically displayed on   //
// the console and the input is line buffered.  That is, the input typed by the user is not read  //
// until the user presses <enter>.  In this mode the user may backspace over the text they have   //
// typed to make corrections.                                                                     //
//                                                                                                //
//      When the console is not in wait mode, the input typed by the user is not automatically    //
// displayed on the console and the input is not buffered.  That is, the characters can be read   //
// as soon as they have been typed.  The user does not have the ability to backspace over input   //
// to make corrections, unless the calling program implements such a feature manually.            //
void
ConSetWatMod(bool WatFlg)                        // Should console be placed in wait mode?        //
{
   DWORD Mod;                                    // Current mode.                                 //
    HANDLE StdInpHndt = GetStdHandle(STD_INPUT_HANDLE);
   GetConsoleMode(StdInpHnd,&Mod);               // Get the console mode.                         //
   if (WatFlg)                                   // If in wait mode, then                         //
      Mod |= ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT; // Enable echo and line buffering.            //
   else                                          // Otherwise, if in non-wait mode, then          //
      Mod &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT); // Disable echo and line buffering.        //
   SetConsoleMode(StdInpHnd,Mod);                // Set the console mode.                         //
}

//------------------------------------------------------------------------------------------------//
// PROC:   PUBLIC                                                                                 //
// TYPE:   BASCON                                                                                 //
// TITLE:  Get Wait Mode.                                                                         //
//      This procedure returns a boolean indicating if the console is in "wait mode"
//                                                                                                //
//      When the console is in wait mode, input typed by the user is automatically displayed on   //
// the console and the input is line buffered.  That is, the input typed by the user is not read  //
// until the user presses <enter>.  In this mode the user may backspace over the text they have   //
// typed to make corrections.                                                                     //
//                                                                                                //
//      When the console is not in wait mode, the input typed by the user is not automatically    //
// displayed on the console and the input is not buffered.  That is, the characters can be read   //
// as soon as they have been typed.  The user does not have the ability to backspace over input   //
// to make corrections, unless the calling program implements such a feature manually.            //
bool                                             // Is the console in wait mode?                  //
ConGetWatMod(void)
{
   DWORD Mod;                                    // Current mode.                                 //
   HANDLE StdInpHndt = GetStdHandle(STD_INPUT_HANDLE);
 
   GetConsoleMode(StdInpHnd,&Mod);               // Get the current console mode.                 //
   return (Mod & ENABLE_LINE_INPUT) != 0;        // Indicate if line buffering is enabled.        //
}


//------------------------------------------------------------------------------------------------//
// PROC:   PUBLIC                                                                                 //
// TYPE:   BASCONINP                                                                              //
// TITLE:  Get Character.                                                                         //
//      This procedure reads a character typed by the user.  The charcter read is not echoed to   //
// the screen.                                                                                    //
char                                             // Character typed by the user.                  //
ConGetChr(void)
{
   char  Chr;
   DWORD ChrRed;
   bool  SavWatMod = ConGetWatMod();            // Original wait mode.                           //
    HANDLE StdInpHndt = GetStdHandle(STD_INPUT_HANDLE);

   ConSetWatMod(false);
   ReadConsole(StdInpHnd,&Chr,1,&ChrRed,NULL);
   ConSetWatMod(SavWatMod);                      // Restore the wait mode.                        //
   return Chr;
}


If you study these functions and the related functions in the VC help you will see you have options for controlling virtually all aspects of the display and input.  You can position the cursor, resize it, scroll portions of the display, change colors of text output, read input without waiting for the enter key, read input without echoing (having the characters typed appear on the screen) etc.  
0
 

Author Comment

by:romyd
ID: 2540194
Thanks so much,
These functions will be very useful. I'll try them out. You clearly have a lot of experience in this field. Is there any way I can give you more "expert" points.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2540282
Not once th question is closed (answer accepted)  You can ask a "dummy question" for me just as a way to award points, but that really isn't necessary.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question