C++ Basic Input/Output
In C++, input and output (I/O) operations are fundamental for interacting with users or handling data. The cin
object is used to take input from the user, and the cout
object is used to display output to the screen. Understanding how to use these two objects effectively is crucial for creating interactive and dynamic programs.
In this post, we'll cover:
cin
for Inputcout
for Outputcin
for InputIn C++, the cin
object is used to accept input from the user. It is part of the iostream
library and is typically used in conjunction with the extraction operator (>>
).
cin >> variable;
Example:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: "; // Prompt for user input
cin >> age; // Take user input and store it in 'age'
cout << "You are " << age << " years old." << endl; // Display output
return 0;
}
In this example:
cout
.age
using cin
.cout
.cin
is used to receive input from the user.>>
operator is used to extract the data from the input stream and assign it to the variable.cout
for OutputIn C++, the cout
object is used to display output to the screen. It is also part of the iostream
library and is typically used in conjunction with the insertion operator (<<
).
cout << expression;
Example:
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl; // Output message to screen
return 0;
}
In this example, "Hello, World!"
is printed to the screen using cout
.
cout
is used to display output to the console.<<
operator is used to insert data into the output stream.C++ allows you to format output in various ways, such as controlling the width of output, setting precision for floating-point numbers, and displaying special characters like newlines.
endl
You can use endl
to insert a newline in your output. It moves the cursor to the next line, which helps in displaying clean output.
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl; // Output "Hello" with a newline
cout << "World!" << endl; // Output "World!" on the next line
return 0;
}
You can control the width and precision of output using the setw
and setprecision
manipulators. To use these, you need to include the iomanip
library.
setw(n)
: Specifies the width of the output field (e.g., setw(10)
ensures the output takes up 10 characters).setprecision(n)
: Specifies the number of decimal places for floating-point numbers.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
double pi = 3.14159265358979;
cout << "Value of pi: " << setprecision(5) << pi << endl; // Set precision to 5 digits
cout << "Formatted output with setw: " << setw(10) << pi << endl; // Set field width to 10
return 0;
}
setw
and setprecision
allow for controlling the width and precision of numeric output.endl
is used to insert a newline in the output.You can read multiple inputs in a single statement using cin
. Just separate the inputs with spaces or newlines, and cin
will store them in corresponding variables.
#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter two numbers: ";
cin >> a >> b; // Read two integer values
cout << "Sum: " << a + b << endl; // Display sum
return 0;
}
In this example, the user is prompted to enter two numbers, and the program calculates and displays their sum.
You can also read strings using cin
. However, by default, cin
will stop reading input when it encounters a space. If you want to read a full line (including spaces), use getline()
.
#include <iostream>
#include <string>
using namespace std;
int main() {
string name;
cout << "Enter your name: ";
getline(cin, name); // Read an entire line, including spaces
cout << "Hello, " << name << "!" << endl; // Greet the user
return 0;
}
In this example, getline(cin, name)
is used to read the full name of the user, including spaces.
cin
can be used to read various data types, including integers, floating-point numbers, and strings.getline(cin, string)
is used to read a full line of text, including spaces.