Thread: Need Help

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    8

    Need Help

    Hi I need help starting this program
    The program check to see if a credit card is valid or not
    the number is 16 digits long
    EX: Card Number 1234567890123456
    Code:
    1. Double the value of alternating digits of the credit card number
    1  2   3   4    5   6   7    8   9    0   1   2    3   4     5   6
       x2       x2       x2       x2        x2      x2       x2       x2
    --------------------------------------------------------------------
    1  4   3   8    5   12   7  16  9   0   1   4    3    8    5   12
    
    2. Add all the digits
    1 + 4 + 3 + 8 + 5 + 12 + 7 + 16 + 9 + 0 + 1 + 4 + 3 + 8 + 5 + 12 =98
    
    3. Divide the sum by 10
    98/10=9.8
    If the number is evenly divisible then it is valid
    If it is not evenly divisible than it is invalud

    thanks for the Help Joe

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Post some code attempts and we'll be glad to help.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    8
    I want to enter it in as a string then convert it to a integer but how do I do that

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Traverse the string, then add every element in it to the total sum. Note that '0' == 48, '1' == 49, '2' == 50, etc...
    Code:
    #define FirstDigit 48
    
    int SumNumbersInString(char* String)
    {
       int Sum = 0;
       if(String != NULL)
       {
          for(int i=0; i<strlen(String); i++)
          {
             Sum += (String[i] - FirstDigit);
          }
       }
       return Sum;
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Do you mean a char * or an ansi string?

  6. #6
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Never mind

    Magos did the char * way in case you're wondering.
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed