# CS50 Credit - again a scope problem

I think I almost finished the credit problem set, however, I have another scope problem. I think it is really simple. After the checksum, the code is not working anymore. If I put extra braces by the while loop for the count. The count loop works, but it keeps counting the digits. it does not stop anymore.

``````#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main (void)
{
//variables
long credit;
long count = 0;
long sumb = 0;
long remain;
long tempa;
long tempb;
long result;
long sumc = 0;
long remain_new = 0;
long r;
long sum;
long remainb = 0;
long total;
long tempc;
long digits;
//Get creditcard number
do
{
credit = get_long("Number: ");
}
while (credit < 0);

//Select every other digit starting with number'second to last digit.
tempa = credit;
tempa = tempa /10;
while (tempa > 0)
{
remain = tempa % 10;
tempa = tempa /100;
// multiply every other digit by 2
remain = remain * 2;
//sum = sum + remain;
// test outcome of remain
//printf("%li\n", remain);
// seperate the digits above 9
if ( remain > 9)
{
remain_new = remain - 9;
}
else
{
sum = sum + remain;
}

sumc = sum + remain_new;
}
//test outcome of suma
printf("sum %li\n", sum);
printf("Remain_new: %li\n", remain_new);
printf("Sum of step 1: %li\n", sumc);
// Lunhs Algoritm - Sum of other digits (step 2)
tempb = credit;
while (tempb > 0)
{
remainb = tempb % 10;
tempb = tempb /100;
//printf("remainb: %li\n", remainb);
sumb = sumb + remainb;
}
{
printf("Sum of step 2: %li\n", sumb);
}
// Total of step 1 and step 1
total = (sumc + sumb);
{
printf("Total, step 1 + 2: %li\n", total);
}

// Checksum of the card is ligit
if (total % 10 != 0)
{
}

//print the right credit card company
tempc = credit;
//Calculate the sum of digits
while (tempc > 0)
{
digits = tempc / 10;
count++;
}
{
printf("Digits: %li\n", count);
}

return 0;
}

//}
//        if (count == 15)
//            {
//                printf("AMEX");
//            }
//        else if (count == 16)
//            {
//                printf("MASTERCARD");
//            }
//        else if (count == 13)
//            {
//                printf("VISA");
//            }
//        else
//        {
//            printf(" Your card is invalid");
//        }
// AMEX = 15 digits - starts with 34 or 37
// MASTERCARD = 16 digits -starts with - 51, 52 53, 54 or 55
// VISA = 13 or 16 - starts with 4
``````

Nope, not a scope problem. It's an infinite loop problem.

This is because the code has an infinite loop. Look carefully at the code:

``````//Calculate the sum of digits
while (tempc > 0)
{
digits = tempc / 10;
count++;
}
``````

The condition that stops the loop is when tempc is not greater than 0. But, tempc never changes inside the loop. So, once the loop starts, it will run forever, or at least until an overflow occurs on count.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

• Thnx. I solved the problem.
– Mark
Apr 19 '20 at 7:30