
I'm generating a random number between 0 and 9
I want to assign that number to a character so I can store it in a character array;
something like
if i = 3
c = '3';
if i =4
c = '4'
but smarter way
|
Light Poster
|
|
| 14Jul2008,18:54 | #1 |
|
Hello again
![]() I'm generating a random number between 0 and 9 I want to assign that number to a character so I can store it in a character array; something like if i = 3 c = '3'; if i =4 c = '4' but smarter way
|
|
Go4Expert Founder
|
![]() |
| 14Jul2008,19:18 | #2 |
|
Directly assigning should do the job for you.
char c = i; |
|
Light Poster
|
|
| 14Jul2008,19:33 | #3 |
|
add the ascii value to it................
|
|
Mentor
|
![]() |
| 16Jul2008,14:23 | #4 |
|
if (i>=0 && i<=9) c=i+'0';
Or just generate random numbers between '0' and '9'. |
|
Light Poster
|
|
| 17Jul2008,07:48 | #5 |
|
Thanks for the tip...yes rand()%10 + '0' works
|
|
Go4Expert Member
|
|
| 20Jul2008,13:53 | #6 |
|
use dynamic cast
or for example int a; char b; b<int>=a; |
|
Mentor
|
![]() |
| 20Jul2008,14:43 | #7 |
|
yes, but will that convert 0x00000000 to '0', which is what the OP wanted, or will it convert it to 0x00?
|