Parsing the content of string array into an integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alien
    New Member
    • Sep 2007
    • 61

    Parsing the content of string array into an integer

    Hi guys,

    I am trying to create to parse the content of a char array into integer as follows but I cant do it and keep getting the following error. Any solution or suggestion is welcome.

    *************** *************** *************
    #include <stdio.h>
    #include <ctype.h>

    int main()
    {
    char isbn[20];
    int weight[10] = {10,9,8,7,6,5,4 ,3,2,1};

    int numericisbn[20];

    int product;
    int sum = 0;


    printf("Enter your ISBN number: ");
    fgets(isbn, 20, stdin);

    int i;
    for (i = 0; i < strlen(isbn); i++)
    {

    numericisbn[i] = atoi(isbn[i]);

    }
    printf("%d\n", numericisbn[0]);
    printf("%d\n", numericisbn[1]);
    printf("%d\n", numericisbn[3]);
    }


    *************** *************** *************
    The code compiles and the error message I get is:

    Segmentation fault(core dumped)



    Any help or suggestion is appreciated.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Alien
    error: parse error at end of input

    Any help or suggestion is appreciated.
    That is a compilation error; most likely you're missing a right curly brace near
    the end of your code. Check them carefully and you'll find the missing one.

    The atoi() function takes a '\0' terminated string and returns the int represented
    by that string; it shouldn't be a problem if your array contains just digits.

    kind regards,

    Jos

    Comment

    • Alien
      New Member
      • Sep 2007
      • 61

      #3
      Originally posted by JosAH
      That is a compilation error; most likely you're missing a right curly brace near
      the end of your code. Check them carefully and you'll find the missing one.

      The atoi() function takes a '\0' terminated string and returns the int represented
      by that string; it shouldn't be a problem if your array contains just digits.

      kind regards,

      Jos

      Yes, it my code was missing closing curly braces of main() function.

      The problem I had was parseing error which after fixing the code is:

      string.c: In function `main':
      string.c:22: error: parse error before '{' token
      string.c: At top level:
      string.c:26: error: parse error before '}' token

      My array is meant to take numbers and dashes "-" which is why I am checking (or rather trying to) each element of the array and sort it into another array of type int (called numericisbn).

      The article is about ISBN of books. As you know it comes with dashes alongside numbers, I am basically trying to filterout the dashes.

      Regards.

      Alien.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Alien
        Yes, it my code was missing closing curly braces of main() function.

        The problem I had was parseing error which after fixing the code is:

        string.c: In function `main':
        string.c:22: error: parse error before '{' token
        string.c: At top level:
        string.c:26: error: parse error before '}' token
        Told you so ;-) We'd have to see the whole code if you want those parse errors
        solved. Seeing the error diagnostic messages it has to do with curly brackets
        that aren't balanced; same as the previous errors.

        kind regards,

        Jos

        Comment

        • Alien
          New Member
          • Sep 2007
          • 61

          #5
          Originally posted by JosAH
          Told you so ;-) We'd have to see the whole code if you want those parse errors
          solved. Seeing the error diagnostic messages it has to do with curly brackets
          that aren't balanced; same as the previous errors.

          kind regards,

          Jos
          Thanks. :) The code is just above.

          Thanks for going through the trouble.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Alien
            Thanks. :) The code is just above.

            Thanks for going through the trouble.
            Huh? you edited your original post: see your text and the part I thought I quoted.
            Now you're talking about a runtime error: you're invoking the atoi() function on a
            single char; that is not correct; atoi() takes a pointer to char(s) as its parameter,
            terminated by a '\0' char.

            kind regards,

            Jos

            Comment

            • Alien
              New Member
              • Sep 2007
              • 61

              #7
              Originally posted by JosAH
              Huh? you edited your original post: see your text and the part I thought I quoted.
              Now you're talking about a runtime error: you're invoking the atoi() function on a
              single char; that is not correct; atoi() takes a pointer to char(s) as its parameter,
              terminated by a '\0' char.

              kind regards,

              Jos
              Since atoi() wont work, is there any other functions that you recommend? I am fairly new to the language and there isn't many that i know of that's likely to do the jobs for me.

              Thanks and regards.

              Comment

              • Alien
                New Member
                • Sep 2007
                • 61

                #8
                Ok, I got down to here:

                *************** *************** *************** ********
                for (i = 0; i < strlen(isbn); i++)
                {
                if(isdigit(isbn[i]))
                {
                numericisbn[i] = isbn[i];

                puts("isdigit is working\n");
                }
                }
                printf("These are the contents of numericisbn: ");
                printf("%d\n", numericisbn[0]);
                printf("%d\n", numericisbn[1]);
                printf("%d\n", numericisbn[2]);
                printf("%d\n", numericisbn[3]);
                printf("%d\n", numericisbn[4]);
                printf("%d\n", numericisbn[5]);


                The output is:

                Enter your ISBN number: 12s2
                isdigit is working

                isdigit is working

                isdigit is working

                These are the contents of numericisbn: 49
                50
                0
                50
                0
                0
                0
                0
                0


                *************** *************** *************** ********

                All is good as expected. When I put the "s" amonst the number, the isdigit() function sorted it out and put a 0 in place.

                Now those values I get for 1 2 and 2 are ASCII equivalent. Can someone tell me how to convert those ASCII to proper ints?

                The issue obviously lies in the line bolded above. Since atoi() wont work I have exhausted all my knowledge on this. Any ideas anyone?

                Regards.


                BTW, the reason why you see all the 0s as the output of printf() is because i ran a for loop before the code above which clears the numericisbn[10] of all its memory addresses and puts 0 inplace.

                Comment

                • mohammadazim
                  New Member
                  • Aug 2007
                  • 28

                  #9
                  In your loop you can do following changes

                  for (i = 0; i < strlen(isbn); i++)
                  {
                  if(isdigit(isbn[i]))
                  {
                  char s[2];
                  s[1] = '\0';
                  s[0] = isbn[i];
                  numericisbn[i] = atoi(s);
                  printf("%d", numericisbn[i]);
                  puts("isdigit is working\n");
                  }
                  }

                  Since we have only one api(atoi) which converts integer strings to inytegers, it is better to make it a temp string and get an int.

                  Comment

                  • Alien
                    New Member
                    • Sep 2007
                    • 61

                    #10
                    Originally posted by mohammadazim
                    In your loop you can do following changes

                    for (i = 0; i < strlen(isbn); i++)
                    {
                    if(isdigit(isbn[i]))
                    {
                    char s[2];
                    s[1] = '\0';
                    s[0] = isbn[i];
                    numericisbn[i] = atoi(s);
                    printf("%d", numericisbn[i]);
                    puts("isdigit is working\n");
                    }
                    }

                    Since we have only one api(atoi) which converts integer strings to inytegers, it is better to make it a temp string and get an int.
                    Thanks a lot mate. I'll try the solution above when i get a chance.

                    Regards.

                    Comment

                    • Alien
                      New Member
                      • Sep 2007
                      • 61

                      #11
                      Yep it work.

                      Once again thank you very much.

                      Comment

                      Working...