C++ User input output

In order to get input from users, C++ user input commands are used in the C++ programming language. There are different libraries in C++ that provide keywords to achieve user interaction. The libraries manage the flows of data and keep records of the sequence of bytes. These sequences are called streams. There are two types of streams.

  • Input Stream: is the flow of bytes from the user to the machine. Bytes travel from the keyboard to the main memory in the input stream.  
  • Output Stream: is the flow of bytes from the main memory to the computer’s output devices. Bytes travel from the main memory to the computer screen in the output stream.  

The libraries are present in the form of header files. These header files can be used in any C++ program. The syntax for including libraries is

#include <library>

The libraries used for input/output operations are:

  • iostream
  • fstream
  • iomanip

iostream stands for standard input-ouput stream. istream is responsible for user input functionality. While ostream is responsible for user output functionality. 

C++ User Input

The keyword used for taking standard user input is cin, which is defined in the header file iostream. cin is pronounced as “see-in”. To use cin in the C++ program, user needs to include the iostream library. The keyword cin takes input from the user and it is then stored in a variable for further manipulations. cin is followed by extraction operator >>, which is followed by a variable. The syntax is as follows:

cin >> x;

Where ‘x’ is a variable. The input can be integer, characters, or strings. 

C++ User Output

The keyword used for displaying standard user output is cout, which is also defined in the header file iostream. cout is pronounced as “see-out”. To use cout in the C++ program, user needs to include the iostream library. The purpose of the keyword cout is to print constants or variables on the console. cout is followed by the insertion operator << and that is further followed by a string in double-quotes or a variable. The syntax is s follows:

cout << "String";
cout << a;

Where ‘a’ is a variable. The output can be of the type integer or character. 

The below example shows the use of cin and cout keywords. It also shows how to get multiple outputs using one cout and more than one insertion operator <<.

#include <iostream> 
using namespace std; 
int main() {
char name[100];
char var[] = "Hello ";
cout << "Please enter your first name" << endl;
cin >> name;
cout << var << name << ", Welcome to TutorialsArt!";
return 0;
}

The output of the above code after compilation is: 

Please enter your first name
David
Hello David, Welcome to TutorialsArt!

Here the code asks the user to input his first name. In the above case, the user has entered Ahmed from the keyboard as input. This value is then saved in the character type variable name. The name is displayed to the user afterward in the form of greetings.

Other than cout the standard library also provides two other output keywords:

  • cerr: is standard output error stream. It is un-buffered object of iosteam class. In un-buffered stream, the variable is directly saved in the main memory. The purpose of cerr is same as cout but according to good programming practice, cerr is used to log errors. The syntax of using cerr is.
cerr << variable;
cerr << "String";
  • clog: is standard output logging stream. It is buffered object of iostream class. That means streams in clog are buffered. In the buffered stream, the output is first saved in the variable and then written to the main memory. The syntax for using clog is.
clog << variable;
clog << "String";

The code below shows how the keywords cerr and clog work.

#include <iostream> 
using namespace std;
int main() 
{
cerr << "error occured while execution" << endl; 
clog << "error logged in file" << endl;
return 0; 
}

The output of the above code, after compilation is.

error occured while execution
error logged in file

String Input

In C++ whitespace, tabs, enter key, new line act as terminators. When any one of these detected, the user input is terminated. The characters before the whitespace are extracted as input and the ones after whitespace are discarded. In order to enter an entire sentence as an input, getline function is used. The getline function takes two arguments. The first argument is the keyword cin and the second argument is string variable. The syntax of getline function is:

string sentence;
getline(cin, sentence);

The code below shows the use of getline function to get sentences from user as string input.

#include <iostream>
#include <string>
using namespace std;
int main ()
{
  string fname;
  cout << "Please enter your full name " << endl;
  getline (cin, fname);
  cout << "Hello " << fname << ", Welcome to TutorialsArt!" << endl;
  return 0;
}

The output of the above code after compilation is.

Please enter your full name 
David Adam
Hello David Adam, Welcome to TutorialsArt!

 

Comments are closed.