In this article, we will create a program to receive and display User input Strings in C++.
![]() |
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.
- getline(cin, string_name) - For reading the user input string.
//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.
- In this program, the user is asked to enter their name, which is stored in the string variable name using getline function.
- The user's name is combined with another text (here 'Greetings,'), and is displayed together using cout function.
Output -
![]() |
Sample Output |
Thanks for reading. If you found any errors or have suggestions to improve the article, please mention them in the comments.
0 Comments