0

Here's my code

#include <cs50.h>
#include <stdio.h>
#include <math.h>
//1:26:36 / 2:24:59
int main(void)
{
    long n;
    int exc;
    do
    {
        n = get_long("Enter your CC number \n");
    }
    while(n <= 0);
    long ganjil = n;
    long card = n;
    long card1 = n;
    long mastercard = n;
    long americanex = n;
    int digit = 0;
    int jeven;
    int digitganjil = 0;
    while (digit < 8)
    {
        if(digit < 1)
        {
            n = n / 10;
            exc = (n % 10) * 2;
            if(exc >= 10)
            {
                jeven = exc % 10 + jeven;
                exc = exc / 10;
                jeven = exc % 10 + jeven;
                exc = exc / 10;
            }
            else
            {
                jeven = (n % 10) * 2;
            }
            digit++;
        }
        else if(digit >= 1)
        {
            n = (n / 100);
            exc = (n % 10) * 2;
            if(exc >= 10)
            {
                jeven = exc % 10 + jeven;
                exc = exc / 10;
                jeven = exc % 10 + jeven;
            }
            else
            {
                jeven = (n % 10) * 2 + jeven;
            }
            digit++;
        }
    }
    while (digitganjil < 8)
    {
        if(digitganjil < 1)
        {
            jeven = (ganjil % 10) + jeven;
            digitganjil++;
        }
        else if(digitganjil >= 1)
        {
            ganjil = ganjil / 100;
            jeven = (ganjil % 10) + jeven;
            digitganjil++;
        }
    }
    jeven = jeven % 10;
    if(jeven == 0)
    {
        mastercard = (mastercard / 10000000000000) % 100;
        card1 = (card1 / 1000000000000) % 10;
        americanex = (americanex / 10000000000000) % 100;
        if(n == 4)
        {
            printf("VISA\n");
        }
        else if(card1 == 4)
        {
            printf("VISA\n");
        }
        else if(mastercard == 51 || mastercard == 52 || mastercard == 53 || mastercard == 54 || mastercard == 55)
        {
            printf("MASTERCARD\n");
        }
        else if(americanex == 34 || americanex == 37)
        {
            printf("AMEX\n");
        }
        else 
        {
            printf("INVALID\n");
        }
    }
    else
    {
        printf("INVALID\n");
    }
}

and here is the output

~/ $ make credit
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    credit.c  -lcrypt -lcs50 -lm -o credit
~/ $ ./credit
Enter your CC number 
4012888888881881
INVALID
~/ $ debug50 credit
Enter your CC number 
4012888888881881
VISA

i already reloaded the tab and the browser but it keeps showing different outputs Can anyone help me?

1 Answer 1

1

Variables may (will?) be assigned different memory locations when run under debug50. This could cause different results when a variable is declared and not intialized. When it is used in, say, an arithmetic expression, the memory assigned to it may have a value and that will corrupt the result.

It would be helpful to know which "INVALID" message is printing. Perhaps (temporarily) change the printf to get eyes on it.

I will expand on the answer if these "hints" don't help you make progress.

2
  • Hi, thanks for responding. I've found out that the problem was at "int jeven;" because I didn't initialize it to " = 0". After I initialize it, the program works consistently.
    – BH_ken
    Jan 2, 2022 at 14:25
  • by the way, still appreciate the hints that you gave. thanks
    – BH_ken
    Jan 2, 2022 at 14:28

You must log in to answer this question.

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