getline cin is being skipped

#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    cout << "Enter your name: ";
    getline(cin, name);
    cout << "Hello, " << name << "!" << endl;
    return 0;
}

Explanation of the Code

  1. Include Libraries: The code includes the necessary libraries for input/output and string handling.

  2. Namespace: The using namespace std; statement allows the code to use names from the std namespace without needing to prefix them with std::.

  3. Main Function: The main() function is the entry point of the program.

  4. Variable Declaration: A string variable name is declared to store the user's input.

  5. Output Prompt: The program prompts the user to enter their name using the cout statement.

  6. Input: The getline(cin, name); statement reads a line of input from the standard input (in this case, the keyboard) and stores it in the name variable.

  7. Output Greeting: The program displays a greeting message using the cout statement, including the user's input.

  8. Return: The return 0; statement indicates that the program has executed successfully and returns a status code to the operating system.