Inputting data from a file to an array

I am trying to write code from a file to an array, but it fails to open each time. I used a simpler skeleton of code to write this one and the simpler version compiles and reads from the file. The stuff I have commented out was just me trying to troubleshoot my problems. If someone could give me some pointers I would appreciate 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
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
#include <iostream>
#include <fstream>
#include <string>


using namespace std;

const int CAPACITY = 20;

class records
{
public:
	string cellPhoneNumber;
	int numOfRelays, minutes;
	double netCost, taxRate, taxOnCall, totalCostOfCall;
};


void initialize_DB(records *call_DB, int &count);
int search_DB(records *,const int);
//void Print(records *,const int);
void process_DB(records *, const int);
void call_stats(records *, const int);
void add_DB(records *, int);
void remove_DB(records *, int);
void print_DB(records *, const int);
bool IsFull(int);
bool IsEmpty(int);

void intialize_DB(records *call_DB, int &count)
{
	string filename;
	ifstream input;

	count = 0;

	cout<<"Enter the data filename:";
	cin>>filename;
	input.open(filename.c_str(), ios::in);
	  //if (input.fail( )) //Testing if file opened correctly
   // {
   //     cout << "Input file opening failed.\n";
   //    exit(1);
   // }
	while(!input.eof() && count < CAPACITY)
	{
		
		if (!input.eof())
		{
			input>>call_DB[count].cellPhoneNumber>>call_DB[count].numOfRelays>>call_DB[count++].minutes;
		}
	
	}

	input.close();
}

int search_DB(records *call_DB, const int count)
{
    string key;
	cout << "Enter the number you want to know exists: "; 
	cin >> key;  


		for(int i=0; i<count; i++)
		{
		if (call_DB[i].cellPhoneNumber==key)
			
		return i;
				
		}
		return -1; 
}

bool IsFull(int count)
{

	return count == CAPACITY;

}

bool IsEmpty(int count)
{
	return count == 0;
}


void process_DB(records *call_DB, int count)
{
	double taxRate;

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

	call_DB[count].netCost = ((0.40 * call_DB[count].minutes)*(call_DB[i].numOfRelays/50.0));	//makes calculations for the cost of the phone calls
	call_DB[count].taxOnCall = (call_DB[count].netCost * taxRate);
	call_DB[count].totalCostOfCall = (call_DB[count].netCost + call_DB[count].taxOnCall);
	}
}

void call_stats(records *call_DB, const int count)
{


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



}

void add_DB(records *call_DB, int count)
{
	string key;
	if (!IsFull(count))
	{
		call_DB[count++].cellPhoneNumber=key;
	}
	else
	{
		cout<<"call_DB is full -- No addition possible\n";
	}
}

void remove_DB(records *call_DB, int & count, string key)
{

	
	int i= search_DB(call_DB, count);

	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";
	}
	else
	{
		cout<<key<<" Is not in call_DB, therefore it can't be deleted. "<<endl;
	}
}
//void Print(records *call_DB,const  int count)
//{
//	int i;
//	for(i=0; i<count; i++)
//	{
//		cout<<call_DB[i].cellPhoneNumber<<"\t"<<call_DB[i].numOfRelays<<"\t"<<call_DB[i].minutes<<endl;
//	}		
//
//	
//	
//}


void print_DB(records *call_DB, int count)
{
	ofstream out;
	string filename;

	cout<<"Enter the output data filename: ";
	cin>>filename;

	out.open(filename.c_str());

	for(int i=0; i<count; i++)
	{
		out<<call_DB[i].cellPhoneNumber<<"\t"<<call_DB[i].numOfRelays<<"\t"<<call_DB[i].minutes<<"\t"<<call_DB[i].netCost<<"\t"<<call_DB[i].taxRate
			<<"\t"<<call_DB[i].taxOnCall<<"\t"<<call_DB[i].totalCostOfCall<<endl;
	}

	out.close();
}

int main()
{
	records call_DB[CAPACITY];
	int count;
	string key;

	intialize_DB(call_DB,count);
	cout<<search_DB(call_DB,count)<<endl;
	//Print(call_DB, count);
	process_DB(call_DB,count);
	call_stats(call_DB,count);
	add_DB(call_DB,count);
	remove_DB(call_DB,count, key);
	print_DB(call_DB,count);

	return 0;
}
Topic archived. No new replies allowed.