Function 'getch( )' is misused in programs
Generally when we pause our program's output screen by using the function 'getch( )' .
But the actual use of the function 'getch()' is to catch a single character from the keyboard dynamically.
So,when we use the 'getch( )' function in the end of the program,it actually dosen't pause the program or screen while execution, but it waits for the entry of any type of character for further execution of the program and then it gets terminated.
For example :- #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"Hello Everyone /n";
getch(); //misused 'getch()' function
}
So, here after printing hello on the screen,the program waits for the entry of the charcater through the getch() function and when we press any key, the job is completed and the program finds no more
statements and hence terminates.
Hence, the program was actually waiting for the response but not pausing the execution.
-------------------------------------------------------------------------------------------------------------
*Use 'System( )' function for pausing the execution*
-------------------------------------------------------------------------------------------------------------
So,for actual pausing of the execution screen, we should use the 'system()' function.
for example :
#include<stdlib.h>
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
cout<<"Hello Everyone /n";
system("pause"); // use of 'system()' function
}
Output will be as :
Hello Everyone
press any key to continue
Output will be as :
Hello Everyone
press any key to continue
--------------------------------------------------------------------------------------------------------

No comments:
Post a Comment