What Is Getch() in C++?
In C++, the library named “cstdio” header file is mostly used for the getch() function. Getch() is also defined in the “conio. h” header file that is compiler-specific and it is a non-standard function because it’s not fully the part of the C++ library standard. The abbreviation of getch is “get character” and it is used to stay on the output screen for a while until the user passes the input.
Let us elaborate on this concept properly with the help of examples. Here, we will learn about the syntax, usage, and behavior of the getch() method in C++ with the help of proper scenarios.
Scenario 1: How to Use Getch() in a Menu-Driven Application
Let’s discuss about the menu-driven small application for which we will list different options. Each option is designed for different purposes like a restaurant menu. The code snippet of this example is attached in the following:
#include <conio.h>
#include <iostream>
using namespace std;
int main() {
char choice;
std::cout<<"Resturant Menu:\n";
std::cout<<"1. Option 1: Pizza\n";
std::cout<<"2. Option 2: Burger\n";
std::cout<<"3. Option 3: Cake\n";
std::cout<<"Enter your choice: ";
choice = getch(); // user give input and getch() reads the input without pressing enter button
switch(choice) {
case '1':
std::cout<<"\n you select 'Pizza'\n";
break;
case '2':
std::cout<<"\n You select 'Burger'\n";
break;
case '3':
std::cout<<"\n You select 'Cake'\n";
break;
default:
std::cout<<"\No choice - No order \n";
}
return 0;
}
First, we declare the required standard libraries at the program's start. We create a logic inside the main function, and anything inside the program's main function will be executable. Here, we declare a “choice” variable whose data type is a character. Then, we create a menu that is visible on the console screen. We give the user a “restaurant Menu” with different options like “Pizza”, “Burger”, “Cake”, and “No choice”.
We pass getch() to the variable choice because the user input is stored in this variable and display the output automatically according to the selected option. To select an option from the menu, we use the switch() function and pass “choice” in this function. Inside the “switch” method, we make the cases against each option individually. In the “switch” method in C++, a break statement is always used at the end of each case which indicates the end of the statement. When we compile and execute it, the output is shown on the console screen that is attached in the following:
Now, the getch() function will work here. When the user enters the choice, the getch() function will compare this input with the switch case and will show the output or option on the console automatically and immediately.
In the previously attached screenshot, the user enters the choice “2” and the output appears on the screen as “you select ‘Burger’”. The working of getch() is cleared in this example as the user enters the input and the expected output is displayed on the console screen instead of the user-given input that is shown on the console.
Scenario 2: Accept the Hidden Password from the User Using Getch()
In this scenario, we will go through the concept on how the user can input the password secretly and show the input password on the console screen. Here, we use the getch() function to hold the screen until the user passes the input. Remember that getch() does not display the user input as the cursor does not display the input on the console screen. The code snippet of this scenario is mentioned as follows:
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <string.h>
int main()
{
char pasw[8];
int m;
std::cout<<"Enter 8 digits password: ";
for (m = 0; m <=7; m++) {
pasw[m] = getch();
std::cout<<"*";
}
pasw[m] = '\0';
std::cout<<"\n";
std::cout<<"Unhide the entered password:";
for (m = 0; pasw[m] != '\0'; m++)
std::cout<<("%c", pasw[m]);
getch();
return 0;
}
As seen in the given code, add the standard libraries that are essential for the getch() function usage and to display the output on the screen. The main function of this program is to contain the different steps through which we input a password from the user and show the password on the screen. We need the array variable whose data type is “Char” having a size of 8 elements. Except for this, we also need a variable that stores the iteration values.
Here, we use a “For Loop” that reads every input count of the user if the array size is “8”. Then, the user only presses the key password on the input console window with the help of the loop. Inside this “for” loop, we pass the getch() function to the passw[] array. This function holds on the screen until the user inputs the 8 characters of the password. On every loop count, print “*” on every user input count, and the loop is closed here.
After that, we use the “for” loop again that shows the hidden password itself by just taking the values from passw[]. Getch holds the screen automatically until the user input is stored perfectly in the array and displays the password on the screen.
Click on the “Compile and Run” option to execute the program and show the output on the screen.
Enter the 8 digit password on the screen. When the 8 digit input is done, it automatically displays the typed password on the console window. Press the keyboard keys 8 times and type the password.
The output screen freezes just because of the getch() function. The getch() function just prints the input data on the screen as shown in the previously attached screenshot.
Conclusion
To conclude, getch() is the most important function of C and C++ programming that facilitates the user to freeze the input from the keyboard without displaying or without the cursor showing an input on the screen. Due to its non-standard library, if we do not use getch() in the program, the console window is displayed until the required time for program execution is in milliseconds. In this article, we explained this method with proper code examples.