Skip to content
CP4CPP logo
CP4CPP/EDU
App settings
Installation and offline settings

Preparing update checks...

Workspaces

12

Reading record

Reading statistics
Reading progress
-
Words
-
Reading estimate
-
Preset chosen
-
Code blocks
-
Images and diagrams
-
Preferences and widgets

    Shared across workspaces

    Heading markersPercentage markers

    Lesson 2 - Input, Variables, Computation, And Output

    • Lesson 1. You can build and run a complete program that prints a fixed line.
    • A working C++ setup, as described in Lesson 1.
    • How to declare an int variable with a clear name.
    • How to read values from standard input with std::cin.
    • How to compute a value and print it with std::cout.

    In Lesson 1 the output was fixed. Now the program reads numbers first, so the output depends on what it is given.

    Three new pieces appear:

    • A variable is a named place that holds one value. int firstNumber = 0; declares an integer variable named firstNumber and starts it at 0.
    • std::cin is the standard input stream. std::cin >> firstNumber; reads one integer from the input and stores it in firstNumber.
    • >> moves data from the stream on its left into the variable on its right. It points the opposite way from the << used for output.

    Whitespace between input numbers does not change how they are read. Spaces, tabs, and line breaks all act as separators, so 7 5 on one line and 7 then 5 on two lines are read the same way.

    Read two integers, store them in named variables, and print their sum.

    #include <iostream>
    int main() {
    int firstNumber = 0;
    int secondNumber = 0;
    std::cin >> firstNumber >> secondNumber;
    int sum = firstNumber + secondNumber;
    std::cout << sum << "\n";
    return 0;
    }
    Input:
    7 5
    Output:
    12

    Notes on the code:

    • std::cin >> firstNumber >> secondNumber; reads two integers in order. The first value read goes into firstNumber and the second into secondNumber.
    • int sum = firstNumber + secondNumber; computes the result once and gives it a name. Naming the result keeps the printing step simple.
    • std::cout << sum << "\n"; prints the value and then a newline. Printing a variable needs no quotation marks, because sum holds a value rather than text.

    Write a complete program that reads one integer and prints twice its value.

    Start from the worked example. These are all of the source changes required:

    1. Delete the line int secondNumber = 0;, because only one input value is needed now.
    2. Change std::cin >> firstNumber >> secondNumber; to std::cin >> firstNumber;, so the program reads one integer instead of two.
    3. Replace int sum = firstNumber + secondNumber; with int doubled = firstNumber * 2;, which multiplies the input value by two.
    4. Change std::cout << sum << "\n"; to std::cout << doubled << "\n";, so the printed name matches the new variable.

    Leave #include <iostream>, int main(), and return 0; unchanged.

    Input:
    7
    Expected output:
    14

    Check these before moving on:

    • The output is 14 and nothing else
    • The output still ends with a newline, as in the worked example
    • Input 0 prints 0, and input 100 prints 200

    You can now read input, name values, compute, and print. The next lesson uses only these tools on a task whose input and output format is fixed in advance.

    Reader layout

    Col / Left

    Left column

    LEFT
    Width
    Rail
    ...
    CENTER
    Width
    Rail
    ...
    RIGHT
    Width
    Rail
    ...
    TOP
    NAV
    MID
    BOT

    Theme settings

    Preset 0 · Default · Locked

    Advanced colors · OKLCH

    Choose preset 1-9 to edit. Changes save on this device. Each color inherits in this order: cell → row → column → app → base theme.

    Editing All

    Selected scope

    Chroma 0 is neutral gray. Displayable colors depend on your screen; the browser maps colors outside its gamut. Grayscale also affects images and charts.

    Progress appearance

    Loading controls…

    Forest0.88 kB
    Slate0.88 kB

    Font settings

    Preset 0 · Default · Locked

    Font typography
    Advanced typography

    Auto uses the selected spacing mode. Variants and available weights depend on the font. The browser may synthesize missing weights or styles.

    a b c d e f g h i j k l m n o p q r s t u v w x y z

    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

    0 1 2 3 4 5 6 7 8 9

    ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

    abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ

    Iosevka Extended10.76 MB
    Inter297.90 kB
    Source Sans 3197.90 kB
    Lora138.11 kB
    Source Serif 4216.02 kB
    JetBrains Mono130.64 kB
    Fira Code53.67 kB

    Keybinds

    Choose a binding, press up to 4 keys together, then release to save. Modifiers count as keys. Esc clears the selected binding. Unsupported keys show Unknown.

    Reader

    Forbidden key combinations

    Windows / Linux reference. These combinations are reserved by this app to avoid browser, system and editing conflicts. Some are intercepted before a page can receive them. Browser extensions and other operating systems may reserve additional keys.

    Fixed navigation: Ctrl + K opens Search. Alt + F6-F9 selects a row; Alt + F10-F12 selects a column. Combine both to select a cell. While selected, F1 captures a screenshot and F2 downloads PNG. Esc cancels dialogs and navigation when a binding is not being recorded.