When posting code, please use code tags. Highlight the code and click the <> button in the box to the right of the input window. Also, it will be easier to read your code if you indent it to follow the structure. Here is your code with tags and indented. See more comments below.
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 71 72 73 74 75 76 77
|
#include <iostream>
#include <string>
using namespace std;
int
main()
{
{
int Weight, State;
double ShippingCost, HazardCode, FinalCost, productPrice;
// Collect data
{
cout << "Please enter the products price: " << endl;
cin >> productPrice;
{
if (productPrice <= 0)
cout << "Error price has to be more than 0" << endl;
}
}
{
cout << "Please enter the weight of product: " << endl;
cin >> Weight;
{
if (Weight <= 0) {
cout << "Error weight must be over 0";
} else if (Weight < 20) {
ShippingCost = 14.95;
} else if (Weight < 50) {
ShippingCost = 19.95;
} else if (Weight >= 100) {
ShippingCost = 29.95;
} else if (Weight > 200) {
ShippingCost = 99.95;
}
cout << "Shipping cost = $ " << ShippingCost << endl;
}
}
{
cout << "Please enter the Hazard Code (A, B or C): ";
cin >> HazardCode;
{
if (HazardCode = 'A') {
HazardCode = 0; // increment the
}
if (HazardCode = 'B') {
HazardCode = 25.00;
}
if (HazardCode = 'C') {
HazardCode = 50.00;
} else
cout << "Error please ender A, B, or C" << endl;
}
}
{
{
cout << "Are you shipping out of state? Yes or No: " << endl;
cin >> State;
// Final Cost
if (State = 'Yes') {
FinalCost =
(productPrice + ShippingCost + HazardCode) * 0.06 -
10.00;
}
if (State = 'No') {
FinalCost = productPrice + ShippingCost + HazardCode;
} else {
cout << "Error" << endl;
}
}
}
}
system("pause");
return 0;
}
|
Lines 8 & 9: weight should probably be a double. change State to a string. HazzardCode should be a char.
Line 30: You have the comparison wrong.:
Line 32: You missed weights between 100 and 200 lbs.
Line 35: I don't think you should print the shipping costs yet. It looks to me like you should print the final shipping cost, after all the adjustments.
Lines 42-49: You should adjus the shipping cost based on the hazzard code. For example:
1 2 3
|
if (HazardCode = 'B') {
ShippingCost += 25.00;
}
|
Lines 61 & 66: strings are surrounded by double-quotes. Characters are surrounded by single quotes. So you want
"Yes"
and
"No"
instead of
'Yes'
and
'No'
.
Line 63: Since you're going to adjust the shipping cost based on hazzard code above, there's no need to add in the hazzard code here. Also multiplying by 0.06 gives you the tax, only, not the amount plus the tax, so you want to multiply by 1.06
Lines 62 & 67: I think these lines should actually just adjust the shipping cost based on whether it's in-state or not. Then after after all that, you should print the output.
General comment: You have a lot of unnecessary braces in the code. They highlight the structure but don't do much otherwise. It's better to highlight the structure with comments. That way you can say what each piece of code does. Better yet would be to break the pieces into separate functions. Here is the code with the extra braces removed and comments at the top of the blocks:
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
|
#include <iostream>
#include <string>
using namespace std;
int
main()
{
int Weight, State;
double ShippingCost, HazardCode, FinalCost, productPrice;
// Collect data
cout << "Please enter the products price: " << endl;
cin >> productPrice;
if (productPrice <= 0)
cout << "Error price has to be more than 0" << endl;
cout << "Please enter the weight of product: " << endl;
cin >> Weight;
// Compute base shipping cost
if (Weight <= 0) {
cout << "Error weight must be over 0";
} else if (Weight < 20) {
ShippingCost = 14.95;
} else if (Weight < 50) {
ShippingCost = 19.95;
} else if (Weight >= 100) {
ShippingCost = 29.95;
} else if (Weight > 200) {
ShippingCost = 99.95;
}
cout << "Shipping cost = $ " << ShippingCost << endl;
// Get hazzard code and adjust shipping cost
cout << "Please enter the Hazard Code (A, B or C): ";
cin >> HazardCode;
if (HazardCode = 'A') {
HazardCode = 0; // increment the
}
if (HazardCode = 'B') {
HazardCode = 25.00;
}
if (HazardCode = 'C') {
HazardCode = 50.00;
} else
cout << "Error please ender A, B, or C" << endl;
// Get in-state shipping and adjust shipping cost
cout << "Are you shipping out of state? Yes or No: " << endl;
cin >> State;
if (State = 'Yes') {
FinalCost =
(productPrice + ShippingCost + HazardCode) * 0.06 -
10.00;
}
if (State = 'No') {
FinalCost = productPrice + ShippingCost + HazardCode;
} else {
cout << "Error" << endl;
}
system("pause");
return 0;
}
|