C++ String Input
In this page:
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: