0

i'm new to programming so forgive me if I ask a ridiculous question. I'll be asking plenty of questions, but here is my problem with credit. I run my code, and it prompts me for a credit card number as it should, but when I input the credit card number it breaks out of the do-while loop and keeps accepting input rather than continuing with the program. Please try the code yourself to see what I mean. What did I do wrong? why is my code behaving this way. Please explain.

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

int main(void)
{
long long cc_number;
do
{
printf("Credit Card Number: ");
cc_number = get_long_long();
}
while (cc_number < 0);

int count = 0;

long long copy;
copy = cc_number;

while (cc_number > 0)
{
   copy = copy / 10;
    count++;
}

long long cc_number_copy;
cc_number_copy = cc_number;

int digits[count];

for (int i = 0; i < count; i++)
{
digits[i] = cc_number_copy % 10;
cc_number_copy = cc_number_copy / 10;
printf(" %i\n", digits[i]);
}



}
0

Actually, no, it isn't accepting input. However, it is in an infinite loop. It only appears that it's waiting for input because the OS is letting the user type data and hit enter. Look at the following code:

while (cc_number > 0)
{
   copy = copy / 10;
    count++;
}

This loop will either never execute because cc_number was <= 0 at the start, or will run forever because cc_number starts > 10 and is never altered within the loop. Since cc_number is never changed, there's never anything to cause the loop to stop, thus infinite loop.

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

| improve this answer | |

You must log in to answer this question.

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