Press ESC to close

C++ Program to Get Input From User

Sure! Here’s a C++ program to gets the user’s name as input and then prints a friendly greeting. 

#include <iostream>
#include <string>
using namespace std;
int main() {
    // Declare a variable to hold the user's name
    string userName;
    // Ask the user for their name
    cout << "Please enter your name: ";
    // Get the user's name from input
    getline(cin, userName);
    cout << "Hello, " << userName << "! Nice to meet you!" << endl;
    return 0;
}

This program includes the #include <iostream> and #include <string> headers for input/output and string operations. It uses the getline function to read the entire line of input, which allows for names with spaces. The program then prints a friendly greeting using the user’s input.

Leave a Reply

Your email address will not be published. Required fields are marked *