help fixing my code

im almost done with my code, just need to fix a few things. the prompt is to calculate shipping charges of a package based on the distance it will be shipped and its weight. this is the data table:

Weight of Package (in kilograms) Rate per 500 Miles Shipped
2 kg or less $3.10
Over 2 kg but not more than 6 kg $4.20
Over 6 kg but not more than 10 kg $5.30
over 10 kg $6.40

here is my code:

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

void calculateCharge();

int main()
{
  double weightOfPackage, distance, milesCharge;



 cout << "                    Shipping Charges                   \n" << endl;

 do 
 {
 cout << "Enter the weight of the package (in kg): ";
 cin >> weightOfPackage;
 if (weightOfPackage == 0)
	{
		cout << "Program ended." << endl;
    break;
	}

 cout << setprecision(2) << fixed;
 
 cout << "Enter the distance the package will be shipped (in miles): " ;
 cin >> distance;
 calculateCharge();

 }
 while (weightOfPackage !=0);

 void calculateCharge ()
 {
   if(weightOfPackage <= 2)
    milesCharge = (distance/500) * 3.10;
 if(weightOfPackage > 2 && weightOfPackage <= 6)
    milesCharge = (distance/500) * 4.20;
 if(weightOfPackage > 6 && weightOfPackage <= 10)
    milesCharge = (distance/500) * 5.30;
 if(weightOfPackage > 10 )
   milesCharge = (distance/500) * 6.40;
 }
 
 cout << "\nTotal shipping charges: $" << milesCharge << "\n\n";
}
Last edited on
I know there's an error on line 35 about the bracket, but I don't know why it is wrong. Also the main thing I need to fix is having that void function included in my code. thank you all in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>
    
double charge_for_package(double weight, double distance)
{
  if (weight <= 2)       return (distance / 500) * 3.10;
  else if (weight <= 6)  return (distance / 500) * 4.20;
  else if (weight <= 10) return (distance / 500) * 5.30;
  else                   return (distance / 500) * 6.40;
}

int main()
{ 
  double totalCharge = 0.0;

  for (double weight, distance; (std::cin >> weight >> distance) && weight != 0; )       
    totalCharge += charge_for_package(weight, distance);

  std::cout << std::setprecision(2) << std::fixed << totalCharge << '\n';
}
Last edited on
This is your code indented properly.
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
#include <iostream>
#include <iomanip>
using namespace std;

void calculateCharge();

int main()
{
  double weightOfPackage, distance, milesCharge;

  cout << "                    Shipping Charges                   \n" << endl;

  do {
    cout << "Enter the weight of the package (in kg): ";
    cin >> weightOfPackage;
    if (weightOfPackage == 0) {
      cout << "Program ended." << endl;
      break;
    }

    cout << setprecision(2) << fixed;
    cout << "Enter the distance the package will be shipped (in miles): ";
    cin >> distance;
    calculateCharge();
  }
  while (weightOfPackage != 0);

  void calculateCharge() {
    if (weightOfPackage <= 2)
      milesCharge = (distance / 500) * 3.10;
    if (weightOfPackage > 2 && weightOfPackage <= 6)
      milesCharge = (distance / 500) * 4.20;
    if (weightOfPackage > 6 && weightOfPackage <= 10)
      milesCharge = (distance / 500) * 5.30;
    if (weightOfPackage > 10)
      milesCharge = (distance / 500) * 6.40;
  }

  cout << "\nTotal shipping charges: $" << milesCharge << "\n\n";
}

C++ doesn't have nested functions, which immediately grant you access to the local variables inside the parent function.

1. Move calculateCharge outside main
2. Give it some parameters.

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

void calculateCharge(double &weightOfPackage, double &distance, double &milesCharge);

int main()
{
  double weightOfPackage, distance, milesCharge;

  cout << "                    Shipping Charges                   \n" << endl;

  do {
    cout << "Enter the weight of the package (in kg): ";
    cin >> weightOfPackage;
    if (weightOfPackage == 0) {
      cout << "Program ended." << endl;
      break;
    }

    cout << setprecision(2) << fixed;
    cout << "Enter the distance the package will be shipped (in miles): ";
    cin >> distance;
    calculateCharge(weightOfPackage, distance, milesCharge);
  }
  while (weightOfPackage != 0);
  cout << "\nTotal shipping charges: $" << milesCharge << "\n\n";
}

void calculateCharge(double &weightOfPackage, double &distance, double &milesCharge) {
  if (weightOfPackage <= 2)
    milesCharge = (distance / 500) * 3.10;
  if (weightOfPackage > 2 && weightOfPackage <= 6)
    milesCharge = (distance / 500) * 4.20;
  if (weightOfPackage > 6 && weightOfPackage <= 10)
    milesCharge = (distance / 500) * 5.30;
  if (weightOfPackage > 10)
    milesCharge = (distance / 500) * 6.40;
}

thank you. when I try to run the code, it doesn't calculate the cost as it should when the void calculate charge function is called. idk how to fix that.
edit: I moved the cout on line 27, fixed it. thank you for the help!
Last edited on
Topic archived. No new replies allowed.