Reading values from 2 text files into array (or vector)

I have two text files:

17204 600 0 0 0 0 0 0 0
16530 600 0 0 0 0 0 0 0
28121 600 0 0 0 0 0 0 0
94199 600 0 885 168 0 0 10 1063
217147 7500 0 10521 1854 0 38 637 13050
445410 7500 0 25488 7938 0 303 1425 35154
696854 7500 0 39942 11757 0 353 2966 55018
735945 7500 0 40557 11649 360 381 2250 55197
570434 7500 0 23148 10044 617 249 984 35042
412416 6000 0 12270 3219 0 0 291 15780
55022 600 0 0 0 0 0 0 0
14111 600 0 0 0 0 0 0 0

and:

January February March April May June July August September October November December

I need to create a table like this:

January 17204 600 0 0 0 0 0 0 0
February 16530 600 0 0 0 0 0 0 0
March 28121 600 0 0 0 0 0 0 0
April 94199 600 0 885 168 0 0 10 1063
May 217147 7500 0 10521 1854 0 38 637 13050
June 445410 7500 0 25488 7938 0 303 1425 35154
July 696854 7500 0 39942 11757 0 353 2966 55018
August 735945 7500 0 40557 11649 360 381 2250 55197
September 570434 7500 0 23148 10044 617 249 984 35042
October 412416 6000 0 12270 3219 0 0 291 15780
November 55022 600 0 0 0 0 0 0 0
December 14111 600 0 0 0 0 0 0 0

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
  	int table1[ROWS][COLS];

	string table2[ROWS];

	int choice,
		count,
		index;

	string monthNames;

	ifstream inputFile1;
	ifstream inputFile2;

	inputFile1.open("visitors.txt");
	if (!inputFile1.is_open()) return -1;

	for (count = 0; count < ROWS; count++)
	{
		for (index = 0; index < COLS; index++)
			inputFile1 >> table1[ROWS][COLS];
	}
	inputFile1.close();
	
	inputFile2.open("months.txt");
	if (!inputFile2.is_open()) return -1;

	for (count = 0; count < ROWS; count++)
	{
		inputFile2 >> table2[ROWS];
	}
	inputFile2.close();


There are more parts to the program, but I need help getting the values into an array (or vector)
1
2
3
4
	for (count = 0; count < ROWS; count++)
	{
		inputFile2 >> table2[ROWS];
	}
do a dry-round
read your code aloud, line by line
replace the variables by the values they hold in that moment and write down what operations are you doing and what result do they give you
simulate all iterations of that loop.


also, I wonder how did you figure out that you didn't read the values correctly.
Last edited on
When I try to compile it there is an error "....has exited with code -1073741510"

I am a beginning beginner. So I'm sure it's a structure problem.
I feel that a vector might be best suited for this so I can recall the values later, but honestly I don't know.
If the process "has exited", that means it at least ran, which indicates it's a run-time error, not a compile-time error. If you run while debugging, it will likely show you the exact like it's crashing on!
(Visual Studio: https://docs.microsoft.com/en-us/visualstudio/debugger/getting-started-with-the-debugger-cpp?view=vs-2019)

ne555 gave you a big hint. Read the code he quoted OUT LOUD, to yourself. Physically, out loud.
Verbally describe each part of code in ne555's post to an imaginary listener.

Hint: How do you access the first element on an array? (or any element of an array, for that matter)
Last edited on
Just use two input files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <fstream>
#include <string>

using std::ifstream;
using std::cout;
using std::string;

int main()
{
    ifstream months("months.txt");
    ifstream nums("nums.txt");
    string monthName;
    string numbers;
    while (months >> monthName) {
	getline(nums, numbers);
	cout << monthName << ' ' << numbers << '\n';
    }
}

Why do you need to read the names of months from file?
> Why do you need to read the names of months from file?
localization may be one reason
also, homework (¿what wonderful things may be learned from this exercise?)


@OP: you have the same problem in lines 17--21, but I think that it's easier to see in lines 27--30
don't forget to fix both.
After a whole day of scanning the internet and trial and error, I was able to successfully read from the file and get the output I needed. @ne555: Yes to everything you said. The code I finally came up with is below.
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

void dataTable()
{

	string month[12];
	int recVisitors[12],
		nonRecVisitors[12],
		conLodging[12],
		tentCampers[12],
		rvCampers[12],
		conCampers[12],
		backCountry[12],
		miscCamp[12],
		overnight[12];

	int index = 0;

	ifstream inputFile1("visitors.txt");
	ifstream inputFile2("months.txt");

	if (!inputFile1 || !inputFile2)
		cout << "Failed to Open File";

	while (inputFile1 >> recVisitors[index] && (inputFile2 >> month[index]))
	{
		inputFile1 >> nonRecVisitors[index];
		inputFile1 >> conLodging[index];
		inputFile1 >> tentCampers[index];
		inputFile1 >> rvCampers[index];
		inputFile1 >> conCampers[index];
		inputFile1 >> backCountry[index];
		inputFile1 >> miscCamp[index];
		inputFile1 >> overnight[index];
		index++;
	}

	index = 0;

	cout << "\t\t\t\t\tVisitors to the Park by Month and Type\n"
		<< "\t\t\t\t\t---------------------------------------\n"
		<< left << setw(10) << "Month" << setw(15) << "Rec Visitors" << setw(11) << "Non-Rec" << setw(15) << "Con Lodging" << setw(9) << "Tent"
		<< setw(8) << "RV" << setw(15) << "Con Camping" << setw(13) << "BackCountry" << setw(13) << "Misc Camp" << setw(9) << "Overnight" << endl;
	cout << "------------------------------------------------------------------------------------------------------------------------\n";

	while (index != 12) {
		cout << left << setw(10) << month[index] << setw(15) << recVisitors[index] << setw(11) << nonRecVisitors[index] << setw(15) << conLodging[index] << setw(9) << tentCampers[index] << setw(8) << rvCampers[index]
			<< setw(15) << conCampers[index] << setw(13) << backCountry[index] << setw(13) << miscCamp[index] << setw(9) << overnight[index];
		cout << endl;
		index++;
	}

	cout << endl << endl;
}

void recAndNonRecVisitors(int recVisitors[], int nonRecVisitors[])
{
	const int ROWS = 12;
	int recVisitorsSum = 0, nonRecVisitorsSum = 0;

	for (int index = 0; index < ROWS; index++)
	{
		recVisitorsSum += recVisitors[index];
		nonRecVisitorsSum += nonRecVisitors[index];
	}

	cout << "Total Recreational Visitors to the Park: " << recVisitorsSum << endl;
	cout << "Total Non Recreational Visitors to the Park: " << nonRecVisitorsSum << endl;
	
	cout << endl << endl;
}

void tentAndRV(int tentCampers[], int rvCampers[])
{
	const int ROWS = 12;
	int tentSum = 0,
		rvSum = 0;
	for (int index = 0; index < ROWS; index++)
	{
		tentSum += tentCampers[index];
		rvSum += rvCampers[index];
	}

	cout << "The total of Tent Campers to the Park: " << tentSum << endl;
	cout << "The total of RV Campers to the Park: " << rvSum << endl;

	cout << endl << endl;
}

void recVisitorsByMonth(int recVisitors[])
{
	const int ROWS = 12;
	int monthSel;

	cout << "Recreational Visitors By Month\n"
		<< "------------------------------\n"
		<< "1. January\n"
		<< "2. February\n"
		<< "3. March\n"
		<< "4. April\n"
		<< "5. May\n"
		<< "6. June\n"
		<< "7. July\n"
		<< "8. August\n"
		<< "9. September\n"
		<< "10. October\n"
		<< "11. November\n"
		<< "12. December\n\n";

	cout << "Enter the number of the Month to see the number of Recreational Visitors: ";
	cin >> monthSel;
	cout << endl << endl;
	while (monthSel < 1 || monthSel > 12) {
		cout << "Please choose a NUMBER with the corresponding month!\n\n";
	
		cin >> monthSel;
		cout << endl << endl;
	}

	cout << "The number of Recreational Visitors in that month: " << recVisitors[--monthSel] << " Recreational Visitors" << endl;

	cout << endl << endl;

}

int main()
{
	const int FIRST_CHOICE = 1,
		SECOND_CHOICE = 2,
		THIRD_CHOICE = 3,
		FOURTH_CHOICE = 4,
		EXIT_CHOICE = 5,
		ROWS = 12;

	int recVisitors[ROWS],
		nonRecVisitors[ROWS],
		conLodging[ROWS],
		tentCampers[ROWS],
		rvCampers[ROWS],
		conCampers[ROWS],
		backCountry[ROWS],
		miscCamp[ROWS],
		overnight[ROWS];
	string month[ROWS];

	int choice,
		count,
		index = 0;

	string monthName;

	ifstream inputFile1("visitors.txt");
	ifstream inputFile2("months.txt");

	if (!inputFile1 || !inputFile2)
		cout << "File Failed to Open";

	while (inputFile1 >> recVisitors[index] && (inputFile2 >> month[index]))
	{
		inputFile1 >> nonRecVisitors[index];
		inputFile1 >> conLodging[index];
		inputFile1 >> tentCampers[index];
		inputFile1 >> rvCampers[index];
		inputFile1 >> conCampers[index];
		inputFile1 >> backCountry[index];
		inputFile1 >> miscCamp[index];
		inputFile1 >> overnight[index];
		index++;
	}

	do
	{
		cout << "Visitors to Arcadia National Park\n"
			<< "----------------------------------\n"
			<< "1. Display the Data Table\n"
			<< "2. Display the total number of recreation and non-recreational visitors\n"
			<< "3. Display the total tent and RV campers by month\n"
			<< "4. Display the number of recreational visitors for a specific month\n"
			<< "5. EXIT PROGRAM\n\n"
			<< "Enter the number of your choice: ";
		cin >> choice;
		cout << endl;

		while (choice < FIRST_CHOICE || choice > EXIT_CHOICE)
		{
			cout << "Please enter a valid choice: ";
			cin >> choice;
			cout << endl;
		}
			switch (choice)
			{
			case FIRST_CHOICE:
				dataTable();
				break;
			case SECOND_CHOICE:
				recAndNonRecVisitors(recVisitors, nonRecVisitors);
				break;
			case THIRD_CHOICE:
				tentAndRV(tentCampers, rvCampers);
				break;
			case FOURTH_CHOICE: 
				recVisitorsByMonth(recVisitors);
				break;
			case EXIT_CHOICE:
				return 0;
			}
	} while (choice == EXIT_CHOICE);

	system("pause");
	return 0;
}


I now have a (probably) simple problem. I can't seem to get my menu from the main function to loop again after the "user" has ran one of the options. I tried the do/while to no avail.

Do I need to flip some of them to different places?

I know I haven't done it, so thank all of you for your help! It's much appreciated.
Line 212 should be while (choice != EXIT_CHOICE); You want to continue running when they don't select exit, not when they do.
Looking at code all day and it was in front of my face! Thank you @dhayden!
Topic archived. No new replies allowed.