Computer Science Canada

Trying to parse a string....

Author:  blissfullydysphoric [ Wed Nov 05, 2008 8:47 pm ]
Post subject:  Trying to parse a string....

Hi, I'm trying to take a 10 digit number and take its individual parts... So I take the number as a string and I am trying to get each character in the string to go into an integer array using the atoi() command but it gives me an error on the line that i use the atoi....

code:

#include <stdio.h>

#include <math.h>
#include <stdlib.h>

int main(void)

{

    int isbn[10];
    char array1[10];
    int i;
    int temp;

    printf( "Please enter the ISBN number:\n" );
    scanf("%s",array1);
    puts(array1);
    isbn=atoi(array1);
    printf("%d",isbn[0]);
    printf("\n");

    return 0;

}


Author:  md [ Wed Nov 05, 2008 8:52 pm ]
Post subject:  RE:Trying to parse a string....

Look up the documentation on atoi, but basically you are trying to use it to do something it doesn't do.

atoi returns an integer, not an array of integers.

Author:  blissfullydysphoric [ Wed Nov 05, 2008 8:55 pm ]
Post subject:  Re: Trying to parse a string....

ok thanks.... I can get it to put all of the string into one integer but not an array..... do you have any other suggestions on how to do this?

Author:  OneOffDriveByPoster [ Wed Nov 05, 2008 9:48 pm ]
Post subject:  Re: Trying to parse a string....

blissfullydysphoric @ Wed Nov 05, 2008 8:55 pm wrote:
ok thanks.... I can get it to put all of the string into one integer but not an array..... do you have any other suggestions on how to do this?
You did say a 10 digit number. Did you try 2147483648? Why are you using atoi()? Do you have to? Standard C says that the characters '0' to '9' are consecutive values. That can help.

Author:  md [ Thu Nov 06, 2008 12:21 am ]
Post subject:  RE:Trying to parse a string....

If your goal is to copy each digit of the number into a different position in an integer array then teh simple way is to set integer_array[i] to character_array[i] - '0'

Why that is and how to use it to your advantage I leave to you.

Author:  blissfullydysphoric [ Thu Nov 06, 2008 3:19 pm ]
Post subject:  RE:Trying to parse a string....

@md
Thanks that helped alot... I tried the subtract zero thing before but i guess I needed the ' ' around it.


: