C++ Program to Read a String with Spaces and Display it on Console

In this program, we will read a string with spaces using gets() function. The puts() function is used to display the entered string.

The function gets() : Reads characters from the standard input and stores them as a string. It stops when either the newline character is read or when the end-of-file is reached, whichever comes first.

The function puts() : prints characters from the standard output.

The cin>>) function reads a single word (without spaces), however gets() reads a line of string (with spaces)



Source Code
#include<iostream>
using namespace std;

int main()
{
    char name[30];
    cout<<"Enter the line of text: ";
    gets(name);
    cout<<"\nYou entered the text....\n";
    puts(name);
    return 0;
}
Output
Enter the line of text: Welcome to the world of C++ Programming!

You entered the text....
Welcome to the world of C++ Programming!