A result can be stored in a double and still lose a fraction before it reaches that variable. The types used during the calculation determine how division works.
Consider seven liters shared between two containers:
#include <iostream>
int main() {
int liters = 7;
int containers = 2;
double wholeFirst = liters / containers;
double share = static_cast<double>(liters) / containers;
std::cout << wholeFirst << "\n";
std::cout << share << "\n";
return 0;
}
The first output line prints 3, not 3.5. Evaluate the initializer first: liters / containers runs with two integer operands, producing an integer result. Only then is that result stored in wholeFirst. Choosing double for the destination happens too late to preserve the fraction.
static_cast<double>(liters) explicitly converts the value of liters to double for this expression. The word between angle brackets is the destination type; the value to convert goes in parentheses. With a floating-point operand, division produces a floating-point result. The second line prints 3.5.
The original liters variable remains an integer and keeps its value. A cast here produces a converted value; it does not rewrite the variable's declaration. Converting the finished integer quotient would be too late, so place the cast around an operand before /.
Your starter divides 9 meters of ribbon between 2 displays. Both input counts remain integers. Replace the result placeholder with the calculation, converting meters to double before dividing. The output statement is already supplied, and the nonzero divisor keeps the calculation safe.
perDisplay using static_cast<double>(meters) / displays, so the supplied line reads Meters per display: 4.5.Edit main.cpp, press Run to inspect the Console, then Submit to check these results.