In this lesson, you will learn
In C++, input and output operations are primarily handled using streams, which are objects that allow data to flow into or out of the program.
The most commonly used stream objects are:
std::cout: Standard output stream, typically used for writing output to the screen.std::cin: Standard input stream, typically used for reading input from the keyboard.
std::cout is used to output data to the standard output stream, which is typically the console or terminal.<< operator, known as the insertion or put-to operator, is used with std::cout to send data to the output stream.
std::cout
#include<iostream>
int main() {
int number = 10;
double pi = 3.14159;
std::string message = "Hello, World!";
// Outputting different types of data
std::cout << "Number: " << number << std::endl;
std::cout << "Value of pi: " << pi << std::endl;
std::cout << "Message: " << message << std::endl;
return 0;
}
std::coutCascading with std::cout involves using multiple insertion operators (<<) in a single statement to concatenate several outputs. This approach can make the code more readable and concise.
std::cout
#include<iostream>
int main() {
int age = 25;
double height = 175.5;
std::string name = "Alice";
// Cascading output with std::cout
std::cout << "Name: " << name
<< ", Age: " << age
<< ", Height: " << height
<< " cm" << std::endl;
return 0;
}
The basic syntax for using cin involves the extraction operator or get from (>>), which directs the input from the keyboard into the specified variable.
For example:
int age;
std::cin >> age;
It reads an integer value from the user and stores it in the variable age. The cin object can handle various data types, including integers, floats, characters, and strings.

std::cin
#include<iostream>
int main() {
int number;
std::cout << "Enter an integer: ";
std::cin >> number;
std::cout << "You entered: " << number << std::endl;
return 0;
}
In this example:
std::cout << "Enter an integer: " Prints a prompt to the screen.std::cin >> number; reads an integer from the user and stores it in the variable number.std::cout << "You entered: " << number << std::endl; Outputs the entered number to the screen.std::cinCascading std::cin with multiple extraction operators (>>) allows you to read various values from the input stream in a single statement.
This can be useful for reading several pieces of data in sequence.
std::cin For Multiple Inputs
#include<iostream>
int main() {
int age;
double height;
std::string name;
// Reading multiple values in a single statement
std::cout << "Enter your name, age, and height (separated by spaces): ";
std::cin >> name >> age >> height; // Reads all three values
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Height: " << height << " cm" << std::endl;
return 0;
}
Enter your name, age, and height (separated by spaces): Amit
28
170
Name: Amit
Age: 28
Height: 170 cm
There are no reviews yet. Be the first one to write one.
You must be logged in to submit a review.