Arrays Homework assistance

Hello again all.

I've been working on this program for a couple of weeks but I still cannot get to work like the homework requires.

The assignment is as follows:
Lone Star Package Service ships packages within the state of Texas. Packages are accepted for shipping subject to the following restrictions:

The package weight must not exceed 50 pounds.
The package must not exceed 3 feet in length, width, or height.
The girth of the package must not exceed 5 feet. The girth is the circumference around the two smallest sides of the package. If side1, side2, and side3 are the lengths of the three sides, and largest is the largest of the three sides, then the girth can be calculated using:
girth = 2 * ( side1 + side2 + side3 - largest )
For each transaction (package to be shipped), the user should enter the package weight followed by the 3 package dimensions in any order. The weight should be specified as a whole number of pounds and the dimensions are specified as a whole number of inches.

Input Validation

Check to be sure that the user enters positive numbers for the package weight and dimensions. For transactions with invalid weight or dimensions, print an error message and skip the transaction.

The shipping charge is based on the following table. This table may be represented in your program as parallel arrays (two one-dimensional arrays), one for the weight and one for the shipping charge. Alternatively, you may define a struct or simple class to represent one table entry (one weight and cost). Then the table would be an array of structs or an array of objects.

Note: Do not use a two-dimensional array to store teh weights and costs. The weights need to be stored as integers, and the costs need to be stored as floating-point values. So they cannot be mixed in one array.

(This is where a table detailing the array values would be, I can't get it to paste correctly, the program has it correct.)

To determine the shipping charge, search the weight array for the package weight and then use the corresponding element from the shipping charge array. For example, the shipping charge for a 3 pound package would be $4.00. If the package weight falls between the weights in the weight table, use the larger weight. For example, the shipping charge for a 4 pound package would be 6.75. Do not hard code these values into your program code. For example, you should NOT have code like:

1
2
if ( packageWeight == 4 || packageWeight == 5)
   shippingCost = 6.75;


Output

Write a Shipping Log to an output file as you process the packages (see example below). Print column headings for the Shipping Log before you start the transaction processing loop (see example below). Then print the information for each package as the package is processed.

Note: Do not store the package information in an array or vector. If you process a lot of packages, you will eventually run out of space in the array. If you use a vector, as you process more transactions you will eat up more and more memory. This program should be designed to run for long periods of time without running out of memory.

For each transaction, print:

the transaction number (start counting with 1)
whether the package was accepted or rejected
the package weight
the cost for shipping (if applicable)
When the program ends, print the number of packages accepted for shipping and the number of packages rejected to the end of the Shipping Log. Transactions that contain invalid input should not be counted.


The screen dialog should look similar to this

For each transaction, enter package weight and 3 dimensions.
Enter -1 to quit.

Enter package weight and 3 dimensions: 1 2 3 3
Enter package weight and 3 dimensions: 7 4 2 3
Enter package weight and 3 dimensions: 21 12 15 12
Enter package weight and 3 dimensions: 45 12 20 2
Enter package weight and 3 dimensions: 49 24 40 20
Enter package weight and 3 dimensions: 25 35 30 20
Enter package weight and 3 dimensions: 68 10 20 10
Enter package weight and 3 dimensions: 50 0 10 10
Error - package weight and dimensions must be larger than 0
Please re-enter transaction
Enter package weight and 3 dimensions: 50 10 10 10
Enter package weight and 3 dimensions: 45 20 20 20
Enter package weight and 3 dimensions: -1



The Shipping Log should look similar to this:

Trans Accept/Reject Weight Cost
1 Accepted 1 1.50
2 Accepted 7 9.90
3 Accepted 21 31.90
4 Accepted 45 47.40
5 Rejected 49 -
6 Rejected 25 -
7 Rejected 68 -
8 Accepted 50 55.20
9 Rejected 45 -

Number of accepted packages: 5
Number of rejected packages: 4


Additional Requirements:

Do not use global variables in any assignment. A global variable is a variable that is declared outside any function. It is okay to use global constants.
You must use multiple functions in this program. Do not write the program as one large main function.
Do not read and store the transaction information in arrays. When you store transactions in arrays, you set a limit on the number of transactions your program can process. For this program, you should be able to read and process transactions one at a time.

What I have will be in the next post
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;

	//struct for info in file
	struct PackageInfo
	{
		int trans;					//number of transactions
		string status;				// package status
		double pkgWeight;			//the package weight
		double pkgCost;				//package shipping cost in file
	};

	int getLargest(int, int, int);
	//string pkgStatus (int, int);

	const int SIZE = 15;
	int main ()
	{
		PackageInfo package;

		ofstream outputFile;
		outputFile.open("/Users/Aengus/Documents/school/ACC/Programing/C++/chapter 8/packages.txt");
		if (!outputFile)
		   {
		      cout << "Error opening output file. Program terminated."
		           << endl;
		      exit(1);
		   }

		//declare arrays
		int weight[SIZE] = {1, 2, 3, 5, 7, 10, 13, 16, 20, 25, 30, 35, 40, 45, 50};
		double shipping[SIZE] = {1.50, 2.10, 4.00, 6.75, 9.90, 14.95, 19.40, 24.40, 27.30, 31.90, 38.50, 43.50, 44.80, 47.40, 55.20};

		//declare variables
		int transNum = 0;				//number of transactions
		int length = 0;						//the package length
		int width = 0;						//the package width
		int height = 0;						//the package height
		int girth = 0;						//the package girth
		int largest = 0;					//the package's largest side
		int index = 0;					//holder variable for the index location of weight compared to shipping charge
		int numGood = 0;				//number of accepted packages
		int numBad = 0;					//number of rejected packages
		string status = " ";			//returned package status

		cout <<"Enter package weight in pounds and 3 dimensions in inches: "; // get input
		cin >> package.pkgWeight >> length >> width >> height;

		if ((length <= 0)||(width <= 0)||(height <= 0)||(package.pkgWeight <= 0))
		{
			cout << "Error: package dimensions must be greater than 0" << endl;
			cout << "Please re-enter your transaction." << endl;
			cin >> package.pkgWeight >> length >> width >> height;
		}

		while (package.pkgWeight > 0)
		{
			//search the arrays for memory location
			for (int i = 0; i < 15; i++)
			{
				if (package.pkgWeight == weight[i])
				{
					index = i;
					break;
				}
				else if (package.pkgWeight >= weight[i])
					continue;
				else
				{
					package.pkgWeight = weight[i];
					index = i;
					break;
				}
			}//end for

			//get the largest number
			largest = getLargest(length, width, height);

			//calculate the girth
			girth = 2 * (length + width + height - largest);

			//remove these before finalizing
			cout << largest << " ";
			cout << girth << " ";
			cout << package.pkgWeight << " ";
			cout << shipping[index] << endl;
			/////////////////////////////////

			package.pkgCost = shipping[index];

			transNum++;
			package.trans = transNum;
			outputFile << package.trans;

			//determine package status
			if ((girth > 60)||(package.pkgWeight > 50))
			{
				package.status = "Rejected";
				outputFile << package.status;
				numBad++;
			}
			else {
				package.status = "Accepted";
				outputFile << package.status;
				numGood++;
			}//end if

			outputFile << package.pkgWeight;
			outputFile << package.pkgCost;

			cout <<"Enter package weight in pounds and 3 dimensions in inches: "; // get input
			cin >> package.pkgWeight;
			if (package.pkgWeight > 0)
			{
				cin >> length >> width >> height;
			}//end if
			else
				break;

		}//end while

		outputFile.close();

		ifstream inFile;
		inFile.open("packages.txt");
		if (!inFile)
		{
			cout << "Error opening input file. Program terminated."
			     << endl;
			     exit(1);
		}

		cout << "Trans " << setw(5) << " Accept/Reject " << setw(5) << " Weight " << setw(5) << " Cost" << endl;
		inFile >> package.trans >> package.status >> setw(5) >> package.pkgWeight >> setw(5) >> package.pkgCost;
		//cout << package.trans << setw(5) << package.status<< setw(5) << package.pkgWeight << setw(5) << package.pkgCost << endl;
		cout << package.trans << setw(10) << package.status<< setw(5) << package.pkgWeight << setw(5) << package.pkgCost << endl;
		cout << "Number of Accepted packages: " << numGood << endl;
		cout << "Number of Rejected packages: " << numBad << endl;

		inFile.close();

		return 0;
	}

	//This function gets that largest side fo th box for the girth calculation
	int getLargest(int side1, int side2, int side3)
	{
		int largest;
		if (side1 > 0 && side2 > 0 && side3 > 0)
		{
			if (side1 > side2 && side1 > side3)
				largest = side1;
			else if (side2 > side1 && side2 > side3)
				largest = side2;
			else if ( side3 > side1 && side3 > side2)
				largest = side3;
		}
		else
			cout << "Package weight and dimensions must be larger than 0" << endl; //error message if length is too big
		return largest;
	}


I cannot get the output to format itself correctly, nor do I know how to make the program ignore the '-1' value to end the while loop.

any help will be greatly appreciated
Last edited on
Topic archived. No new replies allowed.