Thread: Conversion of string to integer array?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    Conversion of string to integer array?

    Hi.
    I'm trying to make a program that reads an input of a barcode number, then validates the "check digit" (the last digit) with an algorithm. I am stuck at the start. In order for the algorithm to work, the barcode must be in separate digits, not a 'number' so to speak.

    I need the input to be a char string because it may start with '0', which wouldn't count if it were an integer.

    Code:
    #include <stdio.h>#include <string.h>
    #include <ctype.h>
    
    
    #define SIZE 12
    
    
    int main() {
        char str[SIZE];
        int digit[SIZE];
    
        int i;
    
    
        
        fgets(str, 14, stdin);
    
    
        
        for(i = 0; i < 12; i++){
              str[i] = digit[i];
              }
              
        system("pause");
        return 0;
    
    
    }
    I know this is completely wrong, but it is all I have so far. I'm not worried about the algorithm or output, just the conversion of the string of numbers into an integer array.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The idea here is that you already have the individual digits, except that they encoded as characters of a character set rather than having the desired values in the range [0, 9]. The solution is to subtract '0' from each character that represents a digit, which works because it is guaranteed that the characters '0' to '9' will be contiguous and in order in the character set.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Remove "system("pause")". No idea why this is so popular, but read this > SourceForge.net: Pause console - cpwiki

    Also, if anything, you have the order wrong:
    Code:
    str[i] = digit[i]
    This will be filling the string with whatever's in digit's memory. Most likely it won't be useful data.

    And even if you are trying to make your own "strtoul" function (hint hint), you need to actually convert them into digits:
    Code:
    str[i] - '0'
    Put this in the right place, and it will subtract the ascii number 0 from the character, turning it into the actual numeral you want.

    Also note that you should probably do some kind of error checking to make sure you're actually converting a digit.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    Thank you both. From what I gathered from you two, something like this should convert the digits.

    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    
    
    #define SIZE 12
    
    
    int main() {
        char str[SIZE];
        int digit[SIZE];
        int i;
    
    
        
        fgets(str, 14, stdin);
    
    
        for(i=0; i<SIZE; i++){
                 str[i] - '0';
                 }
        
        for(i = 0; i < 12; i++){
              digit[i] = str[i];
              }
    
    
        
        
        printf("%d, %d, %d", digit[1],digit[2],digit[3]);
      
              
        getch();
        return 0;
    
    
    }
    the final printf outputs something like 50, 51, 52 every time?

    I think I have a misunderstanding of strings, or atleast unknown strings which are obtained from input. I thought say,
    Code:
    char str[81];    
            printf("Enter a string: "); /*user enters "hello"*/
            fgets(str, 81, stdin);
            
            printf("%s", str[2]); /*output would be "l"*/
    this would output "l" but it does not. what have I missed here?
    Last edited by jigamuffin; 03-19-2012 at 10:10 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This computes the digit's value, but you don't assign the result to anything:
    Code:
    str[i] - '0';
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    @jigamuffin: A string can be further divided into characters, so the type of string foo and foo[i] are different. foo[i] is a character.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    Quote Originally Posted by laserlight View Post
    This computes the digit's value, but you don't assign the result to anything:
    Code:
    str[i] - '0';
    Haha I see now. I feel silly.

    You have helped me with several of my problems laserlight. I greatly appreciate your help. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Byte Array / Integer conversion
    By AdLab in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 08:04 AM
  2. Hex String to byte array conversion
    By IfYouSaySo in forum C# Programming
    Replies: 3
    Last Post: 06-15-2006, 04:59 PM
  3. Replies: 5
    Last Post: 04-12-2006, 06:30 PM
  4. Replies: 2
    Last Post: 11-02-2003, 08:34 AM
  5. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM