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.
#include <iostream>
#include <fstream>
#include <ctime>
usingnamespace 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;
}
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 ...