not reading any values from a file into an array

I am trying to make a program here that will read values from a file into an array, all I'm getting for numbers is 0. do I need to add a for loop?
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
#define NUM_DEPTS 2
#define NUM_STORES 2
#define NUM_MONTHS 12
ifstream in;


double sales[NUM_DEPTS][NUM_MONTHS][NUM_STORES];

	void main()
	{
		in.open("sales.txt");												//opens the file
		if (!in)															//checks for the input file

		{
			cerr << "****Error Sales.txt file does not exist****" << '\n';	//checks for errors from the file
			exit(1);
		}
		in >> sales[NUM_DEPTS][NUM_MONTHS][NUM_STORES];						 //puts the data from the fale into the array

		int month,dept,store, D = 0;
		double X;															//used to handle the incoming data
		do
		{
			cout << "Which month would you like to look at?" << '\n' << "Enter 1-12" << '\n';
			cin >> month;
			cout << "Enter 1 for sales by department, or press 2 for total sales by stores" << '\n';
			cin >> dept;
			month--;														//Removes one value from the user input
			if (dept = 1)

			{
				store = 0;
				X = sales[0][month][store] + sales[1][month][store];		//adds the two values together
				
				month++;													//adds the user inputed value back to the month to print on the screen
				cout << "The sales for the month of " << month << " for both departments are " << X << endl;

			}

			else if (dept = 2)
			{
				dept = 0;
				X = sales[dept][month][0] + sales[dept][month][1];			//adds the two values together

				month++;													//adds the user inputed value back to the month to print on the screen
				cout << "The sales for the month of " << month << " for both stores is " << X << endl;

			}
			cout << "Would you like to run the program again?" << '\n';
			cout << "Enter 1 for Yes or 2 for No" << '\n';
			cin >> D;
			
		} 
		while (D != 2);														//while loop to keep program running
		in.close();															//closes the file
	};
Last edited on
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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

	int A[2][2][2] = 
	//A
	//----------
	{
		//A[0]
		//------------
		{	
			//A[0][0]
			//-------
			//A[0][0][0],A[0][0][1]
			{1,2},

			//A[0][1]
			//------------
			//A[0][1][0], A[0][1][1]
			{3,4},
		},
		//A[1]
		//----------
		{
			//A[1][0]
			//---------
			//A[1][0][0],A[1][0][1]
			{5,6},

			//A[1][1]
			//-----------
			//A[1][1][0],A[1][1][1]
			{7, 8}
		}
	};

	//Q1: I can't seem to figure out how to have the program 
	//ignore one set of data and only give me the other set

	int choice;
	cout << "Enter 1 for 1st set, 2 for second set: ";
	cin >> choice;

	switch (choice)
	{
	case 1:
		//A[0][i][j], 

		cout << endl << "Case 1: " << endl << endl;

		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				cout << setw(4) << A[0][i][j];
			}
			cout << endl;
		}
		break;
	case 2:
		//A[1][i][j]

		cout << endl << "Case 2: " << endl << endl;

		for (int i = 0; i < 2; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				cout << setw(4) << A[1][i][j];
			}
			cout << endl;
		}
		break;
	default:
		cout << "invalid choice" << endl;
	}

	//Q2: I also don't know how to do addition with values from an array.

	//Answer, you should learn arrays from a book if you dont know this.

	//access array element with the [] operator.

	int sum = A[0][1][1] + A[1][0][0];
	cout << endl<< "Sum is "<<sum << endl;

	system("pause");
	return 0;

}
I have added 3 for loops and get the same result, any help would greatly be appreciated.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	in >> sales[NUM_DEPTS][NUM_MONTHS][NUM_STORES];						 //puts the data from the fale into the array
		float A, B, C;
		A = NUM_DEPTS;
		B = NUM_MONTHS;
		C = NUM_STORES;
		for (A = 0; A < 2; A++)
		{
			for (B = 0; B < 12; B++)
			{
				for (C = 0; C < 2; C++)
				{
				}
			}
		}
Topic archived. No new replies allowed.