Loading...

C++ Numbers

This page keeps the nine lines from the string-methods page and adds three lines under them. That page ended with pine, 2, 0.5, 1, 4, pine cone, p, 5, and cone. That nine-line screen is the step this page builds on. The first box still holds the word pine, and the comment // the word stays pine still sits above the declarations.
This module stays on the same file. types.cpp keeps its name, its boxes, and its build command, so the example changes only by three new writing lines. The new lines do arithmetic. They use the int box count, which still holds 2, and they use two plain number expressions with no boxes at all. The point of the page is to see how C++ treats whole numbers and numbers with a decimal point when you calculate with them.
Numbers show up in almost every program. A shopping total adds prices, a timer subtracts seconds, a game multiplies damage by a factor, and a progress bar divides finished steps by all steps. Each of those jobs depends on the type of the numbers involved, so this page checks that part before the longer examples arrive.

The four basic operators are plus, minus, star, and slash. With two int values, the plus sign adds, the minus sign subtracts, the star multiplies, and the slash divides. The expression count * 4 reads the box count, which holds the whole number 2, and multiplies it by the whole number 4. The result is 8, and cout prints it as a plain number. The result is an int too, because both parts are int. The box count itself does not change, so it still holds 2 for the lines that follow.
Operator order follows the usual school rules. Multiplication and division happen before addition and subtraction, and expressions in parentheses happen first. The line count * 4 + 1 gives 9, while count * (4 + 1) gives 10. This page uses one operator per line, so the order does not change the screen. Later pages return to this rule when a line has more than one operator.

Division is the part that surprises most readers. When both operands are int, C++ divides as whole numbers. The expression 7 / 2 does not produce 3.5. It produces 3, because the fraction .5 is dropped. This rule is called integer division, and it is not a bug. The result type follows the operands, so two whole numbers give a whole number. The drop always moves toward zero, so 7 / 2 gives 3, and a negative case such as -7 / 2 gives -3.
To keep the fraction, at least one operand must be a double. The expression 7.0 / 2 has a double on the left, because 7.0 has a decimal point. The division runs in double arithmetic, and the result is 3.5. The rule is simple: a decimal point in a number literal makes it a double, and one double operand makes the whole operation a double. A double box such as ratio also works in arithmetic, so ratio * count reads 0.5 times 2 and gives the double value 1. cout prints that value as 1, because the default output drops the trailing zeros after the decimal point.
Keep this pair of rules in mind: whole divided by whole gives whole, and any double operand gives a double. Most surprising output in early C++ comes from forgetting the first rule.

Type the file in this order, or add only the three new lines if types.cpp is already on disk. Keep the nine lines from the string-methods page. Under the find and substr writing lines, add three lines: one for count * 4, one for 7 / 2, and one for 7.0 / 2. The checked file is below. The file name on disk stays types.cpp.

types.cpp adds three arithmetic lines under the nine

cpp
#include <iostream>
#include <string>

int main()
{
    // the word stays pine
    std::string word = "pine";
    int count = 2;
    double ratio = 0.5;
    bool ready = true;
    std::string other = "cone";
    std::string joined = word + " " + other;
    std::cout << word << std::endl;
    std::cout << count << std::endl;
    std::cout << ratio << std::endl;
    std::cout << ready << std::endl;
    std::cout << word.size() << std::endl;
    std::cout << joined << std::endl;
    std::cout << word[0] << std::endl;
    std::cout << joined.find("cone") << std::endl;
    std::cout << joined.substr(5, 4) << std::endl;
    std::cout << count * 4 << std::endl;
    std::cout << 7 / 2 << std::endl;
    std::cout << 7.0 / 2 << std::endl;
    return 0;
}

Rebuild with g++ -std=c++17 types.cpp -o types and then run ./types. If g++ is missing and clang++ is the compiler you installed, swap only the tool name: clang++ -std=c++17 types.cpp -o types. The screen shows twelve lines: pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, and 3.5. The first nine lines are the same as on the string-methods page. The tenth line is count times 4, the eleventh is the whole division, and the twelfth is the decimal division.
Check the tenth line first. The box count holds 2, so the product is 8. Then check the eleventh line. It shows 3, not 3.5, because both numbers are whole. The twelfth line shows 3.5, because 7.0 is a double. The table gives each row its piece, so compare your run with it line by line. If the screen has more than twelve lines, check that no writing line was copied twice.

📊 Three arithmetic lines and what they show

Piece What the screen shows
Product 8, count times 4 with two int values
Whole 3, 7 / 2 with two int values drops the fraction
Decimal 3.5, 7.0 / 2 keeps the fraction as a double
Screen pine, 2, 0.5, 1, 4, pine cone, p, 5, cone, 8, 3, 3.5

Change the multiplier in the product line to count * 5, then rebuild. The tenth line becomes 10. Put the 4 back. Next, change 7 / 2 to 7 / 4. Both operands are still whole numbers, so the eleventh line becomes 1, the whole part of 1.75. Then change 7.0 / 2 to 7.0 / 4, and the twelfth line becomes 1.75, because the double keeps the fraction. Put the original numbers back before you leave. Finally, try ratio * count in a new writing line. The result is a double, and cout prints it as 1. That output is the same value as 1.0, and the default format shows only the digits that matter.
The next page is booleans. Its job is to compare numbers with the operators greater than and equal to, and to store the true or false answer in a bool box. The bool box ready already holds true, and the next page shows how a comparison makes such a value. This page stops before any comparison. You leave with twelve lines on the screen, the product 8, the whole result 3, and the decimal result 3.5. Count the lines before you leave. There are twelve, and the last one is 3.5.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top