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).
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.
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.
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.
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!
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...
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.
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.
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".
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::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
elseif (speed == "medium")
total = price * 1.1;
// Fast Speed
elseif (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.