My first attempt at 2-dimensional arrays is not working out so well. We are to simply initialize an array and print out the results. My problem is that the data contains text and numbers.....column header is car color, row header is car make. I am getting error for lines 19 and 20 "error C2440: 'initializing' : cannot convert from 'const char [9]' to 'int'" and I am not sure how to get around this. I have try single quotes and double quotes around the data, but the error remains....any help would be appreciated

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
#include <string>

using namespace std;

const int ROW_MAX = 6;
const int COLUMN_MAX = 5;

int carsInStock[ROW_MAX][COLUMN_MAX];

void reportheading(ofstream& outputfile);
void printInventory(int carsInStock[ROW_MAX][COLUMN_MAX], ofstream& outputfile);

int main()
{
	int carsInStock[ROW_MAX][COLUMN_MAX] = {{"In Stock", "Red", "Brown", "Black", "White", "Gray"},{"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'}};

	ofstream outputfile( "Inventory.txt" );
	if( !outputfile )
	{
		cerr << "Could not open the output file.\n";
		return -2; //Terminate the program when the outout file failed.
	}
	reportheading(outputfile);
	printInventory(carsInStock, outputfile);
}
void reportheading(ofstream& outputfile)
{
	outputfile << "\t\t\t     Cars in Stock Inventory\n";
	outputfile << "\t\t\t   Reported by Patrick Nealey\n\n";
	outputfile <<endl<<endl;

}
void printInventory(int carsInStock[ROW_MAX][COLUMN_MAX], ofstream& outputfile)
{
	outputfile << carsInStock;
}

Dani AI

Generated

As hinted, the root problem is a type mismatch and how the data are modeled. Two practical fixes work well: (A) keep the numeric table as an int matrix and store row/column labels separately as std::string; or (B) store every cell as a string (only useful if you never need numeric operations). The recommended approach is (A) — it keeps arithmetic easy and keeps text/labels where they belong.

Example layout (illustrative only — adjust sizes and values for your case):

#include <vector>
#include <string>
#include <iostream>
#include <iomanip>

std::vector<std::string> colors = {"Red","Brown","Black","White","Gray"};
std::vector<std::string> makes  = {"Ford","BMW","Volvo","Chevy"};
std::vector<std::vector<int>> stock = {
  {10,  5,  3,  2,  0},
  { 8,  3,  6,  1,  2},
  { 4,  2,  1,  7,  3}
};

Printing needs nested loops — you cannot stream an array directly. Use std::setw for alignment and iterate rows/cols; pass containers by const reference to your printer:

void printInventory(const std::vector<std::vector<int>>& stock,
                    const std::vector<std::string>& makes,
                    const std::vector<std::string>& colors,
                    std::ofstream& out);

Quick checklist to fix the original program:

  • Don’t mix text and numbers in an int array. Use std::string for text or separate label containers.
  • Avoid redeclaring the same variable name in different scopes (you had a global and then a local carsInStock).
  • Use double quotes for strings and never use single quotes for multi-character/grouped values.
  • Make sure your row/column constants match the actual initializer sizes.
    Following those steps will remove the conversion errors and make printing/manipulating the table straightforward.

Your problem proves not that you don't have an understanding of arrays but that you do not have an understanding of how objects work. Notice at the beginning of your array declaration you put 'int' but then inside ALL of the data are STRINGS, not INTEGERS. Also if your going to make them STRINGS use double quotes: single quotes is only for single characters.

Lastly, based on the array declaration I'm having much trouble understanding exactly what your array is for.

I'm also sorry if I sounded rude in my post, no rudeness intended.

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.