Reading input (stdin)

Read what you type in the IDE's Input tab with std::cin and std::getline.

Runs in Zerethon IDE · C++ (GCC 14) Official C++ documentation ↗

A run cannot ask questions while it runs: type the input in the Input tab before pressing Run.

main.cpp
#include <iostream>
#include <string>

int main() {
    std::string name;
    std::getline(std::cin, name);   // the whole line, spaces included
    int a, b;
    std::cin >> a >> b;             // whitespace-separated values
    std::cout << "Hello, " << name << "! " << a << " + " << b << " = " << a + b << '\n';
}

Input (stdin)

Ada Lovelace
3 4

Output

Hello, Ada Lovelace! 3 + 4 = 7
main.cpp
#include <iostream>

int main() {
    int value, total = 0;
    while (std::cin >> value) total += value;
    std::cout << "total: " << total << '\n';
}

Input (stdin)

5
10
15

Output

total: 30
while (std::cin >> value) stops at the end of the input (or at text that is not a number).

Watch out: After std::cin >> n, the newline is still waiting: call std::cin.ignore() (or read with std::ws) before the next std::getline.