C++ - SPLessons

C++ Input/Output

Chapter 7

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Input/Output

C++ Input/Output

shape Description

Any language uses input/output data from/to the user which shows its success rate. CPP Input/Output streams are also present to receive the data and send the data from a program. This action requires keyboard to provide an input and monitor screen to provide the output. A stream can be defined as an entity through which a program can send and retrieve the data.A stream can be divided into various types shown as below.

All the streams that perform input and output operations are included in the header files(<iostream>, <iomanip> (or) <fstream>) and declare them in the program to use it . There are four CPP Input/Output streams.

CPP Input/Output Stream Description
cin Input Stream
cout Output Stream
cerr Output Stream
clog Output Stream

shape Conceptual
Figure

cin 

shape Description

The stream ‘cin’ is an input stream and is an instance of istream class. cin takes the input from keyboard by using stream extraction operator >>. This stream waits till the input is entered. More than one input in a single statement can also be given.

shape Examples

#include <iostream>
#include <string>
using namespace std;

int main ()
{
  int i,j;  
  cout << "Please enter an two integer values: "; cin >> i>> j; //inputting two values in single statement
  
  cout << "The values you entered are " << i <<" "<< j <<endl ;  
} 	

Output

Please enter an two integer values: 59
86
The values you entered are 59 86

cout

shape Description

The stream ‘cout’ is an output stream and is an instance of ostream class. cout gives the output to display screen by using stream insertion operator <<. More than one output in a single statement can also be given.

To see any output on the screen, there are two ways.

  1. Enter the desired statement in double quotes" ".
  2. Assign the statement to a variable and enter the variable without any quotes

The variables or the statements which are to be displayed must be after insertion operator.( << ). By default, cout does not end the line with any special characters. “\n” or endl can also be used to go to next line.

shape Example

#include <iostream>
#include <string>
using namespace std;

int main ()
{
 string str;
  cout << "What is your name? "; cin >> str;
  getline (cin, str);//taking string input using getline
  
  cout << "Hello " << str << endl;
  cout << "What is your favorite tutorial"<< endl;;
  getline (cin, str);
  cout << "We like " << str << " too!"<< endl;
  return 0;  
} 	

Output

What is your name? Raja
Hello
What is your favorite tutorial
splessons
We like splessons too!

cerr and clog

shape Description

Both “cerr and clog” are similar to cout belonging to ostream class. cerr connected to error device, gives the output immediately as the data in it is not buffered. clog is also connected to error device, but gives the output with a gap as the data is stored in buffer. It loads the output only when the previous output is erased and the buffer becomes empty.

shape Example

#include <iostream>
#include<string>
#include<windows.h>
#include<cstdlib>
#include<unistd.h>
using namespace std;
 
int main( )
{
  string str;
  cout << "enter a statement"<<endl; cin >> str;
  cerr << "Error message : " << str << endl;
 
  cout << "enter a statement"<<endl; cin >> str;
  sleep(1);
  clog << "Error message : " << str << endl;
}

Output

enter a statement
splessons
Error message : splessons
enter a statement
splessons
Error message : splessons

During the display of the output using “cerr and clog” you cannot find any time difference.

stringstream

shape Description

Stringstream is an another stream included in C++ to perform input and output operations. The main functionality is to convert the numerical values to strings and strings to numerical values.

shape Example

// stringstreams
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
  string str;
  float s1=0;
  int s2=0;

  cout << "Enter first value: "; getline (cin,str); stringstream(str) >> s1;
  cout << "Enter second value: "; getline (cin,str); stringstream(str) >> s2;
  cout << "Total price: " << s1*s2 << endl;
  return 0;
}

Output

Enter first value: 10
Enter second value: 5
Total price: 50

Summary

shape Key Points

CPP Input/Output draws out the following important points.

  • <iostream> , <iomanip>  (or) <fstream> are header files performing input/output operations in C++.
  • cin takes the input using “>>”
  • cout, cerr, clog displays the output using “<<” such that the required output must be enclosed withing quotes(” “).
  • stringstream converts the numerics to strings and vice-versa

shape Programming
Tips

Do not confuse between stream extraction operator(>>) and stream insertion operator(<<).