Project Question-Code and Error Check

Hello! For a project we are to rewrite a code example from our book and apply the new things we learned. The question we are doing is: This programming example demonstrates a program that calculates a customer’s bill for a local cable company. There are two types of customers: residential and business. There are two rates for calculating a cable bill: one for residential customers and one for business customers. For residential customers the following rates apply:

Bill Processing Fee: $4.50
Basic Service Fee: $20.50
Premium channels: $7.50 per channels

For business customers the following rates apply:

Bill processing fee: $15.00
Basic Service Fee: $75.00 for first 10 channels, $5.00 for each additional connection
Premium channels: $50.00 per channel for any number of connections

The program should ask the user for a account number and a customer code. Assume that ‘R’ or ‘r’ stands for residential and ‘B’ or ‘b’ stands for Business.

This is what our teacher wants us to do:

Your input will come from a text file of at least 15 customers
Precision should be two decimal places
Calculate the running average for residential and business spending
Print all customer's bill to s single file and at the end you should have the average summary for each customer type
We have a choice to use arrays for this

Right now im not focusing much on the average but just getting it to the point it can read the the file and then output the file here it is so far:

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
  #include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

const int numberofcustomers = 20; //needs to be declared before hand and can be changed

//residential customers constants

const double resBillProc = 4.50;
const double resBasicServCost = 20.50;
const double resCostPremChan = 7.50;

//business customers constants

const double busBillProc = 15.00;
const double busBasicServCost = 75.00;
const double busBasicConnCost = 5.00;
const double busCostPremChan = 50.00;

void calcCustRes(int premiumChanNum[][3], char customerType[][2], string customerID[], int i);
void calcCustBus(int premiumChanNum[][3], char customerType[][2], int basicServNum[][4], string customerID, int i);

using namespace std;

int main()
{
	string customerID[numberofcustomers]; //store customer IDs
	char customerType[numberofcustomers][2]; //storing customer type
	int premiumChanNum[numberofcustomers][3];//store number of premiumChannels
	int basicServNum[numberofcustomers][4]; // store number of basic server connections
	char fileName[60];
	ifstream inFile;
	ofstream outFile;

	cout << "Enter in the input file name: ";
	cin >> fileName; //user inputs file name

	inFile.open(fileName); //opens input file

	if (!inFile)
	{
		cout << "Cannot open the input file." << endl; //checks the user to tell them they opened the wrong file
		return 1;
	}

	for (int i = 0; i < numberofcustomers; i++)
	{
		inFile >> customerID[i];
		inFile.get(); //removes space between customer ids and their info
		customerType[i][2] = inFile.get();
		inFile.get();
		premiumChanNum[i][3] = inFile.get();
		inFile.get();
		basicServNum[i][4] = inFile.get();
	}

	//now to compare said data

	int resNum = 0;
	int busNum = 0;


	for (int i = 0; i < numberofcustomers; i++)
	{
		if (customerType[i][2] == 'r' || 'R')
		{
			resNum++;
			calcCustRes(premiumChanNum, customerType, customerID, i);
		}
		else if (customerType[i][2] == 'b' || 'B')
		{
			busNum++;
			calcCustBus(premiumChanNum, customerType, customerID, basicServNum, i);
		}
	}

	inFile.close();
	outFile.close();

}

void calcCustRes(int premiumChanNum[][3], char customerType[][2], string customerID[], int i)
{
	ofstream outFile;
	char fileName[60];
	cout << "Enter in the output file name: ";
	cin >> fileName; //user inputs file name

	outFile.open(fileName); //opens input file

	if (!outFile)
	{
		cout << "Cannot open the output file." << endl; //checks the user to tell them they opened the wrong file
		return 1;
	}

	double amountDue = 0;

	outFile << "Customer ID: " << customerType[i][2] << customerID[i] << endl;

	outFile << "Number of premium channels: " << premiumChanNum[i][3] << endl;

	outFile << "Bill: $" << amountDue = resBillProc
		+ resBasicServCost
		+ (premiumChanNum[i][3]) * resCostPremChan
		<< endl << endl;

}

void calcCustBus(int premiumChanNum[][3], char customerType[][2], int basicServNum[][4], string customerID, int i)
{
	ofstream outFile;
	char fileName[60];
	cout << "Enter in the output file name: ";
	cin >> fileName; //user inputs file name

	outFile.open(fileName); //opens input file

	if (!outFile)
	{
		cout << "Cannot open the output file." << endl; //checks the user to tell them they opened the wrong file
		return 1;
	}
	
	double amountDue = 0;

	outFile << "Customer ID: " << customerType[i][2] << customerID[i] << endl;

	outFile << "Number of premium channels: " << premiumChanNum[i][3] << endl;

	outFile << "Number of basic connections" << basicServNum[i][4] << endl;

	if (basicServNum[i][4] <= 10)
	{
		outFile << "Bill: $" << amountDue = busBillProc
			+ busBasicServCost
			+ (premiumChanNum[i][3]) * busCostPremChan 
			<< endl << endl;
	}
	else
	{
		outFile << "Bill: $" << amountDue = busBillProc
			+ busBasicServCost
			+ (basicServNum[i][4] - 10) * busBasicConnCost
			+ (premiumChanNum[i][3]) * busCostPremChan
			<< endl << endl;
	}

}


I am getting errors at this part:

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; i < numberofcustomers; i++)
	{
		if (customerType[i][2] == 'r' || 'R')
		{
			resNum++;
			calcCustRes(premiumChanNum, customerType, customerID, i);
		}
		else if (customerType[i][2] == 'b' || 'B')
		{
			busNum++;
			calcCustBus(premiumChanNum, customerType, customerID, basicServNum, i);
		}
	}


and errors for: busBillProc, resBillProc

Any help on why I am getting errors here is appreciated.
Last edited on
Line 5: You're missing using namespace std. It's better practice to leave it out, but if you do so, every reference to the standard library library must be qualified by std::

line 79,84: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y'))

('y') always evaluates to 1 (true), therefore the if statement is always true.

Line 87: The order of your parameters is incorrect. According to your function prototype, basicServNum precedes customerID.

Line 108: calcCustRes() is declared type void, but you're trying to return a value (1).

Line 117-120: Break into two statements, or use () to isolate the calculation.
1
2
3
4
    amountDue = resBillProc
		+ resBasicServCost
		+ (premiumChanNum[i][3]) * resCostPremChan;
    outFile << "Bill: $" << amount_due << endl << endl;


Line 136: calcCustBus() is declared type void, but at line you're trying to return a value.

Line 23, 87, 124: You're inconsistent in your use of customerID. The function prototype and the function say they accept a simple string. But when you call calcCustBus(), you pass an array.

That's it for syntax errors. You have a number of logical errors also.

Line 48: You're assuming there are exactly 20 customers in the file. Your assignment did not specify how many there would be, only that there would be at least 15. You should use a while loop, continuing until a read fails.

1
2
3
4
while (inFile >> customerID[i])
{  // Got a good customerID
...
}


line 51,53,55: infile.get() is unnecessary to remove whitespace if you use the >> operator.
 
    inFile >> customerType[i];   


Line 52,54,56: These are out of bounds references. customerType is declared as char[][2], meaning the elements are [0]-[1].

Line 81,86: calcCustRes() and calcCustBus() should really be designed to calculate a single customer rather than passing them the all the parallel arrays.

Have you covered structs yet? They are much clearer than parallel arrays.
1
2
3
4
5
6
7
struct customer
{  string customerID; 
    char customerType; 
    int premiumChanNum;
    int basicServNum; 
};
customer customers[MAX_CUSTOMERS];


Line 91: You close the outfile, but never opened it or used it in main().

Lines 100, 128: You prompt for output file name every time you call calcCustRes or calcCustBus. That's going to be very repetitive entering that for each customer in the input file.







Ok thankyou! Working editing the code now and no we did not go over structs, i see that is in the later chapters.
I am assuming for the lines 79, and 84 the correct way to do this would be [
 
customerType[i][2] == 'r' || customerType[i][2] == 'R'


and so on
Ok so im dealing with this piece by piece, i replaced the outFile with cout for now just so I can deal with the other stuff first right now i still getting an error at:

 
void calcCustBus(int premiumChanNum[][3], char customerType[][2], int basicServNum[][4], string customerID[], int i);


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

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int numberofcustomers = 20; //needs to be declared before hand and can be changed

								  //residential customers constants

const double resBillProc = 4.50;
const double resBasicServCost = 20.50;
const double resCostPremChan = 7.50;

//business customers constants

const double busBillProc = 15.00;
const double busBasicServCost = 75.00;
const double busBasicConnCost = 5.00;
const double busCostPremChan = 50.00;

void calcCustRes(int premiumChanNum[][3], char customerType[][2], string customerID[], int i);
void calcCustBus(int premiumChanNum[][3], char customerType[][2], int basicServNum[][4], string customerID[], int i);

int main()
{
	string customerID[numberofcustomers]; //store customer IDs
	char customerType[numberofcustomers][2]; //storing customer type
	int premiumChanNum[numberofcustomers][3];//store number of premiumChannels
	int basicServNum[numberofcustomers][4]; // store number of basic server connections
	char fileName[60];
	ifstream inFile;

	cout << "Enter in the input file name: ";
	cin >> fileName; //user inputs file name

	inFile.open(fileName); //opens input file

	if (!inFile)
	{
		cout << "Cannot open the input file." << endl; //checks the user to tell them they opened the wrong file
		return 0;
	}

	for (int i = 0; i < numberofcustomers; i++)
	{
		inFile >> customerID[i];
		inFile >> customerType[i][2];
		inFile >> premiumChanNum[i][3];
		inFile >> basicServNum[i][4];
	}

	//now to compare said data

	int resNum = 0;
	int busNum = 0;


	for (int i = 0; i < numberofcustomers; i++)
	{
		if (customerType[i][2] == 'r' || customerType[i][2] == 'R')
		{
			resNum++;
			calcCustRes(premiumChanNum, customerType, customerID, i);
		}
		else if (customerType[i][2] == 'b' || customerType[i][2] == 'B')
		{
			busNum++;
			calcCustBus(premiumChanNum, customerType, basicServNum, customerID, i);
		}
	}

	inFile.close();

}

void calcCustRes(int premiumChanNum[][3], char customerType[][2], string customerID[], int i)
{

	double amountDue = 0;

	cout << "Customer ID: " << customerType[i][2] << customerID[i] << endl;

	cout << "Number of premium channels: " << premiumChanNum[i][3] << endl;

	amountDue = resBillProc
		+ resBasicServCost
		+ (premiumChanNum[i][3]) * resCostPremChan;
	cout << "Bill: $" << amountDue << endl << endl;

}

void calcCustBus(int premiumChanNum[][3], char customerType[][2], int basicServNum[][4], string customerID, int i)
{

	double amountDue = 0;

	cout << "Customer ID: " << customerType[i][2] << customerID[i] << endl;

	cout << "Number of premium channels: " << premiumChanNum[i][3] << endl;

	cout << "Number of basic connections" << basicServNum[i][4] << endl;

	if (basicServNum[i][4] <= 10)
	{
		amountDue = busBillProc
			+ busBasicServCost
			+ (premiumChanNum[i][3]) * busCostPremChan;

		cout << "Bill: $" << amountDue << endl << endl;
	}
	else
	{
		amountDue = busBillProc
			+ busBasicServCost
			+ (basicServNum[i][4] - 10) * busBasicConnCost
			+ (premiumChanNum[i][3]) * busCostPremChan;
			
		cout << "Bill: $" << amountDue << endl << endl;
	}

}


Before I change them into while loops I just want to see if my arrays and user defined functions can execute without error. Question however for 'char' array to find the custom ID how would I right that so its reading down the column? since the input data would look like something around these lines:

1
2
3
4
12345 R 9 0
13456 R 12 0
17258 B 3 10
91863 B 50 11
Actually, what if instead it is reading the data like

1
2
3
4
5

R12345  9 0
R13456  12 0
B17258  3 10
B91863  50 11


would that make it possible for the code to be simpler? If so how would I read and store said info? I assuming it would read those customer ID's and then based on the first character it would check if its 'R'/'r' or 'B'/'b' and store the rest of the data as a string or char
Line 25,95: The signature of your function prototype and your function do not agree, In the function prototype, customerID is an array. In the function definition, customerID is a simple string. The two must agree.

lines 50,51,52,63,68,84,90,100,106,118,119: You're still doing out of bounds references.

I think i got it!

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

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int numberofcustomers = 20; //needs to be declared before hand and can be changed

								  //residential customers constants

const double resBillProc = 4.50;
const double resBasicServCost = 20.50;
const double resCostPremChan = 7.50;

//business customers constants

const double busBillProc = 15.00;
const double busBasicServCost = 75.00;
const double busBasicConnCost = 5.00;
const double busCostPremChan = 50.00;

void calcCustRes(int premiumChanNum[][2], char customerType[][1], string customerID[], int i);
void calcCustBus(int premiumChanNum[][2], char customerType[][1], int basicServNum[][3], string customerID[], int i);

int main()
{
	string customerID[numberofcustomers]; //store customer IDs
	char customerType[numberofcustomers][1]; //storing customer type
	int premiumChanNum[numberofcustomers][2];//store number of premiumChannels
	int basicServNum[numberofcustomers][3]; // store number of basic server connections
	char fileName[60];
	ifstream inFile;

	cout << "Enter in the input file name: ";
	cin >> fileName; //user inputs file name

	inFile.open(fileName); //opens input file

	if (!inFile)
	{
		cout << "Cannot open the input file." << endl; //checks the user to tell them they opened the wrong file
		return 0;
	}

	for (int i = 0; i < numberofcustomers; i++)
	{
		inFile >> customerID[i];
		inFile >> customerType[i][1];
		inFile >> premiumChanNum[i][2];
		inFile >> basicServNum[i][3];
	}

	//now to compare said data

	int resNum = 0;
	int busNum = 0;


	for (int i = 0; i < numberofcustomers; i++)
	{
		if (customerType[i][1] == 'r' || customerType[i][1] == 'R')
		{
			resNum++;
			calcCustRes(premiumChanNum, customerType, customerID, i);
		}
		else if (customerType[i][1] == 'b' || customerType[i][1] == 'B')
		{
			busNum++;
			calcCustBus(premiumChanNum, customerType, basicServNum, customerID, i);
		}
	}

	inFile.close();

}

void calcCustRes(int premiumChanNum[][2], char customerType[][1], string customerID[], int i)
{

	double amountDue = 0;

	cout << "Customer ID: " << customerType[i][1] << customerID[i] << endl;

	cout << "Number of premium channels: " << premiumChanNum[i][2] << endl;

	amountDue = resBillProc
		+ resBasicServCost
		+ (premiumChanNum[i][2]) * resCostPremChan;
	cout << "Bill: $" << amountDue << endl << endl;

}

void calcCustBus(int premiumChanNum[][2], char customerType[][1], int basicServNum[][3], string customerID[], int i)
{

	double amountDue = 0;

	cout << "Customer ID: " << customerType[i][1] << customerID[i] << endl;

	cout << "Number of premium channels: " << premiumChanNum[i][2] << endl;

	cout << "Number of basic connections: " << basicServNum[i][3] << endl;

	if (basicServNum[i][3] <= 10)
	{
		amountDue = busBillProc
			+ busBasicServCost
			+ (premiumChanNum[i][2]) * busCostPremChan;

		cout << "Bill: $" << amountDue << endl << endl;
	}
	else
	{
		amountDue = busBillProc
			+ busBasicServCost
			+ (basicServNum[i][3] - 10) * busBasicConnCost
			+ (premiumChanNum[i][2]) * busCostPremChan;
			
		cout << "Bill: $" << amountDue << endl << endl;
	}

}


alright so to output to file would it be something along these lines?

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int numberofcustomers = 20; //needs to be declared before hand and can be changed

								  //residential customers constants

const double resBillProc = 4.50;
const double resBasicServCost = 20.50;
const double resCostPremChan = 7.50;

//business customers constants

const double busBillProc = 15.00;
const double busBasicServCost = 75.00;
const double busBasicConnCost = 5.00;
const double busCostPremChan = 50.00;

void calcCustRes(int premiumChanNum[][2], char customerType[][1], string customerID[], int i, ofstream outFile);
void calcCustBus(int premiumChanNum[][2], char customerType[][1], int basicServNum[][3], string customerID[], int i, ofstream outFile);


int main()
{
	string customerID[numberofcustomers]; //store customer IDs
	char customerType[numberofcustomers][1]; //storing customer type
	int premiumChanNum[numberofcustomers][2];//store number of premiumChannels
	int basicServNum[numberofcustomers][3]; // store number of basic server connections
	char fileName[60];
	ifstream inFile;
	ofstream outFile;

	cout << "Enter in the input file name: ";
	cin >> fileName; //user inputs file name

	inFile.open(fileName); //opens input file

	if (!inFile)
	{
		cout << "Cannot open the input file." << endl; //checks the user to tell them they opened the wrong file
		return 0;
	}

	cout << "Enter in the input file name: ";
	cin >> fileName; //user inputs file name

	outFile.open(fileName); //opens input file

	if (!outFile)
	{
		cout << "Cannot open the output file." << endl; //checks the user to tell them they opened the wrong file
		return 0;
	}

	for (int i = 0; i < numberofcustomers; i++)
	{
		inFile >> customerID[i];
		inFile >> customerType[i][1];
		inFile >> premiumChanNum[i][2];
		inFile >> basicServNum[i][3];
	}

	//now to compare said data



	for (int i = 0; i < numberofcustomers; i++)
	{
		if (customerType[i][1] == 'r' || customerType[i][1] == 'R')
		{
			calcCustRes(premiumChanNum, customerType, customerID, i, outFile);
		}
		else if (customerType[i][1] == 'b' || customerType[i][1] == 'B')
		{
			calcCustBus(premiumChanNum, customerType, basicServNum, customerID, i, outFile);
		}
	}

	inFile.close();

}

void calcCustRes(int premiumChanNum[][2], char customerType[][1], string customerID[], int i, ofstream outFile)
{

	double amountDue = 0;

	outFile << "Customer ID: " << customerType[i][1] << customerID[i] << endl;

	outFile << "Number of premium channels: " << premiumChanNum[i][2] << endl;

	amountDue = resBillProc
		+ resBasicServCost
		+ (premiumChanNum[i][2]) * resCostPremChan;
	outFile << "Bill: $" << amountDue << endl << endl;

}

void calcCustBus(int premiumChanNum[][2], char customerType[][1], int basicServNum[][3], string customerID[], int i, ofstream outFile)
{

	double amountDue = 0;

	outFile << "Customer ID: " << customerType[i][1] << customerID[i] << endl;

	outFile << "Number of premium channels: " << premiumChanNum[i][2] << endl;

	outFile << "Number of basic connections: " << basicServNum[i][3] << endl;

	if (basicServNum[i][3] <= 10)
	{
		amountDue = busBillProc
			+ busBasicServCost
			+ (premiumChanNum[i][2]) * busCostPremChan;

		outFile << "Bill: $" << amountDue << endl << endl;
	}
	else
	{
		amountDue = busBillProc
			+ busBasicServCost
			+ (basicServNum[i][3] - 10) * busBasicConnCost
			+ (premiumChanNum[i][2]) * busCostPremChan;
			
		outFile << "Bill: $" << amountDue << endl << endl;
	}

}


Im getting an error at the function calls trying to see what that is about.
Oh I also want to say thank you so much for taking your time out to help me. My teacher wasn't really allowing us to ask questions which was making this harder.
Alright last bit how would I go about calculating the average? would I need to add another thing the function? I know with the void do any return data so far this is what is looks like:

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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

const int numberofcustomers = 20; //needs to be declared before hand and can be changed

								  //residential customers constants

const double resBillProc = 4.50;
const double resBasicServCost = 20.50;
const double resCostPremChan = 7.50;

//business customers constants

const double busBillProc = 15.00;
const double busBasicServCost = 75.00;
const double busBasicConnCost = 5.00;
const double busCostPremChan = 50.00;

void calcCustRes(int premiumChanNum[][2], char customerType[][1], string customerID[], int i, ofstream& outFile, double resSum);
void calcCustBus(int premiumChanNum[][2], char customerType[][1], int basicServNum[][3], string customerID[], int i, ofstream& outFile, double busSum);


int main()
{
	string customerID[numberofcustomers]; //store customer IDs
	char customerType[numberofcustomers][1]; //storing customer type
	int premiumChanNum[numberofcustomers][2];//store number of premiumChannels
	int basicServNum[numberofcustomers][3]; // store number of basic server connections
	double averSpendRes;
	double averSpendBus;
	int resNum = 0;
	int busNum = 0;
	double resSum;
	double busSum;
	char fileName[60];
	ifstream inFile;
	ofstream outFile;
	

	cout << "Enter in the input file name: ";
	cin >> fileName; //user inputs file name

	inFile.open(fileName); //opens input file

	if (!inFile)
	{
		cout << "Cannot open the input file." << endl; //checks the user to tell them they opened the wrong file
		return 0;
	}

	cout << "Enter in the input file name: ";
	cin >> fileName; //user inputs file name

	outFile.open(fileName); //opens input file

	if (!outFile)
	{
		cout << "Cannot open the output file." << endl; //checks the user to tell them they opened the wrong file
		return 0;
	}

	for (int i = 0; i < numberofcustomers; i++)
	{
		inFile >> customerID[i];
		inFile >> customerType[i][1];
		inFile >> premiumChanNum[i][2];
		inFile >> basicServNum[i][3];
	}

	//now to compare said data



	for (int i = 0; i < numberofcustomers; i++)
	{
		if (customerType[i][1] == 'r' || customerType[i][1] == 'R')
		{
			resNum++;
			calcCustRes(premiumChanNum, customerType, customerID, i, outFile, resSum);
		}
		else if (customerType[i][1] == 'b' || customerType[i][1] == 'B')
		{
			busNum++;
			calcCustBus(premiumChanNum, customerType, basicServNum, customerID, i, outFile, busSum);
		}
	}

	averSpendBus = busSum / busNum;
	averSpendRes = busSum / busNum;

	outFile << "Average spending of residential: " << averSpendRes << endl;
	outFile << "Average spending of businesses: " << averSpendBus << endl;

	inFile.close();
	outFile.close();

}

void calcCustRes(int premiumChanNum[][2], char customerType[][1], string customerID[], int i, ofstream& outFile, double resSum)
{

	double amountDue = 0;

	outFile << "Customer ID: " << customerType[i][1] << customerID[i] << endl;

	outFile << "Number of premium channels: " << premiumChanNum[i][2] << endl;

	amountDue = resBillProc
		+ resBasicServCost
		+ (premiumChanNum[i][2]) * resCostPremChan;
	outFile << "Bill: $" << amountDue << endl << endl;

	resSum += amountDue;

}

void calcCustBus(int premiumChanNum[][2], char customerType[][1], int basicServNum[][3], string customerID[], int i, ofstream& outFile, double busSum)
{

	double amountDue = 0;

	outFile << "Customer ID: " << customerType[i][1] << customerID[i] << endl;

	outFile << "Number of premium channels: " << premiumChanNum[i][2] << endl;

	outFile << "Number of basic connections: " << basicServNum[i][3] << endl;

	if (basicServNum[i][3] <= 10)
	{
		amountDue = busBillProc
			+ busBasicServCost
			+ (premiumChanNum[i][2]) * busCostPremChan;

		outFile << "Bill: $" << amountDue << endl << endl;
	}
	else
	{
		amountDue = busBillProc
			+ busBasicServCost
			+ (basicServNum[i][3] - 10) * busBasicConnCost
			+ (premiumChanNum[i][2]) * busCostPremChan;
			
		outFile << "Bill: $" << amountDue << endl << endl;
	}

	busSum += amountDue;

}
	


which I know is wrong but hmm...
Last edited on
Ok yeah, im stuck on this, any help would be nice haha. Im wondering would have to rewrite my code as to store the amount due in another array, but I don't think that be necessary. I think i am missing somethin very simple but I can't put my tongue on it on how I would calculate the averages
line 69-71,80,85,108,110,114,126,128,130,136,144,145: You're still not getting it regarding out of bounds references. If you define an array with 2 elements, those elements are [0] and [1]. You can't reference [2]. That's out of bounds because that would be the third element of an array that only has 2 elements. Array references are 0 based.

Lines 30-32: I really don't understand why you're declaring 2D arrays here. You only need a 1D array.
30
31
32
	char customerType[numberofcustomers];  //storing customer type
	int premiumChanNum[numberofcustomers];  //store number of premiumChannels
	int basicServNum[numberofcustomers];  // store number of basic server connections 

I think this is why from my understanding was that with arrays when doing 2 dimensional is that say you say suchArray [9][4] which would result ins 9 rows and 4 columns. So if you are wanting to find data in a specific area. Like if I was to store or get all the numbers in column 2 i would have suchArray [i][1] (since arrays start at 0) and would be incrementing i every time to keep going down the column. This is how my book described it, and what it was like in the lectures am I wrong in this way of thinking? I was not aware, considering we were discouraged from asking questions.

when doing 2 dimensional is that say you say suchArray [9][4] which would result ins 9 rows and 4 columns.

That's correct for declaring a 2D array. There's a difference between declaring an array and referencing an array.

Like if I was to store or get all the numbers in column 2 i would have suchArray [i][1] (since arrays start at 0) and would be incrementing i every time to keep going down the column.

You would increment i to iterate through the rows. suchArray[i][1] assumes suchArray has at least 2 columns.

However, that's not what you're doing.
30
31
32
33
34
35
36
37
  char customerType[numberofcustomers][1]; //<- This declares a 2D array with one column.
                                                                      // Why not just use a 1D array?
  int premiumChanNum[numberofcustomers][2]; // <- This declares a 20x2 arrray
                                                                          //  A customer can only have one count of premium channels.  
                                                                           // So again, why have a 2D array?
  int basicServNum[numberofcustomers][3]; // <- This declares a 20x3 array.
                                                                   //  A customer can have only one basicServNum
                                                                   //  So again, why a 2D array? 

I think you're trying to use the 1,2,3 as a column number. Arrays don't work that way.
Each array declaration is separate. Forget trying to designate a column number.
See my previous post for how these should be declared.

69
70
71
		inFile >> customerType[i][1];
		inFile >> premiumChanNum[i][2];
		inFile >> basicServNum[i][3];

Here again, I think you trying to use 1,2,3 as column numbers. Again, arrays don;t work that way.

customerType was declared as a 20x1 array. customerType[i][1] refers to the second column of the 2D array, but you only declared one column (line 30), so you're referencing a non-existant column.

premiumChanNum was declared as a 20x2 array. premiumChanNum[i][2] refers to the third column which does not exist.

basicServNum was decalred as a 20x3 array. basicServNum[i][3] refers to the fourth column, which does not exist.

As I stated previously, customerType, premiumChanNum, basicServNum have no need to be 2D arrays.
Last edited on
Ok i think i am starting to understand now. I always over complicate things. I was thinking that while it is reading the file it was going to need me to designate what column it needs to read from next in the file but I can see why that is not necessary going go start editing code again now.
Topic archived. No new replies allowed.