Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use

"screen.h" header file Functions

Using this header file eliminated the need to include these function definitions into your code each time you wished to use these concepts.  It was particularly useful for beginning programmers who were unfamiliar with the use of functions.
Now, however, you may be ready to delve into the "workings" of these functions and you may wish to include the function definitions in your code.

 

 

 

 

 

Pause (delay) screen output:
delay(5000);

Note:  The argument (the value sent to this function) is time in milliseconds. (5000 = about 5 seconds).  Unlike the system("PAUSE");, no message is displayed on the screen.  If you wish to delay the screen after a cout statement, you will need to "flush" the iostream.
(Using the function definition requires header file <time.h>)
// -------------------------------------------------------------------------------
// delay( ) -- Allow the screen to be "paused" for the
// number of seconds determined by the programmer. Unlike
// the system("PAUSE");, no message is displayed on the
// screen. (5000 = 5 seconds, approximately)
// Requires header file <time.h>
// --------------------------------------------------------------------------------
// Function for delay( )

void delay(long seconds)
{
     clock_t time1 = clock(); // use clock time
     clock_t time2 = time1 + seconds;
     while(time1 < time2)
     time1 = clock();
     return;
}

//Read more about "clock time" under CS2 Library Functions.
 


 

 

 

 

Move the cursor around the screen:
gotoxy(x,y);

Note:  Move the cursor on the screen to any coordinates (x,y).  As in mathematics, x is the horizontal coordinate and y is the vertical coordinate.  Remember that the screen is 80 wide and 22 high and the point (1,1) will be the upper left corner.  You will need to "flush" the iostream between successive uses of gotoxy( , ).
// --------------------------------------------------------------------------
// gotoxy( , ) -- Move the cursor to any location on the
// screen. The upper left corner of the screen will be set
// to the coordinates (1,1) for easier counting of spaces.
// Requires headers <windows.h>
// --------------------------------------------------------------------------
// Function for gotoxy( , )

void gotoxy(int x, int y)
{
     // get the console handle
    
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  
     COORD point;
     point.X = x-1;    // set the new cursor location coordinates
     point.Y = y-1;
     SetConsoleCursorPosition(hConsole, point);
     return;

}
/*A console process uses handles to access the input and screen buffers of its console and you need to know which one is in use.*/
 


 

 

 

 

Get keyboard response - response not visible
getch( );

Note:  getch( ) waits for the user to press a key, returns this response, and moves on.  It will NOT show the response on the screen (no echo).  You will not see what was typed.  It does NOT wait for the user to press the ENTER key.
// --------------------------------------------------------------------
// getch( ) -- Waits for user to hit a key, returns
// this response, and goes on. You will not see the
// user's response on the screen (no echo).
// Requires headers <windows.h> and <conio.h>
// --------------------------------------------------------------------
// Function for getch( )

int getch(void)
{
     int response;
     cout << flush;
     response = _getch();
     HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
     FlushConsoleInputBuffer(hConsole);
     return response;
}

 

 

 

 

 

 

Get keyboard response - response visible
getche( );

Note: getche( ) waits for the user to press a key, returns this response, and moves on.  It WILL show the response on the screen (echo).  You will see what was typed.  It does NOT wait for the user to press the ENTER key.
// ------------------------------------------------------------------------------
// getche( ) -- Waits for user to hit a key, returns
// this response, and goes on. You will see the user's
// response on the screen (echo).
// Requires headers <windows.h> and <conio.h>
// -----------------------------------------------------------------------------
// Function for getche( )

int getche(void)
{
     int response;
     response = _getche();
     HANDLE hConsole = GetStdHandle(STD_INPUT_HANDLE);
     FlushConsoleInputBuffer(hConsole);
     return response;
}
 

 

Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use