Open In App

How to Read a Paragraph of Text with Spaces in C++?

Last Updated : 19 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// 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 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.




Next Article
Practice Tags :

Similar Reads