Skip to content

Text and Arithmetic

The Course
•
35 min study

A study report needs two kinds of data: text such as a topic name, and numbers such as minutes converted into hours. Both look simple, and both hide a surprise. Text must be stored somewhere that can grow as you add to it. Division of whole numbers throws away the fraction, so 75 minutes divided by 60 gives 1 hour, not 1.25. This lesson shows how C++ handles each and how to get the answer you meant.

std::string owns a sequence of characters. Include <string> when you use it. "Owns" means the string object is responsible for the memory that holds its characters: when you append text, it finds more room by itself, and when the string goes away, it releases that memory. You never choose a buffer size or free anything by hand.

Predict and run

One line joins text; the other divides the same number two ways. Predict both.

Predict before running C++
#include <iostream>
#include <string>

int main() {
    std::string topic = "C++";
    std::string title = topic + " practice";
    int minutes = 75;
    std::cout << title << "\n";
    std::cout << minutes / 60 << " " << minutes / 60.0 << "\n";
    return 0;
}
Check your prediction
Text
C++ practice
1 1.25

topic + " practice" built a new string, and title owns that result. topic itself is unchanged. In the second line both operands of minutes / 60 are int, so C++ performs integer division and discards the fractional part. In minutes / 60.0 one operand is a double, so the int is converted and the division keeps the fraction.

Join and grow strings

+ joins a string with more text and produces a new string. += appends to an existing string in place. Each string is a separate object with its own characters, so changing one does not change another, even if it was built from it.

Build a title step by step C++
#include <iostream>
#include <string>

int main() {
    std::string topic = "Loops";
    std::string label = topic + " review";
    topic += " and arrays";
    std::cout << topic << "\n";
    std::cout << label << "\n";
    return 0;
}
Text
Loops and arrays
Loops review

label was built from topic before the append, and it kept its own copy of the text. There is one trap: two string literals alone cannot be joined with +. A literal such as "C++" is not a std::string; it is a fixed array of characters, so "C++" + " practice" is a compile error. Start from a std::string variable, as above, and every + after it works.

Integer division and remainder

For whole numbers, / gives how many times one value fits into another, and % gives what is left over. Together they split minutes into hours and minutes without any fractions.

Hours, minutes and fractions C++
#include <iostream>

int main() {
    int minutes = 135;
    std::cout << minutes / 60 << " h " << minutes % 60 << " min\n";
    double wrong = minutes / 60;
    double right = minutes / 60.0;
    std::cout << wrong << " " << right << "\n";
    std::cout << (2 + 3 * 4) << " " << ((2 + 3) * 4) << "\n";
    return 0;
}
Text
2 h 15 min
2 2.25
14 20

wrong is a double, yet it holds 2. The division minutes / 60 happened first, entirely in int, and produced 2; only then was that 2 converted to a double. The type of the variable receiving a result does not change how the expression on the right is calculated. To keep the fraction, make one operand a double before dividing.

The last line shows precedence. Multiplication and division happen before addition and subtraction, so 2 + 3 * 4 is 2 + 12, which is 14. Parentheses change the grouping: (2 + 3) * 4 is 20. The example also wraps each expression in parentheses before sending it to std::cout, because << has its own precedence and parentheses make the intent obvious to a reader.

Two operations to avoid

  • Dividing by zero. Integer division or remainder by zero is undefined behavior: the program may crash or produce nonsense, and nothing warns you at run time. Check that a divisor is not zero before dividing whenever it comes from data.
  • Overflowing an int. Multiplying large values can exceed the largest int, which is also undefined behavior. The planner uses small durations, and later lessons validate input so totals stay well inside the range.

Practice

Exercise

Your turn

Store the topic Reading in a std::string and a duration of 135 minutes in an int. Print exactly:

  • Reading: 2 h 15 min
  • Reading: 2.25 hours

Calculate every number from the minutes variable.

Your workspace C++
#include <iostream>

int main() {
    // Write your solution here.
    return 0;
}
Hint 1: the idea

Whole hours come from /, leftover minutes come from %, and fractional hours need a double operand.

Hint 2: a first step

Include <string>, declare std::string topic = "Reading";, and print minutes / 60 first to check the whole hours.

Show one solution
Run the solution C++
#include <iostream>
#include <string>

int main() {
    std::string topic = "Reading";
    int minutes = 135;
    std::cout << topic << ": " << minutes / 60 << " h " << minutes % 60 << " min\n";
    std::cout << topic << ": " << minutes / 60.0 << " hours\n";
    return 0;
}
Text
Reading: 2 h 15 min
Reading: 2.25 hours

The first line stays in whole numbers on purpose, so / and % split the duration exactly. The second line uses 60.0, which turns the division into floating-point arithmetic and keeps the quarter hour. Change minutes to 90 and both lines update by themselves. In the checkpoint you will combine named values, strings and these calculations into a small report.

Was this article helpful?

Your response is saved on this device.