Input and output (I/O) are fundamental operations that allow a program to communicate with the user or other systems. C++ provides several ways to handle I/O operations, primarily through the standard input and output streams.
C++ uses streams to perform input and output operations. The most commonly used streams are:
std::cin
: Standard input stream (usually associated with keyboard input)std::cout
: Standard output stream (usually associated with the console)std::cerr
: Standard error stream (used for error messages)std::clog
: Standard logging stream (used for logging messages)To use these streams, include the iostream
header:
#include <iostream>
std::cout
for OutputThe std::cout
stream is used to output data to the console. The insertion operator (<<
) is used to send data to the std::cout
stream.
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
int number = 42;
std::cout << "The number is: " << number << std::endl;
return 0;
}
std::cout
: Outputs data to the console.<<
: The insertion operator.std::endl
: Inserts a newline character and flushes the stream.C++ supports formatted output using manipulators from the iomanip
header.
#include <iostream>
#include <iomanip>
int main() {
double pi = 3.14159;
std::cout << "Pi: " << std::fixed << std::setprecision(2) << pi << std::endl;
return 0;
}
std::fixed
: Sets the floating-point number to fixed-point notation.std::setprecision(2)
: Sets the precision of the floating-point number to 2 decimal places.std::cin
for InputThe std::cin
stream is used to receive input from the user. The extraction operator (>>
) is used to read data from the std::cin
stream.
#include <iostream>
int main() {
int age;
std::cout << "Enter your age: ";
std::cin >> age;
std::cout << "Your age is: " << age << std::endl;
return 0;
}
std::cin
: Reads data from the console.>>
: The extraction operator.You can read multiple values using a single std::cin
statement.
#include <iostream>
int main() {
int a, b;
std::cout << "Enter two numbers: ";
std::cin >> a >> b;
std::cout << "The numbers are: " << a << " and " << b << std::endl;
return 0;
}
std::cin
with StringsThe std::cin
stream reads a single word into a string. To read an entire line, use the std::getline
function.
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
}
std::getline
for Full LinesTo read an entire line including spaces, use std::getline
.
#include <iostream>
#include <string>
int main() {
std::string fullName;
std::cout << "Enter your full name: ";
std::getline(std::cin, fullName);
std::cout << "Hello, " << fullName << "!" << std::endl;
return 0;
}
std::cerr
for Error MessagesThe std::cerr
stream is used to output error messages. It behaves like std::cout
but is typically used for errors.
#include <iostream>
int main() {
std::cerr << "An error occurred!" << std::endl;
return 0;
}
std::clog
for LoggingThe std::clog
stream is used for logging messages. It also behaves like std::cout
.
#include <iostream>
int main() {
std::clog << "Logging information..." << std::endl;
return 0;
}
C++ provides several manipulators for formatting output, available in the iomanip
header.
#include <iostream>
#include <iomanip>
int main() {
int width = 10;
std::cout << std::setw(width) << "Right" << std::endl;
std::cout << std::setw(width) << std::left << "Left" << std::endl;
std::cout << std::setw(width) << std::right << "Right" << std::endl;
return 0;
}
std::setw(width)
: Sets the field width for the next insertion operation.std::left
: Left-justifies the output.std::right
: Right-justifies the output.std::cerr
to display error messages and handle exceptions properly.std::endl
or std::flush
to ensure output is written to the console immediately.std::getline
for Strings: Use std::getline
to read entire lines of text to avoid input issues with spaces.