Input/Output stream in C++.

The C++ programming language does not define any statement to perform input and output operations. Instead, C++ has a separate input/output library that is part of the C++ standard library that provides primary input/output and many other facilities.


For the purpose of input and output, you will use the iostream (input-output stream) library which is a part of the C++ standard library. You need to include iostream header file on top of any C++ code file to use the properties define in this library. 

#include<iostream> //header file

int main()
{
   //include the rest of your code here
}


Note: A stream is a sequence of characters read from or written to an Input/Output device. 

  • When the direction of the flow of characters is from the device to the main memory then this process is called input.
  • When the direction of the flow of characters is from the main memory to the device then this process is called output


There are basically four standard objects present inside the iostream library and these are:

Objects Uses
std::cin (pronounced see-in) Standard input
std::cout (pronounced see-out) Standard output
std::cerr (pronounced see-err) Standard error
std::clog (pronounced see-log) General information

std::cout
cout is an object of the standard ostream (output stream) class that is defined in the iostream library. It is used to print the output on the display screen (console). The insertion operator (<<) to insert in the output stream to display the text to the console.
#include<iostream> //header file

int main()
{
    std::cout<<"Hello AlgoLesson"; //printing Hello AlgoLesson on console
    std::cout<<4;  //printing 4
return 0; }
Output:
Hello AlgoLesson4

std::cin
cin is an object of the standard istream (input stream) class that is defined in the iostream library. It is used to take input from the user using an input device like a keyboard. The extraction operator (>>) extracts the data from the cin object which is entered by the user. 
#include<iostream> //header file

int main()
{
    int x; //variable to hold input

    std::cout<<"Enter the value of x: ";
    std::cin>> x; //getting input from keyboard and store in x

    std::cout<<"Value of x: " << x ;
    return 0;
}
Output:
Enter the value of x: 7
Value of x: 7

std::cerr
cerr is a standard error stream that is part of the iostream class and is used to print the error messages. This is used when a person wants to show an error message immediately. 
#include<iostream> //header file

int main()
{
    std::cerr <<"This is an error message";
    return 0; 
}
Output:
This is an error message

std::clog
clog is a standard buffered error stream that is used to store general information or error messages. It is also part of the iostream library and is used to display error messages on the display screen.
#include<iostream> //header file

int main()
{
    std::clog <<"This is an error message log";
    return 0; 
}
Output:
This is an error message log

std::endl
endl stands for "end line" which is used to tell the console to move the cursor to the next line so we can print the output in a new separate line. You can also use '\n' which is a newline character instead of using endl.
#include<iostream> //header file

int main()
{
    std::cout <<"Welcome to AlgoLesson" << std::endl;
    std::cout <<"You are learning C++"; 
    return 0; 
}
Output:
Welcome to AlgoLesson
You are learning C++

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS