How to Read a Paragraph of Text with Spaces in C++?
Last Updated :
19 Feb, 2024
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++.
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++
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Enter a paragraph of text:" << endl;
string paragraph;
getline(cin, paragraph);
cout << "Paragraph you entered:\n" << paragraph << endl;
return 0;
}
|
Output
Enter a paragraph of text:
Hey! Geek Welcome to GeeksforGeeks. Start your coding journey ;)
Paragraph you entered:
Hey! Geek Welcome to GeeksforGeeks. Start your coding journey ;)
We can also use loops to read multiple paragraphs or continue reading until a specific condition is met.
Share your thoughts in the comments
Please Login to comment...