Trouble creating Shipping Cost program

Hello. I am trying to create a shipping program for class. I have constructed a code with a pretty good idea on how to calculate the price by pound, but I am having trouble visualizing where to insert additional calculations for shipping speed (slow, medium or fast). Here's the instructions provided by my professor...

Create a program which calculates shipping costs. Ask the user for package size in lbs and the speed of delivery (slow, medium,fast).

Use the following to calculate prices based on weights:

0-1 lbs: $5 per pound
1-3 lbs: $3 per pound
3-10 lbs: $1 per pound

If the speed is slow, multiply the result by by 1.0 (no change), medium costs 10% more (multiply by 1.1), and fast costs 50% more (1.5).

Output the shipping cost as a single number.

and here is the code I have constructed so far...

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
  #include <iostream>

using namespace std;

int main() {

	// 0-1 pounds:  $5 per pound
       //  1-3 pounds:  $3 per pound
       //  3-10 pounds: $1 per pound

	double pounds = 0;
	double price = 0;
	double speed = 0;

	cout << "Welcome to the shipping cost calculator." << endl;
	cout << "Enter package size (In LBS): ";
	cin >> pounds;
    cout << "Enter shipping speed (Slow, Medium or Fast): ";
    
	// 0-1 pounds: $5 per pound
	if (pounds > 0 && pounds < 1) {
		price = pounds * 5;
	}
	// 1-3 pounds: $3 per pound
	else if (pounds >= 100 && pounds < 3) {
		price = pounds * 3;
	}
	//  3-10 pounds: $1 per pound
	else if (pounds >= 3 && pounds < 10) {
		price = pounds * 1;
	}
	else if (pounds >= 500) {
		cout << "We cannot handle orders that large." << endl;
}
	else {
		cout << "Pounds must be positive." << endl;
	}

	if (price > 0) {
    cout << "Price: $" << price << endl;
	}
	
	system("pause");
	return 0;
}


I just want to know if I'm on the right track. Where is the proper place to insert shipping speed calculations? I know they would be included in the "if" statements, right? I'm still new to programming. Thank you.
Last edited on
If you included the adjustment for shipping speed inside the existing if statements, then you would need to copy the same code 3 times, once for each of the 3 code blocks that set up the price depending on the weight category.

It would be better to have a separate if statement after you've done the weight-based calculation, to adjust the price for the chosen speed.
Line 18: You prompt for the shipping speed, but never input the value.

Line 38: You would create an if/else tree similar to what you have done for the weight, except in this case you multiply the already calculated price by the speed surcharge.

You need how you want to deal with the speed. A single char is easiest, but you can also use a string. You also need to consider if you want to deal with both upper and lower case answers.


I created a separate If Tree, as you both suggested, and the code compiles, but I'm definitely not getting the results I want.

I've been playing around with this for an hour and looking at past examples created in class.

I was trying to see if I should include the shipping speeds (slow, medium or fast) as double variables, but when I input the words (slow, medium or fast), the program terminates. And if I choose 2 as the package weight, it shifts to line 39 and I get "Pounds must be Positive". I also included another variable called "total" to calculate the grand total by multiplying the already calculated price.
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
#include <iostream>

using namespace std;

int main() {

	// 0-1 pounds:  $5 per pound
	//  1-3 pounds:  $3 per pound
	//  3-10 pounds: $1 per pound

	double pounds = 0;
	double price = 0;
	double speed = 0;
	double slow;
	double medium;
	double fast;
	double total = 0;

	cout << "Welcome to the shipping cost calculator." << endl;
	cout << "Enter package size (In LBS): ";
	cin >> pounds;

	// 0-1 pounds: $5 per pound
	if (pounds > 0 && pounds < 1) {
		price = pounds * 5;
	}
	// 1-3 pounds: $3 per pound
	else if (pounds >= 100 && pounds < 3) {
		price = pounds * 3;
	}
	//  3-10 pounds: $1 per pound
	else if (pounds >= 3 && pounds < 10) {
		price = pounds * 1;
	}
	else if (pounds >= 10) {
		cout << "We cannot handle orders that large." << endl;
	}
	else {
		cout << "Pounds must be positive." << endl;
	}

	if (price > 0) {
		cout << "Price: $" << price << endl;
	}

	cout << "Enter Shipping Speed (Slow, Medium, or Fast) ";
	cin >> speed;

	// Slow Speed
	if (cin >> slow) {
		total = price * 1;
	}
	// Medium Speed
	else if (cin >> medium) {
		total = price * 1.1;
	}
	//  Fast Speed
	else if (cin >> fast) {
		total = price * 1.5;

		cout << "Grand total for shipping cost is: $" << total << endl;
	}
	system("pause");
	return 0;
}


I tried to create a tree as suggested, but it's far from what the professor wants. I hope I can get this figured out as it's due Wednesday.

Where am I going wrong here?
28: else if (pounds >= 100 && pounds < 3) { will never be true.

total = price * 1; what's the point of * 1 ?

Why don't you use speed ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Enter Shipping Speed (Slow, Medium, or Fast) ";
	cin >> speed;

	// Slow Speed
	if (cin >> slow) {
		total = price * 1;
	}
	// Medium Speed
	else if (cin >> medium) {
		total = price * 1.1;
	}
	//  Fast Speed
	else if (cin >> fast) {
		total = price * 1.5;
Hello JustMcCollum,

On line 47 you input "speed", but "speed" is defined as a double and I think you are wanting to enter a string. This will not work.

You if statements should look more like if (speed == "slow") and as Thomas1965 pointed out total = price * 1; has no real point. The * 1 has no use.

You have the right idea just going about it th wrong way,

Hope that helps,

Andy

EDIT: typo regarding if statement
Last edited on
I fixed the errors as Andy and Thomas suggested, here's my code now:

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
#include <iostream>

using namespace std;

int main() {

	// 0-1 pounds:  $5 per pound
	//  1-3 pounds:  $3 per pound
	//  3-10 pounds: $1 per pound

	double pounds = 0;
	double price = 0;
	double speed = 0;
	double total = 0;
        double slow;
	double medium;
	double fast;


	cout << "Welcome to the shipping cost calculator." << endl;
	cout << "Enter package size (In LBS): ";
	cin >> pounds;

	// 0-1 pounds: $5 per pound
	if (pounds > 0 && pounds < 1) {
		price = pounds * 5;
	}
	// 1-3 pounds: $3 per pound
	else if (pounds >= 1 && pounds < 3) {
		price = pounds * 3;
	}
	//  3-10 pounds: $1 per pound
	else if (pounds >= 3 && pounds < 10) {
		price = pounds * 1;
	}
	else if (pounds >= 10) {
		cout << "We cannot handle orders that large." << endl;
	}
	else {
		cout << "Pounds must be positive." << endl;
	}

	if (price > 0) {
		cout << "Price: $" << price << endl;
	}

	cout << "Enter Shipping Speed (Slow, Medium, or Fast) ";
	cin >> speed;

	// Slow Speed
	if (speed = slow) {
		total = price;
	}
	// Medium Speed
	else if (speed = medium) {
		total = price * 1.1;
	}
	//  Fast Speed
	else if (speed = fast) {
		total = price * 1.5;

		cout << "Price: $" << price << endl;
	}
	system("pause");
	return 0;
}


But Handy Andy, as I am taking an Introduction to C++ course, we haven't talked about strings yet, we will talk about them at a later point in the semester. I was wondering, could you show me what a string would look like in my case of code? It would be much appreciated!
Last edited on
Okay, I looked over our last class examples, and we briefly talked about strings, but it's the most simple form of strings and while statements you can get.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main() {

	string actual_password = "cop2000";
	string entered_password = "";


	while (actual_password != entered_password) {
		cout << "Enter password: ";
		cin >> entered_password;
	}
	cout << "Correct!" << endl;

	system("pause");
	return 0;
}


but for the program I'm creating now, I imagine it would require a bit more complex string and while statements. I tried taking the idea of strings we used in class toward this program, now it won't compile. How do I incorporate strings with if statements? Here's a redo of my code including strings that won't compile...

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
#include <iostream>

using namespace std;

int main() {

	// 0-1 pounds:  $5 per pound
	//  1-3 pounds:  $3 per pound
	//  3-10 pounds: $1 per pound

	string speed = "slow";
	string speed = "medium";
	string speed = "fast";
	double pounds = 0;
	double price = 0;
	double total = 0;



	cout << "Welcome to the shipping cost calculator." << endl;
	cout << "Enter package size (In LBS): ";
	cin >> pounds;

	// 0-1 pounds: $5 per pound
	if (pounds > 0 && pounds < 1) {
		price = pounds * 5;
	}
	// 1-3 pounds: $3 per pound
	else if (pounds >= 1 && pounds < 3) {
		price = pounds * 3;
	}
	//  3-10 pounds: $1 per pound
	else if (pounds >= 3 && pounds < 10) {
		price = pounds * 1;
	}
	else if (pounds >= 10) {
		cout << "We cannot handle orders that large." << endl;
	}
	else {
		cout << "Pounds must be positive." << endl;
	}

	if (price > 0) {
		cout << "Price: $" << price << endl;
	}

	cout << "Enter Shipping Speed (Slow, Medium, or Fast) ";

	// Slow Speed
	if (while (speed != slow)) {
		total = price;
	}
	// Medium Speed
	else if (while (speed != medium)) {
		total = price * 1.1;
	}
	//  Fast Speed
	else if while (speed != fast)) {
		total = price * 1.5;

		cout << "Price: $" << total << endl;
	}
	system("pause");
	return 0;
}


How do I correct the incorporation of the "string" and "while" when using "If" statements?
Last edited on
It's not soo complicated.
1
2
3
4
5
6
7
8
9
10
11
12
cout << "Enter Shipping Speed (Slow, Medium, or Fast) ";
string speed;
cin >> speed;

if (speed == "Slow")
  // price remains the same
else if (speed == "Medium")
  // 10% higher price
else if (speed == "Fast")
  // 50% higher price
else
  // handle illegal input 


No need to a while loop.
I'm sorry I'm overcomplicating things, I only have class 1 day a week and it's only an hour and 15 minutes long, so I don't get to learn sufficiently as I should. That's why I come to you guys for help, because you have helped made things clearer for me than in class, unfortunately.

Thank you Thomas1965 for helping the code compile. However, there is still a major issue...

Whenever I input "Slow or Medium" when it asks me, the program terminates, and doesn't do speed calculations. However, "Fast" seems to work.

Here's my code with the changes you suggested

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
#include <iostream>

using namespace std;

int main() {

	// 0-1 pounds:  $5 per pound
	//  1-3 pounds:  $3 per pound
	//  3-10 pounds: $1 per pound

	double pounds = 0;
	double price = 0;
	double total = 0;

	cout << "Welcome to the shipping cost calculator." << endl;
	cout << "Enter package size (In LBS): ";
	cin >> pounds;

	// 0-1 pounds: $5 per pound
	if (pounds > 0 && pounds < 1) {
		price = pounds * 5;
	}
	// 1-3 pounds: $3 per pound
	else if (pounds >= 1 && pounds < 3) {
		price = pounds * 3;
	}
	//  3-10 pounds: $1 per pound
	else if (pounds >= 3 && pounds < 10) {
		price = pounds * 1;
	}
	else if (pounds >= 10) {
		cout << "We cannot handle orders that large." << endl;
	}
	else {
		cout << "Pounds must be positive." << endl;
	}

	if (price > 0) {
		cout << "Price: $" << price << endl;
	}

	cout << "Enter Shipping Speed (Slow, Medium, or Fast): ";
    string speed;
    cin >> speed;
    
    // Slow Speed
	if (speed == "slow") {
		total = price;
	}
	// Medium Speed
	else if (speed == "medium") {
		total = price * 1.1;
	}
	//  Fast Speed
	else if (speed == "fast") {
		total = price * 1.5;

		cout << "Grand Total: $" << total << endl;
	}
	system("pause");
	return 0;
}


Why is my code working when I input "Fast" but terminates when I input "Slow" or "Medium?
Last edited on
Nevermind! I did it! All I had to do was copy line 58 in the original code:
cout << "Grand Total: $" << total << endl;

and paste it before the last brackets of each speed if statement, like this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    cout << "Enter Shipping Speed (Slow, Medium, or Fast): ";
    string speed;
    cin >> speed;
    
    // Slow Speed
	if (speed == "slow") {
		total = price;
		cout << "Grand Total: $" << total << endl;
	}
	// Medium Speed
	else if (speed == "medium") {
		total = price * 1.1;
		cout << "Grand Total: $" << total << endl;
	}
	//  Fast Speed
	else if (speed == "fast") {
		total = price * 1.5;

		cout << "Grand Total: $" << total << endl;
	}
	system("pause");
	return 0;
}


Thank you guys for your help and suggestions. I'll post if I have any more questions!
Last edited on
Actually, I do have another question...

I have an "else" statement stating if the pounds of the package is greater than 10, it will say...
1
2
else if (pounds >= 10) {
		cout << "We cannot handle orders that large." << endl;


but the code continues to part 2, where it does the shipping calculations, even if it's greater than 10 lbs. I want the program to stop at part one once it says "We cannot handle orders that large."

What can I insert in the code to stop the program at that point?
Last edited on
Sth. like this;
1
2
3
4
5
else 
{
  cout << "Pounds must be positive." << endl;
  return -1;
}
Some options:

1) As Thomas says, you can simply return from your function at that point; because this is the main function, that will terminate the program. Although, presumably, you'll also want to deal with the case where pounds is negative, too.

2) Add a flag, to record whether an acceptable weight has been entered. Only proceed with the shipping speed option if the flag has been set to the appropriate value.

3) Realise that that you already have a good measure of whether a valid weight has been entered - whether or not the price is 0 after line 37. You're already using this to control whether to output a price; you can also use it to control whether you proceed with the shipping speed stuff.

Hello JustMcCollum,

Two things that I found.

When you prompt the user for a shipping speed "(Slow, Medium, or Fast)" each word starts with a capital letter, but when check the value of "speed" in the if statements the comparison is to a string of lower case letters. These conparisions are case sensitive. Eeither change you prompt to all lowercase letters, easiest, or change the value of "speed" to all lower case letters. I have a function to change the case if you are interested.

The second fix goes with MikeyBoy's third point. I took the closing } of if (price > 0) and moved it down to include the if/else if statements for speed. Also as you will see you only need one line of std::cout << "Grand Total: $" << total << std::endl; after the if/else if statements. It is the comparision of the variable "speed" to const string "slow" that is the problem.

I also added a line to format the output of "price" and "total".

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
	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Added for price and total.

	if (price > 0)
	{
		std::cout << "Price: $" << price << std::endl;


		std::cout << "Enter Shipping Speed (Slow, Medium, or Fast): ";
		std::string speed;
		std::cin >> speed;

                //  <--- A good [lace to make sure "speed" is all lower case.

		// Slow Speed
		if (speed == "slow")  // <--- speed and const string both need to be the same case.
		{
			total = price;
		}
		// Medium Speed
		else if (speed == "medium")
		{
			total = price * 1.1;
		}
		//  Fast Speed
		else if (speed == "fast")
		{
			total = price * 1.5;
		}

                //  Only prints if price is > 0.0.
		std::cout << "Grand Total: $" << total << std::endl;
	}  // <--- Moved from original if to here 


You also need to include the header files <string> and <iomanip>. "iomanip" for the setprecision.

Hope that helps,

Andy
Last edited on
Registered users can post here. Sign in or register to post.