Home > front end >  Luhn Algorithm in C; problem debugging with atoi function
Luhn Algorithm in C; problem debugging with atoi function

Time:09-25

I'm having a problem with taking the individual characters in the string entered by the user and converting them into integers. I use XCode, and the error that pops up when I use atoi function is:

Incompatible integer to pointer conversation passing 'char' to the parameter of type 'const char *'

Then I take the address with &, and then it asks me to fix the issue by adding &. Whenever I do this, however, the debugging screen appears and I have no idea what to do from there. Can somebody show me how to properly format this?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    char cardnumber[17];

    printf("Enter your card number: ");
    scanf("\n%s",cardnumber);
    
    int c;
    for (c=0; cardnumber[c]!='\0'; c  )
    {
        if (isdigit(cardnumber[c]) == 0)
        {
            printf("Invalid Entry");
        }
    }
    
    int i=0;
    int odd1 = atoi(cardnumber[i]);
    int odd2 = atoi(cardnumber[i 2]);
    int odd3 = atoi(cardnumber[i 4]);
    int odd4 = atoi(cardnumber[i 6]);
    int odd5 = atoi(cardnumber[i 8]);
    int odd6 = atoi(cardnumber[i 10]);
    int odd7 = atoi(cardnumber[i 12]);
    int odd8 = atoi(cardnumber[i 14]);
    
    int s1 = odd1*2;
    int s2 = odd2*2;
    int s3 = odd3*2;
    int s4 = odd4*2;
    int s5 = odd5*2;
    int s6 = odd6*2;
    int s7 = odd7*2;
    int s8 = odd8*2;
    
    int t1 = ((s1/10) (s1%10));
    int t2 = ((s2/10) (s2%10));
    int t3 = ((s3/10) (s3%10));
    int t4 = ((s4/10) (s4%10));
    int t5 = ((s5/10) (s5%10));
    int t6 = ((s6/10) (s6%10));
    int t7 = ((s7/10) (s7%10));
    int t8 = ((s8/10) (s8%10));
    
    int even1 = atoi(cardnumber[i 1]);
    int even2 = atoi(cardnumber[i 3]);
    int even3 = atoi(cardnumber[i 5]);
    int even4 = atoi(cardnumber[i 7]);
    int even5 = atoi(cardnumber[i 9]);
    int even6 = atoi(cardnumber[i 11]);
    int even7 = atoi(cardnumber[i 13]);
    int checkdigit = atoi(cardnumber[i 15]);
    
    int check=((t1 t2 t3 t4 t5 t6 t7 t8 even1 even2 even3 even4 even5 even6 even7)%10);
    if(check==0)
    {
        if(check==checkdigit)
        {
            printf("Your card is accepted.");
        }
        else
        {
            printf("Invalid entry.\n");
        }
    }
    else
    {
        if(10-check==checkdigit)
        {
            printf("Your card is accepted.");
        }
        else
        {
            printf("Invalid entry.\n");
        }
    }
    return 0;
}

CodePudding user response:

Assuming your input is in ASCII, all digit characters have consecutive integer codes. So, once you tested with isdigit() all characters are actually digits, it's enough to subtract the code of character zero '0' from each digit to obtain their numerical values:

int odd1 = cardnumber[i] - '0';
int odd2 = cardnumber[i 2] - '0';

...and so on.

CodePudding user response:

atoi should be passed a pointer to the first character of a string of characters.

cardnumber is an array of char. cardnumber[i] is a char. It is not a pointer. Therefore, atoi(cardnumber[i]) does not pass a pointer to atoi.

To convert the character code for a digit to the number the digit represents, subtract the code for the character “0”:

int odd1 = cardnumber[i] - '0';

This works because the code numbers for digit characters are guarantee to be consecutive. So subtracting the code for “0” from the code for a digit yields the number the digit represents—0 for “0”, 1 for “1”, 2 for “2”, and so on.

Also be sure to test that the entered string contains only digits, and that at most 16 characters are read into cardnumber. Use s instead of %s.

  • Related