Clrscr() and Getch() in C++


Prev Tutorial Next Tutorial

Clrscr() and Getch() in C++

clrscr() and getch() both are predefined function in "conio.h" (console input output header file).

Clrscr()

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only.

Example of clrscr()

#include<iostream.h>
#include<conio.h>

void main()
{
int a=10, b=20;
int sum=0;
clrscr();  // use clrscr() after variable declaration
sum=a+b;
cout<<"Sum: "<<s;
getch();
}

Output

Sum: 30

Getch()

It is a predefined function in "conio.h" (console input output header file) will tell to the console wait for some time until a key is hit given after running of program.

By using this function we can read a character directly from the keyboard. Generally getch() are placing at end of the program after printing the output on screen.

Example of getch()

#include<iostream.h>
#include<conio.h>

void main()
{
int a=10, b=20;
int sum=0;
clrscr();
sum=a+b;
cout<<"Sum: "<<s;
getch();  // use getch() befor end of main()
}

Output

Sum: 30
images
Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements