Thread: Converting char to int

  1. #1
    Registered User
    Join Date
    Feb 2010

    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:

    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?

  2. #2
    Registered User
    Join Date
    Feb 2010
    oh, thse are the variable declarations:

    LPWSTR test;
    	int i, year;
            int *julian;
    	char **date;
    	char SomeChar;

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    atoi only works for strings, not individual characters. To convert a char to an integer, subtract '0'.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2010
    thanx elysia, I found the error too. Now it works fine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM