Reading a Paragraph in C++
To read a paragraph of text that includes spaces, we can use the std::getline() function that can read the whole paragraph until the newline character is encountered which indicates the end of the paragraph.
C++ Program to Read a Paragraph of Text with Spaces
The below program demonstrates how we can use the getline() function to read a paragraph of text.
C++
// C++ program to demonstrate getline function to read a // paragraph of text #include <iostream> #include <string> using namespace std; int main() { // prompt user to enter paragraph of text cout << "Enter a paragraph of text:" << endl; // Declare a string to store the input string paragraph; // Read input until Enter is pressed getline(cin, paragraph); // Display the entered paragraph cout << "Paragraph you entered:\n" << paragraph << endl; return 0; } |
Output
Enter a paragraph of text:
Hey! Geek Welcome to w3wiki. Start your coding journey ;)
Paragraph you entered:
Hey! Geek Welcome to w3wiki. Start your coding journey ;)
We can also use loops to read multiple paragraphs or continue reading until a specific condition is met.
How to Read a Paragraph of Text with Spaces in C++?
In C++, we may need to read the input that includes the paragraph of text with spaces. In this article, we will learn how to read a paragraph of text with spaces in C++.