Converting Char Array Loop values to integer

This is a discussion on Converting Char Array Loop values to integer within the C Programming forums, part of the General Programming Boards category; How can I convert the Char array loop values to integers and assign to the interger array? Code: // write ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    13

    Converting Char Array Loop values to integer

    How can I convert the Char array loop values to integers and assign to the interger array?

    Code:
    // write to the procInput array 
    			if( atoi(processes[loop]) >= 1 && atoi(processes[loop]) <=9 )
    			{
    				procInput[x] = processes[loop]; 
    				x++; 
    			}


    procInput is an integer arry and the processes is the char array

    Thanks,
    AzamSharp

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not going to teach you how to think. You'll have to figure out how to do that on your own. You already have the answer right in front of you. Look very closely at your if check. Then go look that up, and see if you can't figure out how to do something with it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Thanks I will check it out.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Heh, bit of a logical error there.

    A good way to do error checking in your program is to output text at various points. Put it into if statements and loops, to see what outputs and how many times.

    As quzah said, look at your if statement conditions.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Hi,

    Well in the documentation it says that atoi takes in character constant. Dont know what is that suppoed to mean I will do some test today and see what happens.

  6. #6
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    what is processes[loop]? is it a char or a char*? If it's a char that is a number, just subtract '0' from it, if it's a char* then give it to atoi()

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Hi,

    well processes is a character array. like this
    Code:
    char processes[10]; 
    
    for(loop = 0; loop<=10; loop++) 
    	{
    		if(processes[loop] != ',' && processes[loop] != ';') 
    		{
    			y = processes[loop]; 
    			printf("The value of y is %d",y); 
    
    
    
    
    
    			
    			//procList[x] = processes[loop]; 
    			//x++; 
    		}
    	}

  8. #8
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    so if process = "0,1,2,3", then to get the int value of 1 from item 2 in the array, you would say:

    Code:
    int n = process[2] - '0';

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    13
    Thanks that did the trick

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21