C++ Input/Output Operator | cout and cin | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Saturday, July 18, 2020

C++ Input/Output Operator | cout and cin | My CS Tutorial

C++ Input/Output | cout and cin | cascading of input and output operators | I/O operator in c++

C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen or the keyboard. A stream is an object where a program can either insert or extract characters to/from it.

The standard C++ library includes the header file iostream, where the standard input and output stream objects are declared.

C++ i/o operations | My CS Tutorial

STANDARD OUTPUT(cout)


By default, the standard output of a program is the screen, and the C++ stream object defined to access it is cout.

cout is used in conjunction with the insertion operator, which is written as << (two "less than" signs).

Example:-

cout << "Output sentence";
// prints Output sentence on screen

cout << 120;
 // prints number 120 on screen

cout << x;
// prints the content of x on screen

The insertion operator (<<) may be used more than once in a single statement:-

cout << "Hello, " << "I am " << "a C++ statement";

This last statement would print the message Hello, I am a C++ statement on the screen. The utility of repeating the insertion operator (<<) is demonstrated when we want to print out a combination of variables and constants or more than one variable:-

cout << "Hello, I am " << age << " years old and my zipcode is " << zipcode;


If we assume the age variable to contain the value 24 and the zipcode variable to contain 90064 the output of the previous statement would be:-
output:-

Hello, I am 24 years old and my zipcode is 90064

STANDARD INPUT(cin)

The standard input device is usually the keyboard. Handling the standard input in C++ is done by applying the overloaded operator of extraction (>>) on the cin stream. The operator must be followed by the variable that will store the data that is going to be extracted from the stream. For example:-

int age;

cin >> age;


The first statement declares a variable of type int called age, and the second one waits for an input from cin (the keyboard) in order to store it in this integer variable.

Example:-


// i/o example

#include <iostream>

using namespace std;

int main ()

{

 int i;

 cout << "Please enter an integer value: ";

 cin >> i;

 cout << "The value you entered is " << i;

 cout << " and its double is " << i*2 << ".\n";

 return 0;

}


Please enter an integer value: 500

The value you entered is 500 and its double is 1000.

You can also use cin to request more than one datum input from the user:-

cin >> a >> b;

is equivalent to:-

cin >> a;

cin >> b;


In both cases the user must give two data, one for variable a and another one for variable b that may be separated by any valid blank separator: a space, a tab character or a newline.

CASCADING OF INPUT/OUTPUT OPERATOR:-

using the cascating techniques,two statement can be combined as follows:-

cout<<"Sum="<<sum<<"\n"
      <<"Average="<<avg<<"\n";
   
 This is one statement by provides two lines of output like:-

 Sum = 5
 Average = 2.5

 If you want only one line of output as given below:-

 Sum = 5  Average = 2.5

 cout<<"Sum="<<sum<<",""
       <<"Average="<<avg<<"\n";
     

C++ Input/Output | cout and cin | cascading of input and output operators | I/O operator in c++

_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment