Output formatting

If the user inputs 0 and 0, how can i get both "Weight must be greater than zero" and "We do not ship less than 10 miles or more than 3000" without requiring an additional input? is it as simple as removing cin >> weight?

thank you


#include<iostream>
#include <iomanip>
using namespace std;

int main()

{
#include<iostream>
#include <iomanip>
using namespace std;

int main();
const int distanceUnit = 500;
double rate = 0;
double shippingCost;
int weight;
int distance;
int distanceunit;

cout << setprecision(2) << fixed;

cout << "Weight of the package in kilograms (max 20 kg):";
cin >> weight;
cout << "Distance the package is to be shipped (min 10 mi, max 3000 Mi):";
cin >> distance;
while (weight <= 0)
{
cout << "Weight must be greater than zero ";
cin >> weight;
}
while (weight > 20)
{
cout << "Weight must be less than twenty kilograms ";
cin >> weight;
}
while (distance < 10 || distance > 3000)
{
cout << "We do not ship less than 10 miles or more than 3000 miles";
cin >> distance;
}
distanceunit = (distance / distanceUnit) + 1;


if (weight <= 2)
{
rate = 1.10;
}
else if (weight > 2 && weight <= 6)
{
rate = 2.20;
}
else if (weight > 6 && weight <= 10)
{
rate = 3.70;
}
else
{
rate = 4.80;
}

shippingCost = distanceunit * rate;
cout << "The shipping charge is $" << shippingCost << endl << endl;

return 0;
}
Hello XxImaWafflexX,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



I think you will find this arrangement of more use. I just moved thing around. I moved the first two while loops up because yo are doing them at the wrong time.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include <iomanip>
using namespace std;

int main()

{
#include<iostream>
#include <iomanip>
	using namespace std;

	int main();
	const int distanceUnit = 500;
	double rate = 0;
	double shippingCost;
	int weight;
	int distance;
	int distanceunit;

	cout << setprecision(2) << fixed;
	//std::cout << std::fixed << std::showpoint << std::setprecision(2);

	cout << "Weight of the package in kilograms (max 20 kg):";
	cin >> weight;

	while (weight <= 0)
	{
		cout << "Weight must be greater than zero ";
		cin >> weight;
	}
	while (weight > 20)
	{
		cout << "Weight must be less than twenty kilograms ";
		cin >> weight;
	}

	cout << "Distance the package is to be shipped (min 10 mi, max 3000 Mi):";
	cin >> distance;

	while (distance < 10 || distance > 3000)
	{
		cout << "We do not ship less than 10 miles or more than 3000 miles";
		cin >> distance;
	}

	distanceunit = (distance / distanceUnit) + 1;


	if (weight <= 2)
	{
		rate = 1.10;
	}
	else if (weight > 2 && weight <= 6)
	{
		rate = 2.20;
	}
	else if (weight > 6 && weight <= 10)
	{
		rate = 3.70;
	}
	else
	{
		rate = 4.80;
	}

	shippingCost = distanceunit * rate;
	cout << "The shipping charge is $" << shippingCost << endl << endl;

	return 0;
}

You may find that line 21 may work better. The "std::showpoint" will print ".00" should it arise and you need it.

Hope that helps,

Andy
Hello XxImaWafflexX,

Just as I posted I noticed that lines 8 - 10 should not be there.

Thinking about I would delete lines 3, 8 and 9 and leave line 10 inside although it is best not to use line 10 anywhere.

Andy
Topic archived. No new replies allowed.