Skip to content
KALANBLON
Show contents

Chapter 3 of 4 · 20 min

Variables, types and text

By the end of this chapter, you'll be able to

  • Declare int, double, bool, char and std::string variables
  • Display decimal numbers with a fixed number of decimal places
  • Protect a value with const and let the compiler deduce a type with auto
  • Predict the result of an integer division and of the modulo

The basic types

#include <iostream>
#include <string>

int main() {
    std::string player = "Nadia";
    int score = 1250;
    double health = 87.5;
    bool has_key = true;
    char rank = 'A';

    std::cout << player << ": " << score << " points, " << health << "% health\n";
    std::cout << "Rank " << rank << ", key: " << has_key << "\n";
    std::cout << std::boolalpha << "Key found? " << has_key << "\n";
    return 0;
}
Nadia: 1250 points, 87.5% health
Rank A, key: 1
Key found? true
TypeHoldsExample
inta whole number42, -7
doublea decimal number3.14, -0.5
booltrue or falsetrue, false
chara character'A', '?'
std::stringa text"Nadia"

By default, std::cout displays a bool as 1 or 0. std::boolalpha asks it to write true or false. std::string comes from the <string> library: it's a text of any length, which manages its memory by itself.

Decimals are written with a dot: 87.5.

Assigning, and two useful keywords

The = sign is assignment: it computes the value on the right and stores it in the variable on the left, replacing the old one.

We display the title and the number of coins.

Step 7 of 7
Text description of the animation
  1. A player picks up coins, then spends some.
  2. coins is 10.
  3. The right-hand side is computed with the old value, then stored in coins.
  4. coins -= 3 is a shortcut for coins = coins - 3.
  5. A text variable of type std::string.
  6. With std::strings, + joins texts together: this is concatenation.
  7. We display the title and the number of coins.
  • const makes a variable unchangeable: const double VAT = 0.18;. If you then try to change it, the compiler refuses. Use it for every value that mustn't move.
  • auto lets the compiler deduce the type from the value: auto total = 3 * 2.5; creates a double. Handy for types that are long to write, but don't overuse it to the point where you no longer know what your variables hold.

Calculations and decimals

The operators are +, -, *, / and % (the remainder of integer division). As in C, dividing two integers gives an integer:

#include <iostream>
#include <iomanip>

int main() {
    std::cout << 7 / 2 << " and " << 7 % 2 << "\n";
    std::cout << 7.0 / 2 << "\n";

    const double VAT = 0.18;
    double price = 12.0 * (1 + VAT);
    std::cout << price << "\n";
    std::cout << std::fixed << std::setprecision(2) << price << "\n";
    return 0;
}
3 and 1
3.5
14.16
14.16

By default, std::cout displays at most 6 significant digits and drops unnecessary zeros: 14.0 would be displayed 14. For prices, we always want two decimal places: std::fixed << std::setprecision(2), from <iomanip>, takes care of that. This setting stays active for every number displayed afterwards.

Your turn

Worked exercise

Worked exercise: the basket

A basket contains 3 items at 4.50 and 2 items at 12.25. Declare a constant VAT rate of 0.2, then display the total before tax and the total including tax, with two decimal places: Before tax: 38.00 and Incl. tax: 45.60.

Hint 1
First compute the total before tax: 3 * 4.50 + 2 * 12.25.
Hint 2
The total including tax is net * (1 + VAT). Turn on std::fixed << std::setprecision(2) once, before the first display.
Show the solution and common mistakes
#include <iostream>
#include <iomanip>

int main() {
    const double VAT = 0.2;
    double net = 3 * 4.50 + 2 * 12.25;
    double gross = net * (1 + VAT);
    std::cout << std::fixed << std::setprecision(2);
    std::cout << "Before tax: " << net << "\n";
    std::cout << "Incl. tax: " << gross << "\n";
    return 0;
}
Before tax: 38.00
Incl. tax: 45.60

Common mistakes:

  • Forgetting #include <iomanip>: std::setprecision is then unknown.
  • Writing VAT = 20: the rate is a proportion, 0.2, not a percentage. And since VAT is const, the compiler would refuse to change it anyway.

Summary

  1. 1Each variable has a type: int, double, bool, char or std::string (from <string>).
  2. 2= replaces a variable's value; + joins two std::strings.
  3. 3const stops a value from changing; auto deduces the type from the value.
  4. 4Dividing two ints gives an int: convert first with static_cast<double>.
  5. 5std::fixed << std::setprecision(2) always displays two decimal places.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.