Enumerations in Array

Could anyone please assist me in incorporating my enumerations with my arrays? I've tried putting it in, but the for-loops don't work. I put it in the "total t-shirt" loop.

Thanks!

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
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;

enum SIZE {S, M, L, XL};
enum COLOR {RED, BLACK, BLUE, GREEN};

int input(istream& in=cin)
{
	int x;
	in >> x;
	return x;
}

int main ()
{
	time_t P;
	time (&P);
	cout << "Today's time and date is: " << ctime(&P) << endl;
	
	int t[4][4];

	//Copy data from "invt.txt" into array t
	 fstream f;
     f.open ("invt.txt", ios::in);

	 for (int i = 0; i < 4; ++i)
     {
		 for(int j = 0; j < 4; ++j)
		 {
			t[i][j] = input(f);
		 }	
     }

	 //Display array t
	 for (int row = 0; row < 4; ++row)
     {
		 for (int col = 0; col < 4;  ++col)
		 {
			cout << t[row][col] << '\t';
		 }

		  cout << endl;
     }
	 cout << endl;

	 //Compute and print the total number of all tee shirts
	 int TotalAll=0;
	 for(COLOR row=RED; row<GREEN; row+1)
	 {
		 for(SIZE col=S; col <XL; col+1)
		 {
			TotalAll+=t[row][col];
		 }
	 }

		cout << "Total number of shirts: " << TotalAll << endl;

	//Compute and print the number of all RED shirts
	int TotalRED=0;
	int row = 0;
	for (int col = 0; col < 4; ++col)
	{
		TotalRED+=t[row][col];
	}
	cout << "Total number of RED shirts is: " << TotalRED << endl;

	//Compute and print the number of all Large shirts
	int TotalLarge=0;
	int col =2;
	for (int row = 0; row < 4; ++row)
	{
		TotalLarge+=t[row][col];
	}
		cout << "Total number of all LARGE shirts: " <<TotalLarge<< endl;

	//Terminate program
	system ("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
	 //for(COLOR row=RED; row<GREEN; row+1)
	 for( COLOR row = RED ; row <= GREEN; row = COLOR(row+1) )
	 {
		 //for(SIZE col=S; col <XL; col+1)
		 for( SIZE col = S ; col <= XL ; col = SIZE(col+1) )
		 {
			TotalAll += t[row][col];
		 }
	 }
It works! Thank you for your help!
This would be more consistent:

1
2
enum SIZE {S, M, L, XL, NSIZES };
enum COLOR {RED, BLACK, BLUE, GREEN, NCOLOURS };


And then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int t[NCOLOURS][NSIZES];

// Copy data from "invt.txt" into array t
// fstream f;
// f.open ("invt.txt", ios::in);
 
 { // restrict the life of the stream object
     ifstream f( "invt.txt" ) ; // prefer using  a std::ifstream for input

     for( COLOR clr = RED ; clr < NCOLOURS ; clr = COLOR(clr+1) )
     {
         for( SIZE sz = S ; sz < NSIZES ; sz = SIZE(sz+1) )
         {
            t[clr][sz] = input(f);
         }
     }
 } // the ifstream is destroyed

// etc ... 

Alternatively, if you want to avoid the casts

1
2
3
4
5
6
7
	 for( int row = RED ; row <= GREEN; ++row )
	 {
		 for( int col = S ; col <= XL ; ++col )
		 {
			TotalAll += t[row][col];
		 }
	 }


Andy
Topic archived. No new replies allowed.