Is this code salvageable or not.

I was doing a example program and it states that:

A courier company charges $1.50 per kg, with a maximum charge
of $15.00. Write a program which allows the operator to enter the
weight of a parcel in kg, and displays the cost of delivery.


Well I was trying to remember (build the top then add down the ladder) I was trying to build a company with its own system that is private, but that wasn't what the example was stating, but I went with it anyways so I think copying memory that's private to a condition acceptable right but not good through parameters set with constraints, I think it's dangerous. I think I should have passed the value through, now I have unused objects and a logical statement with a inputs wow messy, right now I wonder if this codes salvageable if not back to the drawing board.

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

    //Company.header
class Company{
    private: double *charges, *weight, *output;
	public:
    void corrier
( double &max_charges, double &current_weight, double &output, double min_charges, double default_weight);
	  

	};
		//Corrier.cpp
	void Company::corrier
(double &max_charges,double &current_weight, double &output, double min_charges, double default_weight)
	  
	{   
        weight = 0;
		weight = new double;
	   *weight = current_weight;
	    current_weight = 1000;
	   
	    weight = 0;
		weight = new double;
	   *weight = default_weight;
	    default_weight = 1;
	   
	    charges = 0;
	    charges = new double;
	   *charges = max_charges;
	    max_charges = 15.00;
	   
	   	charges = 0;
	    charges = new double;
	   *charges = min_charges;
	    min_charges = 1.50;
	   
	    
        printf(" Please enter weight in kg: ");
        string input;
	    getline(cin, input);
	   	current_weight = stod(input);
	    if(max_charges >= current_weight){
	    output = max_charges + current_weight;
	    printf("The weight of the parcel is %f\n ",output);
	    cout << "The weight of the parcel is  " <<
	    output << current_weight << endl;
	    }else if(max_charges<current_weight)
	    {
	    output =  min_charges * default_weight;
	    printf("The weight of the parcel is %f\n ",output);
	    cout << "The weight of the parcel is**  " <<
	    output << endl;
	    }
  
	   };
//main.cpp
int main(){
	Company Employee; 

    double max_weight;
	double current_charges;
    double output;
    double min_charges;
    double default_weight;
    
    Employee.corrier(max_weight, current_charges, output,min_charges, default_weight);
 
     return 0;
	}

Last edited on
in the grand scheme of things, its a very small amount of code. If you can't fix it quickly or feel you made a big design mess, start over.
A simple way:
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
/*
Write a program which allows the operator to enter the
weight of a parcel in kg, and displays the cost of delivery.
*/

#include <iostream>
#include <iomanip>

using namespace std;

const double MAX_POSTAGE = 15.0;
const double POSTAGE_PER_KG = 1.5;

int main()
{
  cout << "Enter weight in kg ";
  double weight;
  cin >> weight;
  double delivery = weight * POSTAGE_PER_KG;
  if (delivery > MAX_POSTAGE)
    delivery = MAX_POSTAGE;

  cout << "The delivery charge is $" << fixed 
       << setprecision(2) << delivery  << "\n";
}
Topic archived. No new replies allowed.