Using a function to load an array from a text file

Hello, first time posting but I visit this site a lot for help and have been trying to solve this one for a while now, and I cant really find anything here or in my textbook.

Basically, my problem is that I want to load an array in main with a function called loadArray, which takes the 19 numbers from a text file. Then pass that array to my other functions. I just can't figure out the right way to call the loadArray function and have those 19 numbers be in the array initialized in main.

How do I call the loadArray function correctly? what should loadArray take as parameters and what code should it contain?

Sorry if this is a bit confusing. any suggestions or information would be greatly appreciated.



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
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h> 
#include <fstream>
using namespace std;
int loadArray(int array[19]);
void step1(int [], int);
void step2(int [], int);
void step3(int [], int);
void step4(int [], int);
void step5(int [], int);
void step6(int [], int);
void step7(int [], int);


int main(){
	
        const int ARRAY_SIZE = 19;
	int array[ARRAY_SIZE];
	for (int count = 0; count < 20; count++){
		int array[count] = loadArray(array, count);}
	step1(array, ARRAY_SIZE);
	step2(array, ARRAY_SIZE);
	step3(array, ARRAY_SIZE);
	step4(array, ARRAY_SIZE);
	step5(array, ARRAY_SIZE);
	step6(array, ARRAY_SIZE);
	step7(array, ARRAY_SIZE);
	
	
	
	
	system("pause");
	return 0;
}
int loadArray(int array[], int count){
	ifstream inf;
	inf.open("7data.txt");
	ifstream infile("7data.txt");
	if (!infile) {
		cerr << "Input file could not be opened" << endl;
		system("pause");
		exit(1);
	}
	const int ARRAY_SIZE = 19;
	int array[ARRAY_SIZE];
	int count = 0;
	ifstream inputFile;

	infile.open("7data.txt");

	while (count < ARRAY_SIZE && inputFile >> array[count])
		count++;
	infile.close();
	return array[19];
}

void step1(int array[], const int ARRAY_SIZE){
	ofstream outfile("7output.txt");
	if (!outfile){
		cerr << "Output file could not be opened" << endl;
		exit(1);
	}
	outfile << "1. Print out the contents of the array as well as the index values from locations 0 through 19" << endl << endl;
	cout << "1. Print out the contents of the array as well as the index values from locations 0 through 19" << endl << endl;
	outfile << "Element" << setw(13) << "Value" << endl;
	cout << "Element" << setw(13) << "Value" << endl;
	
	for (int j = 0; j < 20; j++) {
		outfile << setw(7) << j << setw(13) << array[j]<< endl;
		cout << setw(7) << j << setw(13) << array[j] << endl;
	}
	
	outfile << endl << endl << endl;
	cout << endl << endl << endl;
	outfile.close();
}
void step2(int array[], const int ARRAY_SIZE){
	ofstream outfile("7output.txt");
	if (!outfile){
		cerr << "Output file could not be opened" << endl;
		exit(1);
	}
	outfile << "2. Print out the contents of the array and the index values from locations 18 to 7." << endl << endl;
	cout << "2. Print out the contents of the array and the index values from locations 18 to 7." << endl << endl;
	outfile << "Element" << setw(13) << "Value" << endl;
	cout << "Element" << setw(13) << "Value" << endl;
	
	for (int j = 18; j > 6; j--) {
		outfile << setw(7) << j << setw(13) << array[j] << endl;
		cout << setw(7) << j << setw(13) << array[j] << endl;
	}
	
	outfile << endl << endl << endl;
	cout << endl << endl << endl;
	outfile.close();
}
void step3(int array[], const int ARRAY_SIZE){
	ofstream outfile("7output.txt");
	if (!outfile){
		cerr << "Output file could not be opened" << endl;
		exit(1);
	}
	outfile << "3. Print out the contents and index values for even index values" << endl << endl;
	cout << "3. Print out the contents and index values for even index values" << endl << endl;
	outfile << "Element" << setw(13) << "Value" << endl;
	cout << "Element" << setw(13) << "Value" << endl;
	
	for (int j = 0; j < 20; j += 2) {
		outfile << setw(7) << j << setw(13) << array[j] << endl;
		cout << setw(7) << j << setw(13) << array[j] << endl;
	}
	
	outfile << endl << endl << endl;
	cout << endl << endl << endl;
	outfile.close();
}
void step4(int array[], const int ARRAY_SIZE){
	ofstream outfile("7output.txt");
	if (!outfile){
		cerr << "Output file could not be opened" << endl;
		exit(1);
	}
	outfile << "4.  Print out the even valued contents and their index values." << endl << endl;
	cout << "4. Print out the even valued contents and their index values." << endl << endl;
	outfile << "Element" << setw(13) << "Value" << endl;
	cout << "Element" << setw(13) << "Value" << endl;
	
	for (int j = 0; j < 20; j ++)
		if (array[j] % 2 == 0){
			outfile << setw(7) << j << setw(13) << array[j] << endl;
			cout << setw(7) << j << setw(13) << array[j] << endl;
		}
	
	outfile << endl << endl << endl;
	cout << endl << endl << endl;
	outfile.close();
}
void step5(int array[], const int ARRAY_SIZE){
	ofstream outfile("7output.txt");
	if (!outfile){
		cerr << "Output file could not be opened" << endl;
		exit(1);
	}
	outfile << "5. Find and print-out the largest integer stored in the array\n as well as its index location value." << endl << endl;
	cout << "5. Find and print-out the largest integer stored in the array\n as well as its index location value." << endl << endl;
	int largest = 0;
	int largenum = 0;
	for (int j = 0; j < 20; j++){
		if (array[j] > largest) {
			largest = array[j];
		}
		largenum = j;
			}
	outfile << setw(7) << largenum << setw(13) << largest << endl << endl;
	cout << setw(7) << largenum << setw(13) << largest << endl << endl;
	outfile.close();
}
void step6(int array[], const int ARRAY_SIZE){
	ofstream outfile("7output.txt");
	if (!outfile){
		cerr << "Output file could not be opened" << endl;
		exit(1);
	}
	outfile << "6. Find and print the sum and average of all integers stored in the array." << endl << endl;
	cout << "6. Find and print the sum and average of all integers stored in the array." << endl << endl;
	int totalARRAYsum = 0;
	int totalARRAYavg = 0;
	for (int j = 0; j < 20; j++){
		totalARRAYsum = totalARRAYsum + array[j];
		totalARRAYavg = totalARRAYsum / 20;


	}
	outfile << "Total Array Value: ";
	cout << "Total Array Value: ";
	outfile << setw(7) << totalARRAYsum << endl;
	cout << setw(7) << totalARRAYsum << endl;
	outfile << "Total Array Average: ";
	cout << "Total Array Average: ";
	outfile << setw(5) << totalARRAYavg << endl;
	cout << setw(5) << totalARRAYavg << endl;
	outfile << endl << endl << endl;
	cout << endl << endl << endl;
	outfile.close();
}
void step7(int array[], const int ARRAY_SIZE){
	ofstream outfile("7output.txt");
	if (!outfile){
		cerr << "Output file could not be opened" << endl;
		exit(1);
	}
	outfile << "7. Find and print-out location(s) and content values,\n if any, where the content of the array is three times its index value." << endl << endl;
	cout << "7. Find and print-out location(s) and content values,\n if any, where the content of the array is three times its index value." << endl << endl;
	for (int j = 1; j < 20; j++){
		if (j * 3 == array[j]) {
			outfile << array[j] << " is 3 times it'ss index value " << j << "\n";
			cout << array[j] << " is three times it's index value " << j << "\n";
		}
	}
	outfile.close();
}
A sample of what you need is this :

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
#include <iostream>
#include <fstream>

using namespace std;

int loadArray(int array[19],int);

int main()
{
	const int ARRAY_SIZE = 19;
	int array[ARRAY_SIZE];
	
	//fill array from file
	loadArray(array, ARRAY_SIZE);

	//print array
	for (int i = 0; i < 19; i++)
	{
		cout << array[i] << endl;
	}

	system("pause");
	return 0;
}
int loadArray(int array[], int arraySize)
{
	//open file
	ifstream infile("numbers.txt");
	if (!infile) {
		cerr << "Input file could not be opened" << endl;
		exit(1);
	}
	
	int count = 0;

	while (count < arraySize && infile >> array[count])
	{
		count++;
	}
	
	//close file
	infile.close();
}
Thank you. I just needed that and a global variable. I have it working now.
Topic archived. No new replies allowed.