Basic Input/Output:

 

Standard Output

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:

  • cout<<
  • puts()

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.

Cout<< Function:

This function is used to display output on the monitor. It can display text, constants or values of variables.

SYNTAX

The syntax of cout is as follows:
cout<<”Format string”;

cout<<variable_name;

It can also be used as:

Cout<<”Format string”<<variable_name;

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.

EXAMPLE

Following example is a program that displays a message and values of integer and character variables.

#include<iostream>

Using namespace std;cout-function

void main()

{

int n=10;

char ch=’*’;

cout<<”testing output”;

cout<<n;

cout<<endl;

cout<<”testing output”<<ch;

}

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.

 

Escape sequences

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:

Escape sequence Purpose
\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.

 

Standard Input

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:

  • cin>>
  • gets()
  • getch()
  • getche()

Cin>> Function

This function is used to get input from the user. The input is stored in a variable in a specified form.

SYNTAX
the syntax of cin is as follows:

cin>>variable_name;

variable name is the name of that variable whose value will be entered by the user.

EXAMPLE

Program to convert distance from kilometers into meters.

# include<iostream>

Using namespace std;

void main()cin-function

{

float k;

double m;

cout<<”enter distance in kilometers”;

cin>>k;

m=k*1000;

cout<<k<<“\nkilometers = “<<m;<< “meters = “;

}

getch() function:

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.

SYNTAX

The syntax of getch() function is as follows:

getch();

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.

EXAMPLE

# include<iostream>

using namespace std;getch-function

#include<conio.h>

void main()

{

char c;

cout<<”enter character:”;

c=getch();

cout<<”you entered:”<<c;

getch();

}

getche() function:

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.

SYNTAX

The syntax of getche() function is as follows:

getche();

gets() function

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.

SYNTAX

The syntax of gets() function is as follows:

gets(variable);

variable indicates the string variable in which the string is stored.

EXAMPLE

The following statement inputs a string from user and stores it in a string variable str.

cout<<”Enter a string”;

gets(str);

Suppose the user types “Pakistan” on the screen and then presses ENTER key. The string will be stored in str as follows:

P a k i s t a n \0

Puts() function

The puts() function is used to display string on the screen.it can display a string constant or string variable.

SYNTAX

The syntax of puts() function is as follows:

puts(parameter);

Parameter indicates the string value in which the string is stored. In case of string constant, it is written in double quotes.

EXAMPLE

The following example displays the contents of a string variable str on screen:

Puts(str);

The following example displays a string constant on the screen:

Puts(“programming makes life interesting”);

PROGRAM

# include<iostream>

using namespace std;puts-function

#include<conio.h>

void main()

{

char book[50];

cout<<”enter name of your favorite book:”;

gets(book);

cout<<”your favorite book is:”;

puts(book);

getch();
}

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.

SYNTAX

The syntax of clrscr is as follows:

clrscr();