I'm having a lot of difficulty trying to figure out how to label headings in a 2D array using enum type. Here is the basic for-loops to read in and out the values of an array. (This is a 5x5 array).
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
using namespace std;
const int ROW_MAX = 6;
const int COLUMN_MAX = 5;
enum MAKERS{Ford, BMW, Volvo, Chevy, Cadillac, Hyundai};
enum COLORS{Red, Brown, Black, White, Gray};
void reportHeading(ostream& out);
void printInventory(ostream& out,int carsInStock[][COLUMN_MAX],
const int ROW_MAX,const int COLUMN_MAX);
void convertToColors(COLORS c);
void convertToMakers(MAKERS m);
int main() {
int carsInStock[ROW_MAX][COLUMN_MAX];
ofstream out ("inventory.txt");
if (!out) {
cerr << "Error: cannot open the output file Cs12a-results.txt\n";
exit (1);
}
reportHeading(out);
printInventory(out,carsInStock,ROW_MAX,COLUMN_MAX);
return 0;
}
void reportHeading(ostream& out){
cout << "Cars In Stock Inventory\n"
<< "====================================================\n\n";
}
void printInventory(ostream& out,int carsInStock[][COLUMN_MAX],
const int ROW_MAX,const int COLUMN_MAX){
MAKERS maker = Ford;
COLORS color = Red;
ifstream infile("data.txt");
if(!infile){
cout << "Couldn't find the input file for data!\n";
exit(2);
}
for (maker; maker < ROW_MAX; maker = MAKERS(maker+1 )){
convertToMakers(maker);
for (color=Red; color < COLUMN_MAX; color = COLORS(color+1)){
infile >> carsInStock[maker][color];
cout << setw(6) << carsInStock[maker][color];
}
cout << endl;
}
}
void convertToMakers(MAKERS m)
{
switch(m)
{
case Ford: cout << "Ford" ; break;
case BMW: cout << "BMW" ; break;
case Volvo: cout << "Volvo" ; break;
case Chevy: cout << "Chevy" ; break;
case Cadillac: cout << "Cadillac" ; break;
case Hyundai: cout << "Hyundai" ; break;
}
}
void convertToColors(COLORS c){
switch(c)
{
case Red: cout << "Red" ; break;
case Brown: cout << "Brown" ; break;
case Black: cout << "Black" ; break;
case White: cout << "White" ; break;
case Gray: cout << "Gray" ; break;
}
}
Output looks something like this:
Ford 18 11 15 17 10
BMW 16 6 13 8 3
Volvo 9 4 7 12 11
Chevy 10 7 12 10 4
Cadillac 12 10 9 5 6
Hyundai 11 13 7 15 9
My question is how to label each column heading using enum type so it looks like this:
Red Brown Black White Gray - The headings should be over each number.
Ford 18 11 15 17 10
BMW 16 6 13 8 3
Volvo 9 4 7 12 11
Chevy 10 7 12 10 4
Cadillac 12 10 9 5 6
Hyundai 11 13 7 15 9
Is there any way to do this within the for loop so you get this desired result? I know the code is a bit sloppy, I've been changing things around constantly trying to get it to display correctly on the command prompt. Any help would be appreciated. If there is a more effiecient way to utilize enum types to display the headings please let me know.