Skip to content
Laviyx Learn

Try

C++ course menu

C++ User Input: std::cin, getline and Validating Input

Read keyboard input in C++ with std::cin: numbers, words, whole lines with std::getline, mixing cin and getline correctly, reading several values at once, and checking for invalid input.

Beginner8 min readUpdated
On this page

Programs become interactive when they read input. std::cin ("character input"), from <iostream>, reads what the user types in the terminal.

In the examples below, the output shows exactly what you would see in the terminal, including what the user typed after each prompt.

Reading a number#

>> takes a value out of std::cin and stores it in a variable, converting it to the variable's type:

C++
#include <iostream>

int main() {
    int age = 0;
    std::cout << "How old are you? ";
    std::cin >> age;
    std::cout << "Next year you will be " << age + 1 << ".\n";
}
Output
How old are you? 21
Next year you will be 22.

The arrows point the way the data flows: std::cout << value sends data out to the screen, and std::cin >> variable brings data in from the keyboard.

Reading several values#

You can chain >>. The values can be separated by spaces or newlines:

C++
#include <iostream>

int main() {
    int a = 0;
    int b = 0;
    std::cout << "Enter two numbers: ";
    std::cin >> a >> b;
    std::cout << a << " + " << b << " = " << a + b << '\n';
}
Output
Enter two numbers: 12 7
12 + 7 = 19

Reading words and whole lines#

std::cin >> text reads one word: it stops at the first space. To read a full line, including spaces, use std::getline:

C++
#include <iostream>
#include <string>

int main() {
    std::string full_name;
    std::cout << "Full name: ";
    std::getline(std::cin, full_name);
    std::cout << "Hello, " << full_name << "!\n";
}
Output
Full name: Asha Rao
Hello, Asha Rao!

With std::cin >> full_name, the program would only store Asha, and Rao would wait in the input for the next read.

The cin + getline trap#

Mixing >> and getline causes a classic bug. After std::cin >> age, the Enter key you pressed is still waiting in the input. The next getline reads that empty remainder and returns immediately with an empty string.

The fix is to discard the rest of the line after reading a number. std::ws skips any leftover whitespace, including that newline:

C++
#include <iostream>
#include <string>

int main() {
    int age = 0;
    std::string name;

    std::cout << "Age: ";
    std::cin >> age;
    std::cout << "Full name: ";
    std::getline(std::cin >> std::ws, name);

    std::cout << name << " is " << age << ".\n";
}
Output
Age: 21
Full name: Asha Rao
Asha Rao is 21.

Another common fix is std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); (from <limits>), which throws away everything up to and including the next newline.

Checking for invalid input#

If the user types abc when a number is expected, the read fails: the variable gets 0, and std::cin enters a fail state in which all further reads are ignored. You can test a read directly in an if:

C++
#include <iostream>

int main() {
    int quantity = 0;
    std::cout << "Quantity: ";
    if (std::cin >> quantity) {
        std::cout << "You ordered " << quantity << ".\n";
    } else {
        std::cout << "That was not a number.\n";
    }
}
Output
Quantity: abc
That was not a number.

To ask again after a failure, clear the fail state with std::cin.clear(), discard the bad input with std::cin.ignore(...), and repeat in a loop. You will see the full pattern in while and do-while.

Reading until the input ends#

while (std::cin >> value) keeps reading until there is no more input (or invalid input). This is how many programs process a list of numbers:

C++
#include <iostream>

int main() {
    int value = 0;
    int sum = 0;
    int count = 0;

    while (std::cin >> value) {
        sum += value;
        ++count;
    }
    std::cout << count << " numbers, sum " << sum << '\n';
}
Output
4 8 15 16 23 42
6 numbers, sum 108

When typing in a terminal, signal the end of input with Ctrl + Z then Enter on Windows, or Ctrl + D on macOS and Linux.

Common mistakes#

Practice#

Practice exercise

Ask for the width and height of a rectangle (they may have decimals) and print its area and perimeter. In the example run, the user types 4.5 and 2.

Show solution
C++
#include <iostream>

int main() {
    double width = 0;
    double height = 0;

    std::cout << "Width: ";
    std::cin >> width;
    std::cout << "Height: ";
    std::cin >> height;

    std::cout << "Area: " << width * height << '\n';
    std::cout << "Perimeter: " << 2 * (width + height) << '\n';
}
Output
Width: 4.5
Height: 2
Area: 9
Perimeter: 13

Key takeaway#

Finished this lesson?