2D Array

Apologies, I am new here and short on time so sorry if this is the incorrect place to be posting. I am just trying to finish my semester and coding has been the bane of my existence because I understand it very little. I am working on a 2D Array project and don't really understand how to get both columns and rows. Here is what I have so far along with txt file information. Any advice in simple terms about how to achieve the rows and columns would be super helpful. The visitor information is supposed to be 9 columns and 12 rows while the months is supposed to be 12 rows.

#include "pch.h"
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>

using namespace std;


int main()
{
double visitors[9];
double months[12];

ifstream inputVisitors;
inputVisitors.open("Visitors.txt");

ifstream inputMonths;
inputMonths.open("Months.txt");


for (int index = 0; index <12; index++)
inputVisitors >> visitors[index];

for (int index = 0; index < 12; index++)
cout << (index + 1) << " " << visitors[index] << endl;


inputVisitors.close();

system("pause");
return 0;
}

Visitors.txt

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


Months.txt

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


Last edited on
Try something like this, however if you want to read in strings you'll need to use a string array.
1
2
3
4
5
6
7
8
int visitors[12][9];
for (int index = 0; index < 12; index++)
	{
		for (int i = 0; i < 9; i++)
		{
			inputvisitors >> visitors[index][i];
		}
	}
Last edited on
Topic archived. No new replies allowed.