User Input in C++
Read a name and an age from the keyboard with cin in C++. See why cin >> stops at the first space, when to use getline, and how a bad number breaks the stream.
1#include <iostream>2#include <string>3using namespace std;45int main() {6 string name;7 int age;89 cout << "Enter your name: ";10 cin >> name;1112 cout << "Enter your age: ";13 cin >> age;1415 cout << "Hello, " << name << "! You are " << age << " years old." << endl;1617 return 0;18}
Sample Output
Enter your name: Alice
The prompt, with the user typing Alice on the same line.
Enter your age: 25
Second prompt; 25 is converted to an int.
Hello, Alice! You are 25 years old.
Both values printed back inside one sentence.
In this one
- How cin >> reads one whitespace-separated word into a variable
- Why a two-word name needs getline(cin, name)
- What happens when a user types letters into an int
- Why mixing >> and getline can skip a line
How it works
- 1
Declare string name and int age with no values yet.
- 2
Print a prompt without endl so the cursor waits on the same line.
- 3
cin >> name reads characters up to the first space or Enter.
- 4
cin >> age converts the next word into an int.
- 5
One cout chain prints the greeting with both values.
Edge cases
Full name typed
Input: Alice Smith, then the program asks for age.
name gets Alice. Smith stays in the buffer, so cin >> age tries to read Smith as a number, fails, and age is not set to anything useful. Use getline(cin, name) for names with spaces.
Letters for age
Input for age: twenty.
Extraction fails and cin enters a failed state. Every later cin >> also fails until you call cin.clear() and discard the bad input. Check with if (!(cin >> age)).
What's going on
This C++ program reads a name and an age with cin and prints them back. cin >> is the usual first input, and it has a sharp edge.
#include <iostream> and #include <string>
cout / cin from iostream. string name needs <string>.
string name;
int age;
Declared, not filled yet. Until cin writes them, treat them as junk.
cout << "Enter your name: ";
No endl on purpose, so the cursor stays on the same line as the prompt.
cin >> name;
Reads one word. A space stops it. Type Alice Smith and name is just Alice. Then age tries to parse Smith and the stream fails. getline(cin, name) keeps the surname.
cin >> age;
Wants a number. Type twenty and cin fails. Later prints look insane unless you check if (!(cin >> age)).
cout << "Hello, " << name << "! You are " << age << " years old." << endl;
Text and variables in one chain. Sample: Alice and 25 → Hello, Alice! You are 25 years old.
Mixing >> and getline later leaves a newline in the buffer. The next getline then reads an empty line. That's a common bug, not a broken keyboard.
Next useful step is math on those numbers — Sum of Two Numbers.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
User types "Bob Lee" for cin >> name. What is in name?
Pick an answer to check it.
Question 2
Which call reads a whole line including spaces?
Pick an answer to check it.
Questions people ask
How do I read a full line with spaces?
Use getline(cin, name). It reads everything up to the Enter key, spaces included.
Why does getline return an empty string after cin >> age?
cin >> leaves the Enter key (a newline character) in the buffer. The next getline sees that newline and stops at once. Call cin.ignore() after the >> read, or use cin >> ws before getline.
What value does age hold if the read fails?
Since C++11, a failed integer read stores 0 into the variable. Older compilers left it unchanged. Either way, you should check the stream instead of trusting the value.