Getting around using global variables

I am working on an assignment where the program calculates shipping costs based on data read in from a text file. I had it running with global variables but have been told by my instructor to not use global variables. Moving the variables, I have run into massive issues with scope and now it doesn't total at all.

Any suggestions on what I am not seeing would be great.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream> 
#include <iomanip> 
#include <cmath> 
#include <fstream> 
#include "myheader.h"

using namespace std;  

  
void header(int lab_number, char lab_part)
{ 
	cout << "Kevin Schultz\n"; 
	cout << "Lab" << lab_number << lab_part << endl << endl; 
} 
  


int main () 
{ 
	double calculate(double length, double width, double height);
	double volume;
	double chargeAmount;
	double charge(ifstream & inFile, ofstream & prt);
	double length, width, height, weight, total = 0, shipping_cost, volume;

	ifstream inFile; 
	ofstream prt("lab7new_out.txt"); 
  
	header(7, 'A'); 
	prt << "        S & S Global Services\n";
	prt << "    Shipping Cost Analysis Report\n\n"; 
	prt << "Length Width Height Weight Shipping\n"; 
	prt << "                             Cost\n\n"; 
  
	inFile.open("c:\\lab7\\pkg.txt"); 
		if (!inFile) 
		cout << "Error opening the file\n"; 
  
	inFile >> length; 
	inFile >> width; 
	inFile >> height; 
	inFile >> weight; 
	  
	volume = calculate(length, width, height);
	chargeAmount = charge(inFile, prt, shipping_cost, volume, length, weight, width, height, total); //Getting the error here telling me that there are too many arguments in function. I have squiggly lines under shipping_cost?
  
	prt << "------------------------------------" << endl;
	prt << "\nTotal cost: $" << total; 
	
	system("pause"); 
	return 0;
   
} 

double calculate(double length, double width, double height)
		{ 
			double volume; 
			volume = length * width * height; 
			return volume; 
		} 

double charge(ifstream & inFile, ofstream & prt, double shipping_cost, double volume, double length, double weight, double width, double height, double total) 
		{
			const double basic_charge = 12;
			const double Vsurcharge = 5;
			const double Dsurcharge = 4;
			const double Wsurcharge = 2;
			double netWeight = 0;
			 
			while (!inFile.eof())  
			{ 
				shipping_cost = basic_charge; 
		 
				if (volume > 7)  
					shipping_cost += Vsurcharge; 
				if (length > 3 || width > 3 || height > 3) 
					shipping_cost += Dsurcharge; 
				if (weight > 50) 
				{
					netWeight = weight - 50;
					shipping_cost += netWeight * Wsurcharge; 
				}
				total += shipping_cost; 
				prt << setw(4) << right << setprecision(1) << length << setw(6) << right << setprecision(1) << width << setw(6) << right << setprecision(1) << height << setw(8) << right; 
				prt << weight << setw(5) << right << setprecision(2) << fixed << "$" << shipping_cost << endl; 
		  
				inFile >> length; 
				inFile >> width; 
				inFile >> height; 
				inFile >> weight; 
			}
			        return total;
		}
Last edited on
What do these definitions

double length, width, height, weight;
...
double volume = 0, shipping_cost;



do after function main()?

They are global as before only are not visible in main().:)
Last edited on
Please indent your code.
If you intend to avoid global variables, then avoid them. (remove their declaration)
Pass as reference parameters into functions, or

Define class types that hold data and have methods that update that data.
Changed the code, now the only error is too many arguments in function. I have shown it above.
Do not declare functions inside main().

Your function declaration does not match your function definition.
You declared the function as having two parameters

double charge(ifstream & inFile, ofstream & prt);

but defined it as having nine parameters.

double charge(ifstream & inFile, ofstream & prt, double shipping_cost, double volume, double length, double weight, double width, double height, double total)
Topic archived. No new replies allowed.