0

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!!!!

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Greywolf333
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.