Input Output Operator In C++ | how to take user input in C++


Input Output operator in C++



Input/Output operator:-

                                      C++ uses a convenient abstraction called streams to perform input and output operations in sequential media such as the screen, the keyboard or a file. A stream is an entity where a program can either insert or extract characters to/from. There is no need to know details about the media associated to the stream or any of its internal specifications.


Input Stream:

                     If the direction of flow of bytes is from device(for example: Keyboard) to the main memory then this process is called input.


Output Stream:

                         If the direction of flow of bytes is opposite, i.e. from main memory to device( display screen ) then this process is called output.


Header files available in C++ for Input – Output operation are:


§  Iostream: iostream stands for standard input output stream. This header file contains definitions to objects like cin, cout, cerr etc.


§  Iomanip: iomanip stands for input output manipulators. The methods declared in this files are used for manipulating streams. This file contains definitions of setw, setprecision etc.


§  fstream: This header file mainly describes the file srteam. This header file is used to handle the data being read from a file as input or data being written into the file as output.


The standard library defines a handful of stream objects that can be used to access what are considered the standard sources and destinations of characters by the environment where the program runs:





stream
description
Cin
standard input stream
cout
standard output stream
cerr
standard error (output) stream
clog
standard logging (output) stream



Standard Output stream:-

                                                           The identifier cout is a predefined object that represents the standard output stream in C++. cout is the instance of the ostream class. cout is used to produce output on the standard output device which is usually the display screen. 
The operator << is called the insertion or put to operator. It inserts (or sends) the contents of the variable on its right to the object on its left.





Example of input operator:-


#include <iostream>
#include <conio.h>

 
void main( )
 {
   char example[] = "IT tech";
 
   cout << example << " -IT stand for information technology";
   
   getch();
}

Output:-
IT tech-IT stand for information technology

Standard input stream:-

                                                      The identifier cin is a predefined object in c++ that corresponds to the standard input stream and cin is the instance of the class istream and is used to read input from the standard input device which is usually keyboard.
The operator >> is known as extraction or get from operator. It extracts (or takes) the value from keyboard and assigns it to the variable on its right. This corresponds to the familiar scanf() operator.



 Example of input operator:-


#include<iostream>
#include<conio.h>

void main()
{
    int a;
    
    cout << "Enter the value of a:";
    cin >> a;
    cout <<”\n The value of a is :"<<a;
     getch();
   
}

Input:-
10

Output:-
Enter the value of a:
The value of a is: 10

   

§  The above program asks the user to input the value of a. The object cin is connected to the input device. The value of a entered by the user is extracted from cin using the extraction operator(>>) and the extracted data is then stored in the variable a present on the right side of the extraction operator.



Un-buffered standard error stream (cerr):-

                                                                      Cerr is the standard error stream which is used to output the errors. This is also an instance of the ostream class. As cerr is un-buffered so it is used when we need to display the error message immediately. It does not have any buffer to store the error message and display later.


#include <iostream>
 
using namespace std;
 
int main( )
{
   cerr << "An error occured";

   return 0;
}
                                   
Output:
An error occurred


buffered standard error stream (clog): -

                                                                          This is also an instance of ostream class and used to display errors but unlike cerr the error is first inserted into a buffer and is stored in the buffer until it is not fully filled. The error message will be displayed on the screen too.


#include <iostream>
 
using namespace std;
 
int main( )
{
   clog << "An error occured";

   return 0;
}

Output:-

An error occurred





To learn more you can just click the below topics:

C++

INTRODUCTION OF C++ || Definition of C++
Brief history of C++ || history of C++
Features of C++ || why we use C++ || concept of C++
Concept of OOP || What is OOP || Object oriented programming language
Difference Between OOP And POP || Different Between C and C++
Characteristics of C++
Interesting fact about C++ || Top 10 interesting fact about C++
C++ Program Structure
Writing first program in C++ || how to write hello world in C++
Basic Data Type And Variable In C++
Identifier in C++
Keywords in C++
Token in C++
Comment in C++
Constant in C++
Modifier in C++
Taking User Input in C++ | User input in C++
C++ Operators | Operator in programming language
How to Add two number in C++
Polymorphism in C++
Compile Time Polymorphism in C++
Function overloading in C++
Operator Overloading in C++
What are Control Structures in C++ || Understanding Control Structures in C++ | How to use if, else, switch
What are Functions and Recursion in C++ | How to Defining and Calling Functions

Class in C++
Object in C++

Data Science

The History and Evolution of Data Science
Scope of Data Science
Why learn Data Science? | Why Data Science?
Impact of Data Science
The Importance of Data in Science | Introduction to Data Science
What is Data Analysis | Data Analyst for Beginners

Algorithm

Why algorithm | The Importance of Algorithms in Modern Technology

Tech to know

Which is better | BSc in Computer Science or BTech?

Post a Comment

Ask any query by comments

Previous Post Next Post