# credit.c code review

How come long long cannot handle this input? Further how is the arbitrary output computed?

What is the appropriate relational operator for `<>` a.k.a. within or between two values i.e. I am looking to only accept a valid credit card number while credit cards have a maximum of 19 and minimum of 13 digits.

Any comments on whether I am heading in the right direction and or guidance for the next step?

``````~/Dropbox/cs50/pset/1\$ clang-3.5 credit.c -lcs50 -o credit;./credit

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

// Prototypes
int GetPositiveInt();

// Global variables
//string S;

int main(void)
{
//string s = "Kevin";
string S = GetString();
//printf("You have a %s credit card.", CC_BRAND);
long long CC_NUM = GetPositiveInt();
printf("Your credit card number: %lli\n", CC_NUM);
}

int GetPositiveInt(long long CC_NUM)
{
//long long CC_NUM;
//string S;
do
{
CC_NUM = GetLongLong();
}
while (CC_NUM <= 19 && CC_NUM >= 13);
return CC_NUM;
}
``````

The first immediate issue that I see with your code is here:

``````while (CC_NUM <= 19 && CC_NUM >= 13);
``````

You wanted to test the length of the number, but this only tests the value contained in CC_NUM. It will never exit the loop unless the value of CC_NUM is a number between 13 and 19 inclusive. You need code that gets the number of digits and tests the length.

RE: testing whether a value is between two other values. There's no shorter way than what you have. You must make two separate tests, comparing the test value to the lower limit && testing against the upper limit, and combine them. You did it right.

There are a number of different apporaches to this problem. Given all of the tests and calculations that have to be done on individual digits, and the problems with numbers having lots of digits, perhaps you should consider taking the cc number in as a string and processing as characters, converting them individually to integers as needed? Also, go back and read the hacker assignment again. They often serve as roadmaps to the solution.

• Could you please elaborate further on `Given all of the tests and calculations that have to be done on individual digits` Jun 29, 2015 at 0:12