Solved

C++ INPUT COMMAND

Posted on 2000-02-19
7
224 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now