0

Also, please when I try to add "firstnumsum", it doesn't give expected value and I don't know what to do.

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

bool checksum(string checksumarray[0]);

int main() { long long cardnumber = 0, calc; //string credit[1]; //string lengthkey[1]; string credit; string lengthkey[1];

//user input
do
{
    credit = get_string("Card number: ");
}
while ((credit <= 0));
//to check if condition works
printf("%s credit\n", credit);

//the long long version of user's string input
cardnumber = strtoll(credit, NULL, 10);

//copy of long long for calculation
calc = cardnumber;

string checksumarray[1];
checksumarray[0]= credit;
//luhn's algorithm
bool checksum(string checksumarray[0]);
{
    long long /*lastdigit, reverse = 0,*/ mult, remaining, firstnumsum = 0, easier, sum_of_even = 0,  finalsum;
    int /*countcardcharacters = 0,*/ a = strlen(checksumarray[0]);
    //sum the odd digits
    for (int x = a - 1; x >= 0; x -= 2)
    {
        easier = strtoll (checksumarray[0][x], NULL, 10);
        printf("%lli checksumarray\n",easier);
        firstnumsum = firstnumsum + easier;
    }
    printf("%lli firstsum\n", firstnumsum);
    //sum of even digits
    for (int j = a - 2; j >= 0; j -=2)
    {
        printf("%c checksumarray2\n", checksumarray[0][j]);
        mult = checksumarray[0][j];
        mult *= 2;
        //mult = (checksumarray[0][j]) * 2;
        printf("%lli mult\n", mult);
        do
        {
            remainder = mult % 10;
            mult -= remainder;
            
            printf("%lli remainder\n", remainder);
            sum_of_even += remainder;
            mult /= 10;
            printf("%lli sum of even\n", sum_of_even);
        }
        while (!(mult > 0));
        
    }
    finalsum = firstnumsum + sum_of_even;
    printf("%lli final\n", finalsum);
    if (finalsum % 10 == 0)
        return true;
    else
        return false;
}
    

1 Answer 1

0

this converts it from its ASCII form

    (checksumarray[0][x] - '0')

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .