Ey all.

Im folowing a c++ course and im stuck writing string or char arrays to matrices.

The problem is i have a matrix of size [j] with random values from 0 to 3.
Each of these values have to correspond to either a string or char array.

0=EMPTY
1=RES
2=SUS
3=KILL

Now i want to know if it is possible to either make use of enumarations since they already correspond to integer values like.


enum CELLTYPE {EMPTY, RES,SUS, KILL}

and then seed them in matrix randomly put i dont know how to do this.

I think it's much easier to just first seed matrix with 0 to 4 which i know how to do.
But then i cant convert the values to string or char. gives me error cant convert int to char if i do it like this.

char usergrid(int rows, int col, int world[10][10])
{
	cout << endl;
	
	char EMPTY[] = "EMPTY";
	char SUS[] = "SUS";
	//string RES = "RES";
	//string KILL = "KILL";

	for(int i=0; i < rows ; ++i)
	{
		for(int j=0; j < col; ++j)
		{
			if (world[i][j] == 0)
				world[i][j] = EMPTY;
			else if  (world[i][j] == 1)
				world[i][j] = SUS;
			else if (world[i][j] == 2)
				//world[i][j] = RES;
			else if (world[i][j] == 3)
				//world[i][j] = KILL;
		}
	}
		for(int i=0; i < rows ; ++i)
	{
		for(int j=0; j < col; ++j)
			cout << world[i][j] << "\t";
		cout << endl;
	}

	return world[10][10];
}

plz help!!!!

Dani AI

Generated

— the root issue is a type mismatch: you can't store text in an array declared for integers, and the function in your snippet also has an incorrect return (it returns a single char but tries to return the whole matrix). 's suggestion to use a 2D string array is the simplest fix, but there are two common, safe patterns depending on whether you want to keep numeric data or store labels directly.

Keep the numeric grid and map to labels at print-time (low memory, easy to use enums). Example: create a small lookup and use it when printing. Always check indexes before you use them.

const char* labels[] = {"EMPTY", "RES", "SUS", "KILL"};
for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < cols; ++j) {
        int v = world[i][j];
        if (v >= 0 && v < 4) std::cout << labels[v] << '\t';
        else std::cout << "?" << '\t';
    }
    std::cout << '\n';
}

Or store strings directly in a container (easier when you want to move/modify labels). Use std::vector and std::string and a modern RNG for seeding:

std::vector<std::vector<std::string>> grid(rows, std::vector<std::string>(cols));
std::array<std::string,4> names = {"EMPTY","RES","SUS","KILL"};
std::mt19937 rng{std::random_device{}()};
std::uniform_int_distribution<int> d(0,3);

for (int i=0;i<rows;++i)
  for (int j=0;j<cols;++j)
    grid[i][j] = names[d(rng)];

Extra tips: prefer std::string and standard containers over raw C arrays, make functions void or return a container (don’t try to return world[10][10]), pass large data by (const) reference, and validate indices before using them. If you use enums, map them to printable names at output (or keep the enum-backed ints and use the lookup above).

Ey all.

Im folowing a c++ course and im stuck writing string or char arrays to matrices.

The problem is i have a matrix of size [j] with random values from 0 to 3.
Each of these values have to correspond to either a string or char array.

0=EMPTY
1=RES
2=SUS
3=KILL

Now i want to know if it is possible to either make use of enumarations since they already correspond to integer values like.


enum CELLTYPE {EMPTY, RES,SUS, KILL}

and then seed them in matrix randomly put i dont know how to do this.

I think it's much easier to just first seed matrix with 0 to 4 which i know how to do.
But then i cant convert the values to string or char. gives me error cant convert int to char if i do it like this.

char usergrid(int rows, int col, int world[10][10])
{
	cout << endl;
	
	char EMPTY[] = "EMPTY";
	char SUS[] = "SUS";
	//string RES = "RES";
	//string KILL = "KILL";

	for(int i=0; i < rows ; ++i)
	{
		for(int j=0; j < col; ++j)
		{
			if (world[i][j] == 0)
				world[i][j] = EMPTY;
			else if  (world[i][j] == 1)
				world[i][j] = SUS;
			else if (world[i][j] == 2)
				//world[i][j] = RES;
			else if (world[i][j] == 3)
				//world[i][j] = KILL;
		}
	}
		for(int i=0; i < rows ; ++i)
	{
		for(int j=0; j < col; ++j)
			cout << world[i][j] << "\t";
		cout << endl;
	}

	return world[10][10];
}

plz help!!!!

Use a 2D array of strings instead of integers, and populate it with the proper text as you generate the random numbers

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.