0
votes

I'm doing another coding exercise and my code is not giving me correct answers.

For example:

What was the total amount of milk produced in the morning: 3590.56

  • Number of cartons = 949 supposed to be 950
  • Cost of producing = 1364.41 correct
  • Profit of producing = 256.47 supposed to be 256.5

Below is my code

#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>

using namespace std;

const double CARTON = 3.78;
const double PRODUCECOST = 0.38;
const double PROFIT = 0.27;

int main() {
    double milkProduced, numCartons;
    double costOfProducing, profitOfProducing;

    cout << fixed << showpoint << setprecision(2);

    cout << "What was the total amount of milk produced in the morning: " << endl;
    cin >> milkProduced;

    numCartons = milkProduced / CARTON;
    costOfProducing = milkProduced * PRODUCECOST;
    profitOfProducing = numCartons * PROFIT;

    cout << static_cast<int>(numCartons) << endl;
    cout << costOfProducing << endl;
    cout << profitOfProducing << endl;

    return 0;
}
1
Is there an easier way to post code? The reason why I posted a link is because I don't know how to post the entirety of it without doing samples. Is there a faster way? Copy and pasting it makes the code look weird.Darunia
Darunia, you can just copy the code in (making sure there's a blank line before it), select it with mouse or arrow keys, and press CTRL-K. That indents the selected text by 4 spaces, which is what makes it look like a code block (I basically did that when I copied your code to on-site).paxdiablo

1 Answers

2
votes

That's very close, but your num_cartons value is actually 949.883... and, when you cast that to an int, it truncates the fractional part rather than rounding it.

In addition, you only do that for the output and do not feed that change back into the variable for the subsequent calculations (specifically, the profit).

I suggest, rather than doing that, you try:

numCartons = round(milkProduced / CARTON); // needs <cmath>

to set the value.

That will make the number of cartons an integral value, or close enough for classwork assignments. In other words:

#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>
#include<cmath>

using namespace std;

const double CARTON = 3.78;
const double PRODUCECOST = 0.38;
const double PROFIT = 0.27;

int main() {
    double milkProduced, numCartons;
    double costOfProducing, profitOfProducing;

    cout << fixed << showpoint << setprecision(2);

    cout << "What was the total amount of milk produced in the morning: " << endl;
    cin >> milkProduced;

    numCartons = round(milkProduced / CARTON);
    costOfProducing = milkProduced * PRODUCECOST;
    profitOfProducing = numCartons * PROFIT;

    cout << numCartons << endl;
    cout << costOfProducing << endl;
    cout << profitOfProducing << endl;

    return 0;
}

The output of that is more in line with what you expect:

> ./testProg
What was the total amount of milk produced in the morning: 3590.56
950.00
1364.41
256.50

As pointed out in the comments, you possibly should be truncating rather than rounding down, since you can most likely not sell a partially filled container (or add water if you're, shall we say, ethically ambiguous).

However, even if that is the case, you should modify the variable so that it affects the profit figure, rather than just outputting the rounded value and using the original.

I've only rounded since you stated that 950 was the correct carton count.