Data from file is not being input into array

I am going to post the header file and the implementation file so maybe somebody can help me figure out why this is not working. I am trying to input data from a file into an array of records, but it does not seem to be working properly. Anyone with suggestions I would appreciate it. I have commented out some functions because I was trying to identify the bugs.
#include <iostream>
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <fstream>
#include <string>
#include "calls.h"

using namespace std;

/**********************************************************
Name: Default constructor
Precondition: call_DB and count have not been initialized
Postconditon: Elements stored in the input file have been
              entered into call_DB.  The value of count is equal
			  to the number of elements stored in call_DB.
Description: Enter records from a user-specified datafile
             into the array call_DB.
***********************************************************/
CALLS::CALLS()
{
	this->call_DB = NULL;
	
	cout<<"Default Constructor has been called.\n";
	
	ifstream input ("call_data.txt");
	string filename;

	CAPACITY = 5;

	call_DB = new call_record[CAPACITY];
	
	count = 0;

	//cout<<"Enter the filename: ";
	//cin>>filename;

	//input.open(filename.c_str());
		while(!input.eof() && (*this).count < this->CAPACITY)
		{
		
			if (!input.eof())
			{
			input>>this->call_DB[count].cellPhoneNumber;
			input>>this->call_DB[count].relays;
			input>>this->call_DB[count++].minutes;
			}
		}

	input.close();

	}
	
/****
Name: CALLS    destructor
Precondition: call_DB has dynamic memory
Postconditon: call_DB's dynamic memory has been de-allocated 
              entered into call_DB.  The value of count is equal
			  to the number of elements stored in call_DB.
Description: Enter integers from a user-specified datafile
             into the array call_DB.
***********************************************************/
CALLS::~CALLS()
{
	cout<<"The destructor has been called.\n";
	delete [ ] call_DB;
	
}
/**********************************************************
Name: search_DB
Precondition: call_DB and key have been initialized.
Postconditon: key location has been found; if not, -1 
              is return
Description: Search for key inside call_DB
***********************************************************/
int CALLS::search_DB(string key) const
{

	
		for(int i=0; i<count; i++)
		{
			if (call_DB[i].cellPhoneNumber==key)
			{
			
				return i;
			}	
		}
		return -1; 
	
}
/**********************************************************
Name: process_data_DB
Precondition: call_DB and key have been initialized.
Postconditon: Tax rate, net cost, tax on call, and total cost of call are calculated.
Description: Function calculates the rest of the information for the phone record.
***********************************************************/

void CALLS::process_data_DB()
{

	for(int i=0; i<count; i++)
	{
	if (1<=call_DB[i].relays&&call_DB[i].relays<=5)	//calculates tax rate
		call_DB[i].taxRate = .01;
	else if (6<=call_DB[i].relays&&call_DB[i].relays<=11)
		call_DB[i].taxRate = .03;
	else if (12<=call_DB[i].relays&&call_DB[i].relays<=20)
		call_DB[i].taxRate = .05;
	else if (21<=call_DB[i].relays&&call_DB[i].relays<=50)
		call_DB[i].taxRate = .08;
	else if (51>=call_DB[i].relays)
		call_DB[i].taxRate = .12;
	else if (call_DB[i].relays <= 0)
		call_DB[i].taxRate = 0;

	call_DB[i].netCost = ((0.40 * call_DB[i].minutes)*(call_DB[i].relays/50.0));	//makes calculations for the cost of the phone calls
	call_DB[i].taxOnCall = (call_DB[i].netCost * call_DB[i].taxRate);
	call_DB[i].totalCostOfCall = (call_DB[i].netCost + call_DB[i].taxOnCall);
	}
}
/**********************************************************
Name: call_Stats
Precondition: call_DB and key have been initialized.
Postconditon: Prints formatted call stat to the screen.
Description: User inputs cell phone number to be printed to the screen.
***********************************************************/

void CALLS::call_Stats(const string & key) const
{
	cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
	
	call_record total;
	
	total.relays=0;
	total.minutes=0;
    total.netCost=0;
    total.taxOnCall=0;
    total.totalCostOfCall=0;


	for(int i=0; i<count; i++)
	{
		if(key==call_DB[i].cellPhoneNumber)
		{
			cout<<"\t"<<call_DB[i].cellPhoneNumber<<"\t";
			cout<<call_DB[i].relays<<"\t";
			cout<<call_DB[i].minutes<<"\t";
			cout<<call_DB[i].netCost<<"\t";
			cout<<call_DB[i].taxRate<<"\t";
			cout<<call_DB[i].taxOnCall<<"\t";
			cout<<call_DB[i].totalCostOfCall<<endl;
		
			total.relays+=call_DB[i].relays;
			total.minutes+=call_DB[i].minutes;
			total.netCost+=call_DB[i].netCost;
			total.taxOnCall+=call_DB[i].taxOnCall;
			total.totalCostOfCall+=call_DB[i].totalCostOfCall;
		}
		
	}
		cout<<"Totals\t\t\t";
		cout<<total.relays<<"\t";
		cout<<total.minutes<<"\t";
		cout<<total.netCost<<"\t\t";
		cout<<total.taxOnCall<<"\t";
		cout<<total.totalCostOfCall<<"\t"<<endl;

}
/******************************************************
Name: IsFull
Precondition: None
Postcondtion: True is return if call_DB is full; otherwise false
Description: Determine if call_DB is full
********************************************************/
bool Is_Full(const CALLS & current)
{
	return current.count == current.CAPACITY;
}

/******************************************************
Name: IsEmpty
Precondition: None
Postcondtion: True is return if call_DB is empty; otherwise false
Description: Determine if call_DB is empty
********************************************************/
bool Is_Empty(const CALLS & current)
{
	return current.count == 0;
}
/******************************************************
Name: add_DB
Precondition: key contains the value to be added to call_DB
Postcondtion: key has been added to call_DB if call_DB is not full
Description: Adds cell phone number, relays, and minutes to call_DB.
********************************************************/

//void CALLS::Add_DB(const string & key)
//{
//	
//
//
//	
//		if (!Is_Full(*this))
//		{
//			call_DB[count++].cellPhoneNumber=key;
//		}
//		else
//		{
//			double_DB();
//		}
//
//	
//}
/******************************************************
Name: remove_DB
Precondition: key contains the value to be removed from call_DB
Postcondtion: key has been removed from call_DB if call_DB is not empty
Description: Removes key from call_DB if it is there
********************************************************/

//void CALLS::remove_DB(const string & key)
//{
//	cout<<"Inside remove_DB\n";
//	
//	int i= search_DB(key);
//	
//	
//	while(i!=-1)
//	{
//		if (i!=-1)
//		{
//			for(int j=i; j<count-1; j++) //shifting up
//			{
//				call_DB[j] = call_DB[j+1];
//			}
//			count--;
//		}
//		
//		else if (count == 0)
//		{
//			cout<<"Can't delete "<<key<<" because call_DB is empty\n";
//		}
//		
//	i= search_DB(key);
//	 }
//
//}
/**********************************************************
Name: print_DB
Precondition: none
Postconditon: If call_DB was not empty its contents are printed
Description: This function prints the contents of call_DB to a
             user-specified file.
***********************************************************/
void CALLS::print_DB() const
{
	cout<<"print_DB has been called";
	
	cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
	cout.precision(2);


	for(int i=0; i<(*this).count; i++)
	{
		cout<<call_DB[i].cellPhoneNumber<<"\t"<<"\n";
	}

}
/******************************************************
Name: double_DB
Precondition: array call_DB needs to be doubled to make room for more records
Postcondtion: array call_DB doubled in size
Description: Doubles call_DB if it is needed
********************************************************/
//void CALLS::double_DB() 
//{
//	cout<<"Inside double_DB, original capacity = "<<CAPACITY<<endl;
//	
//	CAPACITY *= 2;
//	
//	cout<<"New CAPACITY = "<<CAPACITY<<endl;
//
//	call_record *call_DB2 = new call_record[CAPACITY];
//	
//	for(int i=0; i<count;i++)
//	{
//		call_DB2[i]=call_DB[i];
//	}
//	count = CAPACITY;
//	
//	delete [] call_DB;
//	call_DB=call_DB2;
//}
/////******************************************************
////Name: ostream & operator<<
////Precondition: A has been initialized
////Postcondtion: Sum of A has been computed and is returned
////Description: Determines the sum of all the element in A
////********************************************************/
ostream & operator<<( ostream & out, CALLS & original) 
{
	cout<<"Overloaded operator<< has been called\n";
	
	for(int i=0; i<original.count; i++)
	{
		out<<original.call_DB[i].cellPhoneNumber<<endl;
	}

	return out;

}
One of the first suggestions I will make is to stop trying to do things like opening a file in your constructor. What happens if your file fails to properly open? Since this is a constructor you have very limited ways of dealing with this failure. Also never use eof() to control a data entry loop, you will usually get one extra line of input if you do. I also don't recommend reading a file in a class constructor, use a separate function to read the file and populate the class variables. The reason for this is because a constructor doesn't really have a way of telling you that it failed (until you start using exceptions) so delay populating the class until after your constructor because normal member functions can return error codes.

And by the way you should always check that your file opens correctly, and take proper action if it doesn't.

Next when you are accessing class member variables in a class member function you don't need to use the *this pointer, except if there is a scope issue. Also If you do use the *this pointer use it consistently, don't switch from (*this).something to this->something.
Thanks, I was switching between this pointers to see if it made a difference on anything.
Topic archived. No new replies allowed.