Standard input and output streams in C++
In this part we will discuss about two standard streams in C++ which is used in almost all the programs, and also about new line character with the help of a simple program.
Streams are as follows :
- standard input stream - cin
- standard output stream - cout
Now lets understand cin :
- Its a keyword, so it cannot be used as identifier.
- The cin operator with extraction operator (>>) is used to read the input from user and unlike cout don't use double qoutes (" ") but the line should always terminate with semicolon (;) as each line in C++ must terminate with semicolon for successful operation .
- The extraction operator (>>), extracts the value from the stream named cin and assign it to next named variable.
Now lets discuss about a new concept :
New line character - * represent by endl or ”\n” or ’\n’.
* always used with standard output stream (cout).
* when written, the newline character causes the successive output to be directed to the next line, otherwise each successive cout display the output at the next column after the previous output.
* example :
1. cout <<”the sum of 5+2 = ” << ’\n’ ;
cout << 5+2 ;
Output for this code will be ->
the sum of 5+2 =
the sum of 5+2 =
7
2. cout <<”the sum of 5+2 = ” ;
cout << 5+2 ;
Output for this code will be ->
the sum of 5+2 =7
the sum of 5+2 =7
PROGRAM - To find the product and sum of two numbers entered by the user
OUTPUT :
Now take a look on the above program :
* line 1,2,3,4,12 i have discussed in my first program, go and check if you don't know.
Now lets understand the codes under our main function :
* in line 5 i have declared two integer variables which holds integer value.
* in line 6 using cout i am asking two numbers from the user for the operation.
* in line 7 the user will enter two numbers (remember to press space bar or enter key to separate numbers while entering other wise two numbers will be treated as a one number, ex - 56 is one number without space between them and 5(space/enter)6 are two numbers) and the cin operator will assign that value to a and b variable.
* in line 8 and line 10 using cout we multiply and add two variables whose result will be displayed on the output screen.
* in line 9 we use new line character (discussed above) so that the next line output (line 10) will print on the next line, as you can see in the output screen. Most of you may be thinking that after line 7 we didn't use any new line character then how the line 8 output prints on the next line, so this is because after encounter cin operator our cursor automatically move to next line as we can see in the output screen.
* and line 11, 12 you know if don't then go to my first program.
Hope you understood all this concepts, if not then build up your basics and using them make your own simple programs or you can remake this above program in which you deal with three numbers.
In next part, i will be dealing with some other interesting programs, so
Be connected.
Comments
Post a Comment