> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/08.-user-input.md).

# 08. User Input

`std::cin` ("console in") reads input the user types in the terminal. It's the counterpart to `std::cout`.

```cpp
#include <iostream>

int main() {
    int age;

    std::cout << "Enter your age: ";
    std::cin >> age;

    std::cout << "You are " << age << " years old\n";

    return 0;
}
```

`>>` here is the **extraction operator** — it pulls data *out of* the input stream and *into* your variable. (Notice it points the opposite direction of `<<`, which pushes data *out* to the console.)

***

### Reading multiple values at once

```cpp
#include <iostream>

int main() {
    int a, b;

    std::cout << "Enter two numbers: ";
    std::cin >> a >> b; // chain multiple reads together

    std::cout << "Sum: " << a + b << '\n';

    return 0;
}
```

The user can type `5 10` (space-separated) and hit Enter — `cin` splits on whitespace automatically.

***

### ⚠️ The classic `cin` + strings trap

`std::cin >> someString` only reads **one word** — it stops at the first space. To read a full line (like a full name), use `std::getline`.

```cpp
#include <iostream>
#include <string>

int main() {
    std::string fullName;

    std::cout << "Enter your full name: ";
    std::getline(std::cin, fullName); // reads the WHOLE line, spaces included

    std::cout << "Hello, " << fullName << "!\n";

    return 0;
}
```

**Compare the two:**

```cpp
std::string name;
std::cin >> name;          // "Cristiano Ronaldo" -> only stores "Cristiano"
std::getline(std::cin, name); // "Cristiano Ronaldo" -> stores the whole thing
```

***

### More cool examples

#### 1. Simple age checker

```cpp
#include <iostream>

int main() {
    int age;

    std::cout << "Enter your age: ";
    std::cin >> age;

    if (age >= 18) {
        std::cout << "You're an adult\n";
    } else {
        std::cout << "You're a minor\n";
    }

    return 0;
}
```

#### 2. Mini calculator using input

```cpp
#include <iostream>

int main() {
    double num1, num2;
    char op;

    std::cout << "Enter calculation (e.g. 5 + 3): ";
    std::cin >> num1 >> op >> num2;

    double result = 0;

    if (op == '+') result = num1 + num2;
    else if (op == '-') result = num1 - num2;
    else if (op == '*') result = num1 * num2;
    else if (op == '/') result = num1 / num2;

    std::cout << "Result: " << result << '\n';

    return 0;
}
```

#### 3. Reading name + age together (mixing `cin >>` and `getline`)

```cpp
#include <iostream>
#include <string>

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

    std::cout << "Enter your name: ";
    std::getline(std::cin, name);

    std::cout << "Enter your age: ";
    std::cin >> age;

    std::cout << name << " is " << age << " years old\n";

    return 0;
}
```

#### 4. A tiny login-style prompt loop

```cpp
#include <iostream>
#include <string>

int main() {
    std::string password;

    std::cout << "Enter password: ";
    std::cin >> password;

    if (password == "1234") {
        std::cout << "Access granted\n";
    } else {
        std::cout << "Access denied\n";
    }

    return 0;
}
```

#### 5. Validating input isn't garbage

```cpp
#include <iostream>

int main() {
    int number;

    std::cout << "Enter a number: ";

    if (std::cin >> number) {
        std::cout << "You entered: " << number << '\n';
    } else {
        std::cout << "That wasn't a valid number!\n";
    }

    return 0;
}
```

***

### Quick Recap

* `std::cin >> variable;` reads input, stops at whitespace.
* `std::getline(std::cin, stringVar);` reads a **full line**, spaces included — use this for names/sentences.
* You can chain reads: `std::cin >> a >> b;`
* `std::cin` can be checked in an `if` to detect bad/invalid input.
* Mixing `cin >>` and `getline` in the same program needs care — `cin >>` leaves a leftover newline that can mess up the next `getline` (a common next-level gotcha to look out for).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/08.-user-input.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
