0

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
Please enter your name: ss
Hello ss, Please enter your credit card number: 6666663333444
Your credit card number: 874090052


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

// Prototypes
int GetPositiveInt();

// Global variables
//string S;

int main(void)
{
    //string s = "Kevin";
    printf("Please enter your name: "); //\n");
    string S = GetString();
    //printf("You have a %s credit card.", CC_BRAND);
    printf("Hello %s, Please enter your credit card number: ", S);
    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;
}

1 Answer 1

0

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.

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

2
  • Could you please elaborate further on Given all of the tests and calculations that have to be done on individual digits
    – phillipsk
    Jun 29, 2015 at 0:12
  • As I recall, you have to add every odd digit, or even digit, etc. You should really go back and read the hacker assignment very, very closely, particularly the part about validating cc numbers.
    – Cliff B
    Jun 29, 2015 at 0:36

You must log in to answer this question.

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