C++ Array [Solved]

Posts 113 of 13 · Page 1 of 1
C++ Array [Solved]
Hello I have this code
Code:
srand ( time(NULL) );
	int bad1 = rand() % 9 + 1;
	int bad2 = rand() % 9 + 1;
	int bad3 = rand() % 9 + 1;
	int position = rand() % 9 + 1;
	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	table1[position] = position;
	table2[bad1] = bad1;
	table3[bad2] = bad2;
	table4[bad3] = bad3;
But I prefer to use chars for example

Code:
table1[position] = "D";
But that gives me an error.. any help to use char on a int array?
"D" isn't a char. 'D' is. Double quotes mean a char array (string), regardless of whether there is only 1 char in the array.
So should be
Code:
table2[bad1] = 'D';
or ? didnt understand some wordscause im not english.. can you explain?
Quote Originally Posted by alvaritos View Post
So should be
Code:
table2[bad1] = 'D';
or ? didnt understand some wordscause im not english.. can you explain?
Yes. That's right.
Code:
	srand ( time(NULL) );
	char *num;
	int bad1 = rand() % 10;
	int bad2 = rand() % 9 + 1;
	int bad3 = rand() % 9 + 1;
	int position = rand() % 9 + 1;
	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	table1[position] = position;
	table2[bad1] = 'D';
	table3[bad2] = bad2;
	table4[bad3] = bad3;
Using that code just gives me all numbers and the letter 'D' becomes a big number ex : 68 ...
Quote Originally Posted by alvaritos View Post
Code:
	srand ( time(NULL) );
	char *num;
	int bad1 = rand() % 10;
	int bad2 = rand() % 9 + 1;
	int bad3 = rand() % 9 + 1;
	int position = rand() % 9 + 1;
	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	table1[position] = position;
	table2[bad1] = 'D';
	table3[bad2] = bad2;
	table4[bad3] = bad3;
Using that code just gives me all numbers and the letter 'D' becomes a big number ex : 68 ...
Yea, because that's the ascii value of that char.
How I convert it to letter?
Quote Originally Posted by alvaritos View Post
How I convert it to letter?
Use the char data type instead of int.

e.g; [highlight=c++]char lalala = 'D';[/highlight]
Code:
srand ( time(NULL) );
	char num = 'D';
	int bad1 = rand() % 10;
	int bad2 = rand() % 9 + 1;
	int bad3 = rand() % 9 + 1;
	int position = rand() % 9 + 1;
	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	table1[position] = position;
	table2[bad1] = num;
	table3[bad2] = bad2;
	table4[bad3] = bad3;
Continue getting number.

Code:
srand ( time(NULL) );
	char num = 'D';
	int bad1 = rand() % 10;
	int bad2 = rand() % 9 + 1;
	int bad3 = rand() % 9 + 1;
	int position = rand() % 9 + 1;
	int table1[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table2[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table3[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	int table4[] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
	table1[position] = position;
	table2[bad1] = num;
	table3[bad2] = bad2;
	table4[bad3] = bad3;
Continue getting number.
Make your arrays char arrays then, or typecast the elements.
Ok, solved thanks
/Marked Solved.
Posts 113 of 13 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?