The information or the processed data given by the computer is known as output. The output that is displayed on the monitor is the hardcopy and the printed form of output is a softcopy. In C++ programming language we use various outputs.
Some important output functions in C++ are as follows:
The functions used for input and output are stored in the header file <iostream>. Header file is necessary to include in the program it input/output functions are used.
This function is used to display output on the monitor. It can display text, constants or values of variables.
The syntax of cout is as follows:
It can also be used as:
Format string is given in double quotes as shown. It is also called control string. Format string may include text that is the message to be printed, escape sequences that are used to specify the format of output e.g. \n, \t, etc.
Variable name is the variable whose value is going to be printed.
Following example is a program that displays a message and values of integer and character variables.
Using namespace std;
Working of the above program:
The above program declares and initializes two variables n and ch. It displays message “testing output” on the screen. it then displays the values of n and ch.
These are the special characters used in format string to modify the format of output. These characters are not displayed in the output. These characters always begin with backslash “\”. The backslash is known as escape character.
Some of the escape sequences are as follows:
|\n||It is used to insert a new line|
|\b||It is used to insert a backspace|
|\t||It is used to insert a tab in output|
|\’||This is used to display single quotes|
|\”||It is used to display double quotes|
The escape sequences are written in double quotes or format string in cout statement.
The process of giving something to the computer is known as input. The input is mostly given by keyboard. The term standard input refers to the input using keyboard. A program may need certain input from the user for working properly. C++ programming language provides many functions to get input from the user.
Some important input functions are as follows:
This function is used to get input from the user. The input is stored in a variable in a specified form.
the syntax of cin is as follows:
variable name is the name of that variable whose value will be entered by the user.
Program to convert distance from kilometers into meters.
Using namespace std;
cout<<”enter distance in kilometers”;
cout<<k<<“\nkilometers = “<<m;<< “meters = “;
The word getch() stands for get character. The function is used to input single character from the user. When this function is executed, it waits for any key to be pressed. The character entered by the user is not displayed on the screen.
The function getch() is defined in the header file conio.h. this header file must be included in the program to use this function.
The syntax of getch() function is as follows:
Another way to use this function is as follows:
variable = getch();
variable indicates the variable in which the character is stored. The use of variable is optional.
This function is normally used in the program to stop execution of program temporarily. For example, the user can use it at the end of program so that the control remains on DOS screen until the user presses any key.
using namespace std;
The letter e in getche stands for echo. The getche() function is used to input single character from the user. When this function is executed, it waits for any key to be pressesd. The function echoes(displays) the character on the screen entered by the user.
This function is defined in the header file conio.h.
The syntax of getche() function is as follows:
This function is used to enter string value from the user. The input is stored in a string variable. After typing the string from keyboard, the user presses ENTER key and the string is stored in the variable. The null character \0 is automatically entered at the end of string.
The syntax of gets() function is as follows:
variable indicates the string variable in which the string is stored.
The following statement inputs a string from user and stores it in a string variable str.
cout<<”Enter a string”;
Suppose the user types “Pakistan” on the screen and then presses ENTER key. The string will be stored in str as follows:
The puts() function is used to display string on the screen.it can display a string constant or string variable.
The syntax of puts() function is as follows:
Parameter indicates the string value in which the string is stored. In case of string constant, it is written in double quotes.
The following example displays the contents of a string variable str on screen:
The following example displays a string constant on the screen:
Puts(“programming makes life interesting”);
using namespace std;
cout<<”enter name of your favorite book:”;
cout<<”your favorite book is:”;
The clear screen function (clrscr):
The clrscr function is used to clear the screen. it is an abbreviation of clear screen. When this function is executed, the screen is cleared and the cursor blinks on the top left corner. This function is available in the header file conio.h.
The syntax of clrscr is as follows: