Program won't execute shipping calculation

Why won't my program run the calculations? I was able to compile it without getting any errors. But when I plugged some numbers in, I got 0 for the shipping cost calculation. I can't see where I went wrong and I've been searching for a few hours.

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>
# include <iomanip>
using namespace std;
int main ()
{
  // Declare variables for shipment type
  char R, P, O;

  // Assign variable to delivery type
  int type;

  // Assign variable to package weight and price
  float weight, price;

  // Ask user to specify shipping service and weight of package
  cout << "Specify service type (R/P/0) and package weight (XX.X)" << endl;
  cin >> type >> weight;

  // Assign cost to package
  switch (type)
    {
    case 'R':
      if (weight < 2)
        price = (1 * weight);
      if (weight > 2 && weight <= 6)
        price = (1.5 * weight);
      if (weight > 6 && weight <= 10)
        price = (2 * weight);
      if (weight > 10)
        price = (2.5 * weight);
    case 'P':
      if (weight < 2)
        price = (3.5 * weight);
      if (weight > 2 && weight <= 6)
        price = (5.5 * weight);
      if (weight > 6 && weight <= 10)
        price = (7.5 * weight);
      if (weight > 10)
        price = (9.5 * weight);
      break;
    case 'O':
      if (weight < 2)
        price = (11.5 * weight);
      if (weight > 2 && weight <= 6)
        price = (16.5 * weight);
      if (weight > 6 && weight <= 10)
        price = (21.5 * weight);
      if (weight > 10)
        price = (26.5 * weight);
      break;
    }

  // Limit final output to 2 decimal places
  cout << setprecision(2) << fixed;

  // Display the package info and shipping price
  cout << "The service you chose is " << type << endl;
  cout << "The weight of your package is " << weight << endl;
  cout << "The shipping charge for this package is " << price << endl;

  return 0;
}
Line 10. Asking for a char from user and then attempting to read a char will fail.
Last edited on
Ah, you are right. I didn't notice that. How would I correcting that in line 17? I never learned how to insert multiple char variables into a single 'cin' line. Would it be this?

cin >> 'R' >> 'P' >> 'O' >> weight;
But ... your switch statement implies that you want only one char from the user.
Correct, there are 3 shipping options available and the user has to select 1 of them (one of the char variables). For this program, the shipment methods have to be char variables. Based on the shipment method and weight selected, the program is supposed to compute the shipping cost.
For this program, the shipment methods have to be char variables.

Then... why are you using a variable of type int?
Ok, I tried making some changes but now it won't even compile. I'm honestly not sure how to properly declare the 3 char variables and have the program select from them.

# include <iostream>
# include <iomanip>
using namespace std;
int main ()
{
// Declare variables for shipment type
char R, P, O;

// Assign variable to delivery type
// int type;

// Assign variable to package weight and price
float weight, price;

// Ask user to specify shipping service and weight of package
cout << "Specify service type (R/P/0) and package weight (XX.X)" << endl;
cin >> char >> weight;

// Assign cost to package
switch (char)
{
case 'R':
if (weight < 2)
price = (1 * weight);
if (weight > 2 && weight <= 6)
price = (1.5 * weight);
if (weight > 6 && weight <= 10)
price = (2 * weight);
if (weight > 10)
price = (2.5 * weight);
break;
case 'P':
if (weight < 2)
price = (3.5 * weight);
if (weight > 2 && weight <= 6)
price = (5.5 * weight);
if (weight > 6 && weight <= 10)
price = (7.5 * weight);
if (weight > 10)
price = (9.5 * weight);
break;
case 'O':
if (weight < 2)
price = (11.5 * weight);
if (weight > 2 && weight <= 6)
price = (16.5 * weight);
if (weight > 6 && weight <= 10)
price = (21.5 * weight);
if (weight > 10)
price = (26.5 * weight);
break;
}

// Limit final output to 2 decimal places
cout << setprecision(2) << fixed;

// Display the package info and shipping price
cout << "The service you chose is " << char << endl;
cout << "The weight of your package is " << weight << endl;
cout << "The shipping charge for this package is " << price << endl;

return 0;
}
Ok, I tried making some changes but now it won't even compile. I'm honestly not sure how to properly declare the 3 char variables and have the program select from them.

You don't need 3 variables.

When you need an int or float value, you don't need a variable for each possible value that you deem acceptable input, do you? You need one variable to hold the input, but that variable must be the correct type.
For this program, the 3 shipment options are regular, priority, and overnight. The user must enter P, R, or O to select the shipping option. It can't be a full word, just a single letter. How would I modify my program to get it to run correctly? I tried changing the variable type and now it won't even compile (as opposed to before when it compiled but didn't calculate).
I tried changing the variable type and now it won't even compile

Yes. You made three entirely new variables, then failed to use any of them. When someone suggests changing a type, perhaps you should try just changing the type. ;)

In the OP, change line 10 to char type; and don't change anything else.
I got it now. It's compiling and calculating. Thank you :)
Topic archived. No new replies allowed.