Having trouble with "double"

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include<iomanip>
#include <cmath>
using namespace std;
const double MilK = 3.78f;
const double cost = 0.38f;
const double milk = 0.27f;

int main() {
	//cout << fixed << showpoint << setprecision(12);

	double milkTotal;
	double NUMmilk;
	double milkCost;
	double milkProf;
	

	// Write your main here
	cout << "please enter the amount of milk produced: " << endl;
	cin >> milkTotal;
	NUMmilk = milkTotal / MilK;
	cout << NUMmilk << endl;
	milkCost = cost * milkTotal;
	milkProf = milk * NUMmilk;

	cout << "The number of milk cartons needed is: " << round(NUMmilk) << endl;
	cout << "The cost of producing the milk: " << milkCost << endl;
	cout << "the profit of producing the milk: " << milkProf << endl;
	system("pause");
	return 0;
}
Last edited on
show exactly what it is doing.

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.

Last edited on
Hello CantSpel,

That makes two of us. And I can not type either.

Welcome to the forum.

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"?

Hope that helps,

Andy
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.
Last edited on
I will put in the liters to ;)
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.
ok that worked thank you, but why does when as a double it rounds itself, in case this happens again.
so if I do this
1
2
3
4
double test = 1296.296*.27;
cout << test << endl;

350

why does it do this
sorry for doing this but the given answer is wrong on the assignment,going to tell the professor.
Hello CantSpel,

Another thing you could is remove the comment from line 10 and change 12 to 2. That will show 2 decimal places for the "double" numbers.

As lastchance pointed out if you round on line 21 you will not need to round on line 26.

And for you output you could try this:
1
2
3
std::cout << std::noshowpoint << std::setprecision(0);
cout << "The number of milk cartons needed is: " << NUMmilk << endl;
std::cout << std::showpoint << std::setprecision(2);

Did it this way to make a point. Actually most of line 1 could be put at the beginning of line 2 and line 3 in the next line.

Hope that helps,

Andy
Topic archived. No new replies allowed.