VKS-LEARNING HUB

Home » Class-XI » C++ XI » INPUT & OUTPUT in C++

INPUT & OUTPUT in C++

C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called input operation and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc, this is called output operation.

I/O Library Header Files:

Header files: A header file is a file containing C++ declarations and macro definitions  to be shared between several source files. You request the use of a header file in your program by including it, with the C ++ pre-processing directive `#include’.

There are following header files important to C++ programs:

Header File Function and Description
<iostream> This file defines the cin, cout, cerr and clog objects, which correspond to the standard input stream, the standard output stream, the un-buffered standard error stream and the buffered standard error stream, respectively.
<iomanip> This file declares services useful for performing formatted I/O with so-called parameterized stream manipulators, such as setw andsetprecision.
<fstream> This file declares services for user-controlled file processing. We will discuss about it in detail in File and Stream related chapter.

The standard output stream (cout):

The predefined object cout is an instance of ostream class. The cout object is said to be “connected to” the standard output device, which usually is the display screen. The cout is used in conjunction with the stream insertion operator, which is written as << which are two less than signs as shown in the following example.

Console Output (cout)
Using cout and output operator (<<) value can be displayed on the screen (console). A list of data items can be displayed with single cout, each data separated by output operator (<<).
 Rule:          cout<<Value;
cout<<Value1<<Value1<<Value3…;
cout<<Value<<endl;
 
Usage of cout
cout<<“Vinay Ahuja”;
cout<<11;
cout<<‘A’;
cout<<78.5;
Produces output like
Vinay Ahuja11A78.5
 
Usage of cout
cout<<“Vinay Ahuja”<<11<<‘A'<<78.5;
 
Produces output like
Vinay11A78.5
 
Usage of cout
cout<<“Vinay Ahuja”<<endl;
cout<<11<<endl;
cout<<‘A'<<endl;
cout<<78.5<<endl;
 
   
 
 
Produces output like
Vinay Ahuja
11
A
78.5
 
Usage of cout
char name[20]=”Vinay Ahuja”;
int cla=11;
char sec=’A’;
double marks=78.5;
cout<<“Name   =”<<name<<endl;
cout<<“Class  =”<<cla<<endl;
cout<<“Section=”<<sec<<endl;
cout<<“Marks  =”<<marks<<endl;
 
Produces output like
Name   =Vinay Ahuja
Class  =11
Section=A
Marks  =78.5
Displaying many values by using single cout and separating the values by output operator (<<) is known as cascading of output operator. An example is given below:
cout<<“Vinay Ahuja”<<11<<‘A'<<78.5<<endl;

The standard input stream (cin):

The predefined object cin is an instance of istream class. The cin object is said to be attached to the standard input device, which usually is the keyboard. The cin is used in conjunction with the stream extraction operator, which is written as >> which are two greater than signs as shown in the following example.

Console Input (cin)
Using cin, value can be inputted in a variable when a program is getting executed (running). cin causes a program to stop and wait for user to input value through a keyboard (console). It will then store the value inputted in a variable. A variable is to be created (defined) and then value can be inputted by using cin. List of value can be inputted using cin, separating the variable names by input operator (>>).
 
Rule:          cin>>VariableName;
cin>>VariableName1>>VariableName2>>VariableNname3 …;
 
Usage of cin
char name[20], sec;
int cla;
double marks;
cin>>name;
cin>>cla;
cin>>sec;
cin>>marks;
 
Produces a screen like
Vinay◄─┘
11◄─┘
A◄─┘
78.5◄─┘
After every input Enter key (◄─┘) is pressed. When inputting a string (Vinay) double quotes (“) are not required. When inputting a character (A) single quote quotes (‘) are to be avoided.
 
Usage of cin
char name[20];
int cla;
char sec;
double marks;
cin>>name>>cla>>sec>>marks;
 
Produces a screen like
Vinay□11□A□78.5◄─┘
Every input is separated by Space (□) but final key stroke is Enter (◄─┘).
 
Or,
Produces a screen like
Vinay──►11──►A──►78.5◄─┘
Every input is separated by Tab (──►) but final key stroke is Enter (◄─┘).
 
Inputting many values by using single cin and separating the variable names by input operator (>>) is known as cascading of input operator. An example is given below:
 
char name[20];
int cla;
char sec;
double marks;
cin>>name>>cla>>sec>>marks;
 
To make an input more user friendly, it is better to display a prompt or a message before an input so that the user knows exactly what kind of input is required for the program.
 
Usage of prompt or message with cin
int cla;
char name[20], sec;
double marks;
cout<<“Input Name   ? “; cin>>name;
cout<<“Input Class  ? “; cin>>cla;
cout<<“Input Section? “; cin>>sec;
cout<<“Input Marks  ? “; cin>>marks;
After execution of above program segment produces a screen like this
 
Input Name   ? Vinay
Input Class  ? 11
Input Section? A
Input Marks  ? 78.5
 
Structure of a C++ program
A complete C++ program consists of header files and at least one function (main() function). The most important function in C++ is the main() function. A complete C++ program may contain other functions as well, but they are invoked from the main() function only. An example is given below:
 
#include<iostream.h>
#include<conio.h>
void main()
{
cout<<“This is my first program using C++”;
getch();
}
 
Running of the program will produce output screen like
This is my first program using C++
Note:               Output remains on the screen till you press any key because getch() function waits for a user to strike any key.
The header file iostream.h is required for cout and output operator (<<). To use the function getch() we need the header file conio.h. C++ compiler will obtain necessary information about cout, << and getch() from the header files.
Function:        A C++ function has two main components – header and block (body).
Function Header – void main()
Function block
{
cout<<“This is my first program using C++”;
getch();
}
A block starts with curly bracket ({) and ends with curly bracket (}). Every C++ function contains C++ statements. Every C++ statement is separated by a semi-colon (;).
 
Some important keyboard shortcuts:
a)      To compile a program press ALT+F9 (Click Project from Menu Bar and then click Compile). Compiler will convert a program written in high level language (source code – CPP file) into an intermediate machine language code (Object Code – OBJ file). Compiler also checks for syntax errors. Object code will be successfully generated provided the Source Code does not contain any syntax error(s).
b)      To make (compile and link) a program press F9 (Click Project from Menu Bar and then click Make). A linker will add Run-Time Library to the Object Code to obtain Executable Machine Language Code (Executable Code – EXE file). A computer or the CPU (processor) of the computer can only execute Machine Language Code (EXE file). Run-Time Library is collection sub-routines needed to run a program.
c)      To run (compile, link and execute) a program press CTRL+F9 (Click Debug from Menu Bar and then click Run). Machine Language Executable file is loaded in the computer’s main storage from the computer’s secondary storage and the program is executed. When the program is getting executed, a DOS Window pops up on the Desktop. DOS Window disappears after the execution of the program.
 
#include<iostream.h>
#include<conio.h>
void main()
{
char name[20], sec;
int cla;
double marks;
cout<<“Input Name   ? “; cin>>name;
cout<<“Input Class  ? “; cin>>cla;
cout<<“Input Section? “; cin>>sec;
cout<<“Input Marks  ? “; cin>>marks;
cout<<“Name   =”<<name<<endl;
cout<<“Class  =”<<cla<<endl;
cout<<“Section=”<<sec<<endl;
cout<<“Marks  =”<<marks<<endl;
getch();
}
 
Running of the program produce screen like
Input Name   ? Vinay◄─┘
Input Class  ? 11◄─┘
Input Section? A◄─┘
Input Marks  ? 78.5◄─┘
Name   =Vinay
Class  =11
Section=A
Marks  =78.5
 
Running of the program produce screen like
Input Name   ? Vinay Ahuja◄─┘
Input Class  ? Input Section? Input Marks  ? Name   =Vinay
Class  =1
Section=
Marks  =1.84513e-307
Inputting string with a space creates run-time error. C++ program treats space as separator. Remaining three inputs are ignored. String “Vinay” get stored in the variable name. Garbage values get stored in the variable cla, sec and marks.

Leave a comment