Getline from Console
Programming
This program demonstrates reading a full line of input (including spaces) from the console using std::getline().
Implementation
#include <iostream>
#include <string>
int main() {
std::string name;
std::cout << "Enter your name: ";
std::getline(std::cin, name);
std::cout << "Hello, " << name << "!" << std::endl;
return 0;
} Key Concepts
- std::string: Standard library string class for text
- std::getline(): Reads a line of text (including spaces) until newline
- Difference from
>>:std::cin >>stops at whitespace, whilegetline()reads the entire line - Useful for reading names, addresses, or any input containing spaces
Example Interaction
Enter your name: John Doe
Hello, John Doe!