I am having trouble with double. The program seems to rounding when it feels like it depending on the input. Its probably something pretty simple but I thought "double" was to store decimal input. Can someone explain to me what is going on?
2 common issues:
1: output format. if you don't specify how many digits, you don't see the entire double. Be sure to output the double as much as you want to see of it using set precision and set width modifiers to cout. try
cout << std::setprecision(20) << variable << endl;
2: actual floating point representation. Some values cannot be stored exactly in floating point format, and you end up with like 7.999999999999999999999999999999999 instead of 8.0 even if you input 8.0. (this is a made up example, I don't memorize which values can't be stored). Or the reverse, you get 8.00000000000000000000001 instead of 8.0. This is normal expected behavior and its fine as long as you don't try to do == comparisons of values (zero is stored exactly, so == on zero is ok for special cases / checks).
also, consider just using pennies for money, using integer math.
In lines 5 - 7 you do not need the "f" at the end of your number. This is causing the number to be stored wrong. It is more like a float than a double.
After removing the "f" this is the output that is produced:
please enter the amount of milk produced:
5000
1322.75
The number of milk cartons needed is: 1323
The cost of producing the milk: 1900
the profit of producing the milk: 357.143
Press any key to continue . . .
Just a comment here, your output could use a little more information along with some blank lines. For "5000" is this gallons or liters or something else? In this case what is "1322.75"?
Its in liters. So the issue I am running into is that when I enter 4900 i am getting this
1 2 3 4 5 6
please enter the amount of milk produced:
4900
The number of milk cartons needed is: 1296
The cost of producing the milk: 1862
the profit of producing the milk: 350
Press any key to continue . . .
which it is not the answer I should be getting. It should be 349.92 and not 350 at the last output. This is the issue I am running into.
If NUMmilk is the NUMBER OF milk cartons then it should be an int - you should do the rounding on line 21. You might have to consider whether to round up, round down or round to nearest.
It's my experience that drinks cartons don't come in fractional parts ... except when they fall off the back of my bicycle.