How to perform I/O operation in c++
What is input ?
- Information given to the program, is called input
- In input data is flow inside the program
- It required a source(for reading )
stadin - - > standard input used as scanf function
stdout ---> standard input used as printf function taken by monitor(gives destination)
What is output :
- Information given by the program is called output
- In output data is flow outside the program
- In c++ I/O operations are alone using streams
- A stream is class is provide set of functions to perform input and output operations
- A stream represents input source (reading) and output destination (writing)
- C++ provides 2 stream classes
- .IIstream The istream consists of input functions to read a streams of characters from the keyboard.
- Ostream The ostream consists of output functions to write a character onto the screen.
stdout is a file pointer which is pointing to console
I/O operation in C++ Example
- It is a predefined object of class Ostream
- Cout represent console output
- Cout represent standard output devide(Monitor)
- For cout object destination is standard output device
- Ostream cout(“ con ”);
- This predefined object is a variable in IOstream.h file
- This object uses << (insertion) operator to perform formatted output operation
- This object uses all the member function of stream class to perform output operation
- << ( insertion operator) is overloaded operation on member function
ex : printf(“%d”,IO)// This operation performs O/P operation without using formatting specifier.
- This operation insert one values
- Insertion means writing / printing Cout << ’10,20 ; // it will print 10
In order to print multiple values cout uses multiple insertion operators.
Cout<value 1<value2<value 3…….
- Using multiple insertion operator with cout is called cascading , it is a method of organizing
- evaluation of insertion operators are are done from right to left and insertion is done from left to right
cout<< “welcome to c++”;
cout<<10; // cout <<10<<end l;
cout<< 1.5; // cout<<1.5<<end l;
cout << ‘A’; // cout<<’A’<<end l;
cout<<1/5; // cout<<1.5<<end l;
cout<<’A’; // cout << ‘A’ << end l;
cout<< “c++” // cout << “c++” <<endl;
endl : It is a manipulator used for inserting new line
- Q) What is the difference between \n and endl ?
In is a back slash is escape sequences
|it is called manipulator||it is called escape sequence|
|It doesn’t occupy any space||It occupy 1 byte|
|It is available in iostream.h||it is available in ‘C’|
# include <iostream.h>
cout<<10<<1.5<<’A’<<”c++”; // cascading
Cin : It is an object of istream class.
- istream class provide set of functions to performs input operations
- Cin uses >> (extraction) operation to read any type of value
- It does not required any formatting specifiers
Formatting is an implicit functionality of extract of operation
- It is an overloaded operation // reading (source) writing Cdestination which extend functionality of >> (shift operator)
Cin >> variable –name;
- One extraction operator read only one value cin>>var1>>var2>>var3----
- Using multiple extraction is called as cascading
Variable (local) :
The variable declared inside function is called local variable.
- Memory for this variable is allocated on execution of function and after completion of function
- The scope of this variable within function
- Local variable are called auto variable.
- Auto is storage class and variable is never declared without class
- Storage class define scope life time and storage of variable.
Cout << X << endl;
o/p : The output of above program is garbage value, Because local variables are not given any default values.
- C++ allows to declare local variable anywhere within function
# include <iostream.h>
cout<< “\n input n1 value”;
cin >> n1;
cout<< “\n input n2 value”;
cout<< “\n sum is ” << n3
C++ allows to declare block level variables