isbn problem (barcodes on books)

This is a discussion on isbn problem (barcodes on books) within the C Programming forums, part of the General Programming Boards category; does anyone know how i can be able to do operations on individual characters from a users input ?? lets ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    isbn problem (barcodes on books)

    does anyone know how i can be able to do operations on individual characters from a users input ??

    lets say user typed in : 0-8797-0658-9

    (isbn problem)
    i want to multiply 8 with 2, 7 with 3, 9 with 4, and so on ..
    can anyone help me out get those individual numbers from the string so i can do some operations?? thanx

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by djxtremor
    does anyone know how i can be able to do operations on individual characters from a users input ??

    lets say user typed in : 0-8797-0658-9

    (isbn problem)
    i want to multiply 8 with 2, 7 with 3, 9 with 4, and so on ..
    can anyone help me out get those individual numbers from the string so i can do some operations?? thanx
    Another idea:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    void f1(const char *buff)
    {
       int result;
       char s[5] = {0};
    
       strncpy(s, &buff[2], 4);           /* copy the second field to s[] */
       result = (int)strtol(s, NULL, 10); /* convert s[] text to int */
       printf("result = %d\n", result);
    
       strncpy(s, &buff[7], 4);           /* copy the third field to s[] */
       result = (int)strtol(s, NULL, 10); /* convert s[] text to int */
       printf("result = %d\n", result);
    }
    
    int main(void)
    {
       const char isbn[] = "0-8797-0658-9";
       f1(isbn);
       return(0);
    }
    
    /* my output
    result = 8797
    result = 658
    */

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Another idea:
    This doesn't do what the original poster wants. They want this basic formulae:

    string[x] * n;

    Where n increments once with each place in.

    Thus, if our string were:

    1357

    It would do:

    value = 1 * 2
    value = 3 * 3
    value = 5 * 4
    value = 7 * 5

    Where 'value' is the resulting value of the multiplication operation for that cell in the array / string.
    Where the value you're multiplying by increments once for each place further along in the string.

    So in essence it simply boils down do:

    x = 0, n = 2;
    string[x++] * n++;

    Your example just turns the string form of the number into its literal integral value. As usual, Salem's answer was what they were looking for.

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

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    5
    thanx for the help guys ..sorry bout that salem .. i thought both of them were the same thing (buff[i] *2 = ((buff[i] -'0') *2).. but i tried your suggestion and it worked .. thanx so much ..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM

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