How to Receive and Display a User Input String in C++

 In this article, we will create a program to receive and display User input Strings in C++.


How to read and display user input strings
How to receive and display user input strings


The following header files are used -

  • iostream - For input and output stream.
  • string - For string manipulation operations.


The following functions are used -

  • getline(cin, string_name) - For reading the user input string.


Code - 

//Program to recieve and display a user input string using C++ by Vishruth codes
#include<iostream>
#include<string>
using namespace std;

int main()
{
    string name;
    cout<<"\nEnter your name: ";
    getline(cin, name);
    cout<<"\nGreetings, "<<name<<"!";
    return 0;
}


Subject - 

In the above example, I have implemented a program to read and display user input strings in C++.


Basic features - 

  • Receives the User's name as input and outputs a greeting to the user.


Working - 

  1. In this program, the user is asked to enter their name, which is stored in the string variable name using getline function.
  2. The user's name is combined with another text (here 'Greetings,'), and is displayed together using cout function.


Output - 

How to read and display user input strings in c++
Sample Output



Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.


Post a Comment

0 Comments