← Back to C++ Course | Chapter 2: Input & Output | Lesson 8 of 8

C++ String Input

String input is how your program reads words or whole sentences from the person typing. cin reads one word at a time, while getline grabs a whole line.
Syntax
cpp
std::string variable_name;

std::cin >> variable_name;             // reads one word

std::getline(std::cin, variable_name);  // reads the whole line

String Input with cin

cin >> variable reads input only up to the first whitespace character — a space, tab, or newline — which makes it well suited for reading a single word but unable to capture a full sentence in one read.

If the user types "John Smith", cin >> name captures only "John".

Example: String Input with cin

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("John Smith");
	std::string name;
	cin >> name; // stops at the first space
	std::cout << name << std::endl; // prints "John" only
	return 0;
}

Full Line Input with getline

getline(cin, variable) reads an entire line, spaces included, stopping only when the user presses Enter, which makes it the right tool whenever input might contain multiple words — full names, addresses, or sentences.

Unlike cin >>, it never skips over whitespace within the line.

Example: Full Line Input with getline

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("John Smith");
	std::string fullName;
	std::getline(cin, fullName); // reads the whole line, spaces included
	std::cout << fullName << std::endl;
	return 0;
}

Mixing cin and getline

Mixing cin >> and getline in the same program is a classic pitfall: cin >> leaves the trailing newline character in the input buffer, and a getline call right after immediately reads that leftover newline as an empty line instead of waiting for new input.

Calling cin.ignore() between them clears that leftover newline so getline behaves as expected.

Example: Mixing cin and getline

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("25\nJohn Smith\n");
	int age;
	cin >> age;
	cin.ignore(); // clears the leftover newline before getline
	std::string name;
	std::getline(cin, name);
	std::cout << age << " " << name << std::endl;
	return 0;
}

Limiting String Input

When reading into a fixed-size C-style char array, an unbounded read can overflow the buffer if the user types more characters than the array can hold, corrupting nearby memory.

Using cin.width() or the setw manipulator caps how many characters are read, preventing that overflow at the source.

Example: Limiting String Input

cpp
#include <iostream>
#include <iomanip>

int main() {
	char buffer[6];
	std::cin.width(6); // caps how many characters are read into buffer
	std::cout << "Buffer capped to 6 characters" << std::endl;
	return 0;
}

Reading Custom Delimited Strings

getline accepts an optional third argument specifying a custom delimiter character to stop at instead of the default newline — getline(cin, value, ','), for instance, reads up to the next comma.

This makes it useful for parsing simple comma-separated input directly from the console without writing your own splitting logic.

Example: Reading Custom Delimited Strings

cpp
#include <iostream>
#include <sstream>

int main() {
	std::istringstream cin("apple,banana,cherry");
	std::string value;
	std::getline(cin, value, ','); // stops at the comma, not newline
	std::cout << value << std::endl;
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Mixing cin >> x and getline without clearing the leftover newline, so getline returns an empty string.
  2. Reading a full name with cin >> name and getting only the first word.
  3. Forgetting #include <string> when using getline with std::string.
Chapter Summary
  • cin reads input and cout prints output, including numbers and new lines.
  • cerr and clog write error and log messages.
  • Escape sequences, the iomanip library, and string input help control how data is formatted and read.
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.