What is stream in #include <​iostream> in c++ code and its applications?

What is stream in #include <iostream> in c++ code and its applications?

In the context of the iostream library in C++, a stream is a sequence of characters or other data that is used for input or output operations. Streams are the underlying mechanism for reading from and writing to various sources, such as the keyboard, the screen, and files.

In C++, a stream is represented by an object of a class that is defined in the iostream library. For example, the cin and cout objects are streams that are used for standard input and output, respectively.

Streams have properties and methods that can be used to control the flow of data through the stream. For example, the width property can be used to control the number of characters that are written to the stream, and the precision property can be used to control the number of digits that are written after the decimal point.

By using streams and the classes and functions defined in the iostream library, C++ programs can perform a wide range of input and output operations in a simple and flexible way.

Here's an example of using the width property in C++ to control the width of output when using the cout stream:



#include <iostream>

#include <iomanip>


int main() {

std::cout << std::setw(10) << 123 << std::endl;

std::cout << std::setw(10) << 45.678 << std::endl;

std::cout << std::setw(10) << "Hello" << std::endl;

return 0;

}

In this example, the setw manipulator from the iomanip library is used to set the width of the output stream. The first call to setw sets the width to 10 characters, which means that the next output operation will be padded with spaces to reach a total width of 10 characters.

The output of this program will be:


123

45.678

Hello


As you can see, the values are right-aligned within the specified width of 10 characters.




Here's an example of using the width property in C++ to control the width of input when using the cin stream:


#include <iostream>


int main() {

int number;

double decimal;

std::string word;

std::cout << "Enter a number: ";

std::cin >> std::setw(10) >> number;

std::cout << "You entered: " << number << std::endl;

std::cout << "Enter a decimal number: ";

std::cin >> std::setw(10) >> decimal;

std::cout << "You entered: " << decimal << std::endl;

std::cout << "Enter a word: ";

std::cin >> std::setw(10) >> word;

std::cout << "You entered: " << word << std::endl;

return 0;


In this example, the setw manipulator is used to set the width of the input stream when reading data from the keyboard using the cin stream. The width of the input stream is set to 10 characters, which means that the next input operation will only read up to 10 characters from the keyboard.


When you run this program, you'll be prompted to enter values for number, decimal, and word. If you enter a value that is longer than 10 characters, only the first 10 characters will be read into the corresponding variable.

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics