Input/Output in C++ using cin and cout
In the C++ programming language, the input and output statement keywords are built-in and controlled within the iostream library (header file/preprocessor directive) in every C++ program. The input and output statements in any C++ program are the two most commonly used stream statement methods for displaying textual value information.

The most commonly used methods in C++ programming are cin and cout.
- cin statement – The cin statement is used to input values directly from the user programmer in a C++ program.
- cout statement – The cout statement is used to represent or display the generated output value of user input value information on the console screen in a C++ program.
The objects cin and cout are built-in parts of the standard input/output stream system in every C++ program and are pre-defined in the std namespace library for every C++ program.
cin (Character Input Stream) concept in C++.
The cin statement stream is used in C++ programs to allow direct input of any type of value from the programmer via a standard input device (typically the user keyboard). In cin, user input value data is accepted and processed using the extraction operator >>.
Basic syntax of the cin statement stream.
cin >> parameter;
Here, the cin statement stream parameter in a program is a user-defined variable that will hold the user input value in the current program.
Example of a cin statement stream.
#include <iostream>
using namespace std;
int main() {
char emp_name[100];
char address[100];
int emp_age;
cout << “Enter employee name – “;
cin.getline(emp_name, 100);
cout << “Enter employee address – “;
cin.getline(address, 100);
cout << “Enter employee age – “;
cin >> emp_age;
cout << “\n Employee Details” << endl;
cout << “Employee Name is – ” << emp_name << endl;
cout << “Employee address is – ” << address << endl;
cout << “Employee Age is – ” << emp_age << endl;
return 0;
}
cout (character output stream) concept in C++.
The cout statement stream is used in C++ programs to display or represent the output result value of programmer user input values through the standard output device (typically the console screen). Program result output is processed and managed using the insertion operator <<.
Basic syntax of cout (character output stream).
cout << user-defined expression;
A user expression here can be any program output display value, such as a user-defined variable, a literal, or a formatted string.
Example of a cout statement stream.
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char emp_name[] = “Bhavishi Deora”;
char address[] = “India”;
int age = 21;
cout << “Employee Name – ” << emp_name << endl;
cout << “Employee Address – ” << address << endl;
cout << “Employee Age – ” << age << endl;
return 0;
}
cin and cout group methods.
In a C++ program, the programmer can use the cin stream and cout output stream statements in combination to allow direct user input interaction, or to accept user input values and display the output result of the input stream value within the same program.
Example of the cin and cout group method.
#include <iostream>
#include <string>
using namespace std;
int main() {
string stu_name;
string inst_name;
string course_name;
int c_price;
// here it takes user input with the cin stream
cout << “Enter student name – “;
getline(cin, stu_name);
cout << “Enter institute name – “;
getline(cin, inst_name);
cout << “Enter course name – “;
getline(cin, course_name);
cout << “Enter course price – “;
cin >> c_price;
// here it Displaying output with cout stream statement
cout << “\n Student Details” << endl;
cout << “Student Name – ” << stu_name << endl;
cout << “institute Name – ” << inst_name << endl;
cout << “Course Name – ” << course_name << endl;
cout << “Course Price – ” << c_price << endl;
return 0;
}
Multiple inputs in a cin stream statement.
In a C++ program, you can accept multiple input values in the same program statement using the cin stream. Each user input value is displayed as a user-defined variable separated by the extraction operator >>.
Example of multiple inputs in a cin stream.
#include <iostream>
using namespace std;
int main() {
int p, q, r;
cout << “Enter three different decimal values - “;
cin >> p >> q >> r;
cout << “Your entered decimal values are – “
<< p << ” and ” << q << ” and ” << r << endl;
return 0;
}
cin / whitespace handling concept with string data type.
In a C++ program, when the user can use the cin stream statement to read a text string containing spaces, it only reads up to the first whitespace (space, tab, or newline) in the current program. If you need to read a text string containing a complete line containing spaces in a program, you can apply the getline() built-in C++ function method.
Example of cin / whitespace handling.
#include <iostream>
#include <string>
using namespace std;
int main() {
string empfullName;
string empfullId;
string empfullAddress;
long long contact;
cout << “Enter your full name with a space – “;
getline(cin, empfullName);
cout << “Enter Employee ID – “;
getline(cin, empfullId);
cout << “Enter Address – “;
getline(cin, empfullAddress);
cout << “Enter Contact Number – “;
cin >> contact;
cout << “\n Employee Details” << endl;
cout << “Employee Name – ” << empfullName << endl;
cout << “Employee ID – ” << empfullId << endl;
cout << “Employee Address – ” << empfullAddress << endl;
cout << “Employee Contact – ” << contact << endl;
return 0;
}
This program uses the getline(cin, variable) function method to read a complete line from the input, which also includes custom user-added spaces.
Manually format program output with the cout stream statement.
C++ users can apply multiple individual manipulators to an existing program to read program statements and format its output.
Common C++ formatting manipulator methods.
endl – Inserts a new blank, empty line into the current C++ program and flushes the program output buffer to memory.
cout << “Vanhelpsu#” << endl;
endl Example.
#include <iostream>
using namespace std;
int main() {
cout << “Vanhelpsu#” << endl;
return 0;
}
setw(n) – Sets the width of an output in the current C++ program to n characters and displays it. If the C++ program output is smaller than n, it appends a space to the output.
cout << setw(7) << 3 << endl; // Prints 3 at a width of 7 characters
setw(n) Example.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << setw(7) << 3 << endl; // Prints 3 at a width of 7 characters
return 0;
}
setprecision(n) – This sets the value of floating-point numbers to one decimal place for displaying in the current C++ program.
cout << fixed << setprecision(4) << 9.34389 << endl; // Outputs ‘9.3438’
setprecision(n) Example.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << fixed << setprecision(4) << 9.34389 << endl;
return 0;
}
Fixed and Scientific – Controls and manages the formatting of floating-point numbers (fixed-point or scientific value notation) in the current C++ program.
cout << fixed << setprecision(4) << 34.91398 << endl;
cout << scientific << setprecision(4) << 987123.1938 << endl;
Fixed and Scientific Example.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << fixed << setprecision(4) << 34.91398 << endl;
cout << scientific << setprecision(4) << 987123.1938 << endl;
return 0;
}
left, right, internal – This controls text alignment within a given width in the current C++ program.
cout << left << setw(7) << “Left” << endl; // Left-align
cout << right << setw(9) << “Right” << endl; // Right-align
left, right, internal Example.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << left << setw(7) << “Left” << endl; // Left-align
cout << right << setw(9) << “Right” << endl; // Right-align
return 0;
}
Example of a common C++ formatting manipulator method.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << “Vanhelpsu!” <<endl;
cout << setw(7) << 3 << endl;
//here it prints 3 with 7 characters
cout << fixed << setprecision(4) << 9.34389 << endl;
// result is – 9.3439 (rounded)
cout << fixed << setprecision(4) << 34.91398 << endl;
cout << scientific << setprecision(4) << 987123.1938 << endl;
cout << left << setw(7) << “Left” << endl; // left align
cout << right << setw(9) << “Right” << endl; // right align
return 0;
}
