Ok, so I thought I was done with my program, but it seems I have another error, which, for the life of me, I cannot find out why!
I have this following code:
1 2 3 4 5 6 7 8 9
TCHAR *keyTchar = new TCHAR[ App::szKey ];
int *key = newint[ App::szKey ];
//NULLify the key variables
for( int i = 0; i < App::szKey; ++i )
{
key[ i ] = NULL;
keyTchar[ i ] = NULL;
}
//create a key( digit ) counter
int keyCount = 0;
//convert from TCHAR to int
for( int i = 0; i < App::szKey; ++i )
{
if( keyTchar[ i ] == NULL )
break;
key[ i ] = _ttoi( &keyTchar[ i ] );
++keyCount;
}
//create an index counter
int index = keyCount;
////////Break the key in to single digits/////////
for( int i = 0; i < index; ++i )
{
for( int j = 0; j < ( keyCount - 1 ); ++j )
key[ i ] /= 10;
--keyCount;
}
//////////////////////////////////////////////////
Which will take a key from TCHAR and convert it to an int. This works fine, until I type a message that generates 10 or more keys.
Ok, so I found out why it was doing it! I should have done a debug before posting this, but I still need help with it anyway.
When I'm converting to int:
1 2 3 4 5 6 7 8 9 10
//convert from TCHAR to int
for( int i = 0; i < App::szKey; ++i )
{
if( keyTchar[ i ] == NULL )
break;
key[ i ] = _ttoi( &keyTchar[ i ] );
++keyCount;
}
I'm using _ttoi() and passing the address, which is reading all the proceeding elements of the TCHAR. By doing this, I'm hitting the top range, 2147483647.
So when it's broken down, I'm always ending with a 2, until the TCHAR can go in to the int without hitting the top range.
So, is there a way to do this without using the current address of the TCHAR, so that they are single digits?
Or is it possible to use GetDlgItemInt() and store each digit singly in to an array?
I no longer need help with this. Don't know why I never thought of this in the first place...
1 2 3 4 5
for( int i = App::szKey; i >= 0; --i )
{
if( keyTchar[ i ] != NULL )
key[ i ] = ( int )keyTchar[ i ] - 48;
}
If anyone finds themselves in a similar situation.
All I have done, is typecast the TCHAR to an int and minus 48 to get the corresponding number from the ascii table. http://www.asciitable.com/
The number 3 on the ascii table has a value of 51 when typecast to an int. -48 gives you the number you had in the TCHAR.