0

This is my code:

#include <iostream>

using namespace std;

const int MAX_LITERS_OF_MILK_PER_CARTON = 3.78;

const double COST_PER_LITER = 0.38;

const double PROFIT_PER_CARTON = 0.27;

int main(void)
 {
    // Write your main here
    int litersOfMilk;
    int numberOfCartonsNeeded;
    double actualNumberOfCartonsNeeded;
    double totalCostOfMilk;
    double totalProfit;

// Get the liters of mild from the user
    cout << "Enter the amount of milk in liters: ";
    cin >> litersOfMilk;
    cout << endl;

//Calculate the decimal number of milk cartons needed
actualNumberOfCartonsNeeded = static_cast<double> (litersOfMilk) / MAX_LITERS_OF_MILK_PER_CARTON;

//Calculate the number of cartons needed
numberOfCartonsNeeded = static_cast<int> (actualNumberOfCartonsNeeded + 0.5);

//Calculate the total cost of producing the milk today
totalCostOfMilk = litersOfMilk * COST_PER_LITER;

//Calculate the profit of the milk for today
totalProfit = litersOfMilk * (PROFIT_PER_CARTON/MAX_LITERS_OF_MILK_PER_CARTON);

//Output the number of cartons needed, the total cost of the milk & the profit to the screen
cout << "Number of Cartons Needed: " << numberOfCartonsNeeded << endl;
cout << "Total Cost to produce " << litersOfMilk << " liters: " << totalCostOfMilk << endl;
cout << "Total Profit to produce " << litersOfMilk << " liters: " << totalProfit << endl;


    return 0;
}

When the program runs, every time there is division, it is doing integer division.

For example, the constant MAX_LITERS_OF_MILK_PER_CARTON is defined as 3.78, but when I divide by this constant, it is dividing by 3 instead of dividing by 3.78

How do I fix this?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Matt
  • 11
  • 1
  • 6
  • 7
    `MAX_LITERS_OF_MILK_PER_CARTON` is the integer `3`. Did you mean to define this as a double? – 1201ProgramAlarm Feb 27 '20 at 21:07
  • Just use doubles or floats instead of integers.Why are you trying to use an integer to represent a non-integer value, while use doubles in other places? – Nathan Wride Feb 27 '20 at 21:09
  • @NathanWride Or even better, don't use `double`s (or any real numbers) for money. Instead, represent them as the amount of cents, as integers. OP's risking selling their milk for $3.799999993 per gallon due to floating-point inaccuracies. – HolyBlackCat Feb 27 '20 at 21:10
  • @HolyBlackCat haha that's true, I guess another option is to decide on a quanta, like cents, so you can use integers for everything which would make `MAX_LITERS_OF_MILK_PER_CARTON = 378`, `COST_PER_LITER = 38` and `PROFIT_PER_CARTON = 27`. – Nathan Wride Feb 27 '20 at 21:14
  • @NathanWride: Although you need to be careful with such a scheme: some commodities are quoted in subdivided monetary units with appropriate roundup following the multiplication. – Bathsheba Feb 27 '20 at 21:15

1 Answers1

3

Your first job is to replace

const int MAX_LITERS_OF_MILK_PER_CARTON = 3.78;

with

const double MAX_LITERS_OF_MILK_PER_CARTON = 3.78;

(Didn't your compiler warn you of this?)

Then you can write

totalProfit = litersOfMilk * PROFIT_PER_CARTON / MAX_LITERS_OF_MILK_PER_CARTON;

Finally, use

std::round(actualNumberOfCartonsNeeded)

rather than

static_cast<int>(actualNumberOfCartonsNeeded + 0.5);

This adding 0.5 to effect normal rounding has pitfalls which fooled the Java bods right up to version seven!

Bathsheba
  • 231,907
  • 34
  • 361
  • 483