0

I am still really new to C and programming in general(just started a week ago) and I'm having difficulty with the do while loop. My code doesn't run beyond while (ccNUM < 0);.

I tried running debug50 and even though while (ccNUM < 0); is false it doesn't move on to the next link of code.

Any help will be greatly appreciated!

The code I used:

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

int main(void) {

// Get Credit Card Number from User
long ccNUM;
   do {
       ccNUM = get_long("Insert Credit Card Number: \n");
   }
       while (ccNUM < 0);

// Find number of digits in Credit Card Number
   int digits;
   for (digits=0; 0 != ccNUM; digits++) {
       ccNUM /= 10;
   }

   if (digits == 13 || digits == 15 || digits == 16) {
       return digits;
   }
  else {
     printf("INVALID\n");
  }
  
printf("%i", digits);
}

1 Answer 1

1

Of course it's moving past the while loop. It's just not doing anything that you see before it terminates. Try inputting a 3 digit number and see what it does.

The actual problem is here:

if (digits == 13 || digits == 15 || digits == 16) {
   return digits;
}

A return statement in main will immediately terminate the program. If it has a value or variable in the statement, it will return that value to the operating system. This code is returning any cc number length stored in digits that is 13, 15, or 16. If the length of the cc length is anything else, it'll run the code that follows.

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

You must log in to answer this question.

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