Need4Sleep (509) wrote:
|Note that chars can only hold ONE character, so your char's can only hold a value of 0-9(dont make rand go over 9 then!).|
A char is just a small int, one just has to be careful the char referred to by the int, is the one that you you think it is, and that the int isn't bigger than the limit for the char (either 127 or 255). I think this is the point for line 21 in the OP's code.
So one can do either of these equivalent:
char MySpace = 48; //space character in decimal
MySpace = ' '; //space character as an actual char
The problem you have at the moment, is if the variables height and width become larger than a char, then there will be problem. The other thing is, you assign the first 50 ascii code to the chars in the array, which includes some things you might not expect - including the bell escape char.
Variables height and width should be defined as const to avoid the magic number thing.
You could make the variables height and width chars, but this would limit the size of your array.
Need4Sleep is right, the chars need to be converted to int, so you can avoid the limiting problem.
If you just want to put a series of chars into the array, you can use a for loop like this:
for (MyChar = 'a';MyChar <='z'; MyChar++)
MyCharArray[MyChar -97] = MyChar; //ascii 97 is 'a'
Finally, try to avoid constructs like this, because you are explicitly specifying each case:
if(num==10 || num==20 || num==30 || num==40 || num==50 || num==60 || num==70 || num==80 || num==90 || num==100)
Better to do this:
if (num % 10 == 0 && num <= 100 && num > 0)
This is scalable and more elegant I think.
Hope all goes well.