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

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.




Reffered: https://www.geeksforgeeks.org


C++

Related
How to Find the Mode in a Sorted Array in C++? How to Find the Mode in a Sorted Array in C++?
How to Implement Priority Queue Using Multimap in C++? How to Implement Priority Queue Using Multimap in C++?
How to Find First Occurrence of an Element in a Set in C++? How to Find First Occurrence of an Element in a Set in C++?
How to Check if a Vector Contains a Given Element in C++? How to Check if a Vector Contains a Given Element in C++?
How to Create a Map with Vectors as Keys and Sets as Values? How to Create a Map with Vectors as Keys and Sets as Values?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11