Value return function trouble

I have an assignment here for a value returning function that uses another function to obtain the values, if any one could help me this is the description:You want to write a program that will generate accurate shipping charges for a local grocery store. The shipping charges for the store are as follows:
Weight of Package (in pounds) Rate per mile Shipped
5 lbs or less $0.20
Over 5 lbs but no more than 10 lbs $0.30
Over 10 lbs but not more than 20 lbs $0.45
Over 20 lbs $0.50
Write a function in a program that asks for the weight of a package in pouds and the distance it is to be shipped. Using that data, write another function that calculates the shipping charge and returns it to the main program and displays the value inside the main program.

For the second function, pass the two values (weight and distance) into the function as arguments. In the function, if the shipping distance the user provided earlier is a distance greater than 20 miles, there will be an additional charge of $25 added to the order. For example, if an item weighing 21 pound is shipped 25 miles, the total cost will be $37.50, calculated by the formula: ((25 miles * 0.50) + 25).
input validation: Do not accept any weights less than 1 pound, nor any distances less than 0 miles.

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
#include <iostream>
using namespace std;


float rate;

void input();
float calculation (float weight, float distance);

int main()
{
    float total;
    total = calculation();
    cout << "Your total cost is " << total;
}

void input ()
{
    cout << "How much does your shipment weigh in pounds? ";
    cin >> weight;
    cout << "What is the distance of the shipment in miles? ";
    cin >> distance;
    while (weight < 1 || distance < 0)
    {
        cout << "invalid input please try again\n";
        cout << "How much does your shipment weigh in pounds? ";
        cin >> weight;
        cout << "What is the distance of the shipment in miles? ";
        cin >> distance;
    }

    float calculation (weight, distance)
    {
        float totalCost;
        if (weight <= 5)
            rate = 0.20;
        else if (weight > 5 && weight <= 10)
            rate = 0.30;
        else if (weight > 10 && weight <= 20)
            rate = 0.45;
        else if (weight > 20)
            rate = 0.50;
        totalCost = weight * rate;
        if (distance > 20)
            totalCost += 25;
        return totalCost;
    }
}
Last edited on
C++ doesn't allow functions to be declared inside other functions - line 32

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
#include <iostream>
using namespace std;


// there's no need for globals
// float rate;

// Use reference parameters as a way to get answers back
void input(float &weight, float &distance);

float calculation (float weight, float distance);

int main()
{
    float weight, distance;
    input(weight, distance);
    float total;
    total = calculation(weight, distance);
    cout << "Your total cost is " << total;
}

void input (float &weight, float &distance)
{
    cout << "How much does your shipment weigh in pounds? ";
    cin >> weight;
    cout << "What is the distance of the shipment in miles? ";
    cin >> distance;
    while (weight < 1 || distance < 0)
    {
        cout << "invalid input please try again\n";
        cout << "How much does your shipment weigh in pounds? ";
        cin >> weight;
        cout << "What is the distance of the shipment in miles? ";
        cin >> distance;
    }
}

float calculation (float weight, float distance) // you need the types here as well.
{
      float totalCost, rate = 0.0;
      if (weight <= 5)
          rate = 0.20;
      else if (weight > 5 && weight <= 10)
          rate = 0.30;
      else if (weight > 10 && weight <= 20)
          rate = 0.45;
      else if (weight > 20)
          rate = 0.50;
      totalCost = weight * rate;
      if (distance > 20)
          totalCost += 25;
      return totalCost;
}

salem c wrote:
C++ doesn't allow functions to be declared inside other functions


To be precise: C++ doesn't allow functions to be defined inside other functions - which is what's being attempted at line 32.

Declaring a function inside another function is legal, if not very commonly done; if it weren't, then the Most Vexing Parse would be less of a problem.
Last edited on
Topic archived. No new replies allowed.