-
Converting char to int
I am writing some code that reads dates (format yyyymmdd) from a file into a 2D array. I define the array by a pointer to pointer to char. I convert the third character of the string to an integer and use this in an If statement:
Code:
for(i=0; i<50; i++)
{
SomeChar = date[i][3];
year = atoi(&SomeChar);
if(year > 4)
{
julian[i] = (i/24) -365*(year-1);
MessageBox(hwnd, TEXT("If statement"), TEXT("Warning!"), MB_OK | MB_ICONINFORMATION);
}
else julian[i] = (i/24) + 1 - 365*(year-1);
wsprintf(test, L"%d", year);
MessageBox(hwnd, test, TEXT("Warning"), MB_OK | MB_ICONINFORMATION);
}
I use the wsprintf command, because I use Unicode. If I print 'year' for all 51 entries it is always 1, because the year is always 2001. The MessageBox in the If statement does not show up. If I print 'i' it obviously prints 0, 1, ....50, but it also shows the MessageBox in the If statement at i=31. How is that possible? Whether or not the If statement is chosen, does not depend on what I print in the last MessageBox right? Furthermore, if I print 'julian[i]', the sequence printed is 1 or each hour of the first day and 2 for each hour on the second day and so on. But all the sudden, for i=18 (still the first day) the If statement is chosen and julian[i] gives -4745. How is all this possible?
-
oh, thse are the variable declarations:
Code:
LPWSTR test;
int i, year;
int *julian;
char **date;
char SomeChar;
-
atoi only works for strings, not individual characters. To convert a char to an integer, subtract '0'.
-
thanx elysia, I found the error too. Now it works fine!