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.

Yes, you can do it in a loop. The trick is making things line up nicely.

For this kind of exercise, I usually find it best to get out a piece of graph paper and decide where each character in the output should be. I notice you are using the setw() manipulator. Good job. You'll also want to output some strategically places spaces and the like to make your header line up.

Good luck.

I've been trying various things but take this for example:

for (maker; maker < ROW_MAX; maker = MAKERS(maker+1 )){
		convertToMakers(maker);
		for (color=Red; color < COLUMN_MAX; color = COLORS(color+1)){
			convertToColors(color);
			infile >> carsInStock[maker][color];
			cout << setw(6) << carsInStock[maker][color];
		}
	cout << endl;
	}

Obviously this is wrong, but I'm just showing it as an example. If I put the function call in the second loop, it will display the heading before each number. I'm still extremely confused on how to display each heading, and then proceed to display each number. I have no idea where to place it properly in the loop to achieve what I want. :(

You'll need two separate loops. One to display the heading, then a nested loop to display the rest of the information.

Please try the graph paper approach. It will help you figure out exactly how to format your output.

for(color; color < COLUMN_MAX; color = COLORS(color+1)){
	cout << setw(7);
                convertToColors(color);
	}
	cout << endl;
	for (maker; maker < ROW_MAX; maker = MAKERS(maker+1 )){
		cout << setw(10) << left;
		convertToMakers(maker);
		for (color=Red; color < COLUMN_MAX; color = COLORS(color+1)){
				infile >> carsInStock[maker][color];
			    cout << setw(7) << carsInStock[maker][color];
		}
	cout << endl;
	}
}

Making a separate loop makes more sense (is this what you meant?). The first loop creates the column heading and formats them. The nested loop creates the array along with the row headings and formats both to fit properly. Thanks for the advice.

Your indentation is horrific. Try this:

for(color; color < COLUMN_MAX; color = COLORS(color+1)) {
	cout << setw(7);
	convertToColors(color);
	}
cout << endl;

for (maker; maker < ROW_MAX; maker = MAKERS(maker+1 )) {
	cout << setw(10) << left;
	convertToMakers(maker);

	for (color=Red; color < COLUMN_MAX; color = COLORS(color+1)) {
		infile >> carsInStock[maker][color];
		cout << setw(7) << carsInStock[maker][color];
		}

	cout << endl;
	}
//}  <-- what's this?

After reformatting, it looks OK.

Please, pay attention to how you indent your code. It is a visual cue both to yourself and to anyone else who reads it --a visual cue to help understand what the code does. Many beginner mistakes are directly caused by mis-formatted code.

Hope this helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.