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
Include Libraries: The code includes the necessary libraries for input/output and string handling.
Namespace: The
using namespace std;
statement allows the code to use names from thestd
namespace without needing to prefix them withstd::
.Main Function: The
main()
function is the entry point of the program.Variable Declaration: A string variable
name
is declared to store the user's input.Output Prompt: The program prompts the user to enter their name using the
cout
statement.Input: The
getline(cin, name);
statement reads a line of input from the standard input (in this case, the keyboard) and stores it in thename
variable.Output Greeting: The program displays a greeting message using the
cout
statement, including the user's input.Return: The
return 0;
statement indicates that the program has executed successfully and returns a status code to the operating system.