Hot answers tagged

20 votes
Accepted

Finding the nth digit for credit card number

An array is not strictly required to solve this. You already have the mechanism to extract the last digit from a number. You just need to extend this by using use division to "shift" the digits the ...
user avatar
  • 2,625
10 votes
Accepted

Why do I get the "invalid operands to binary expression" error?

The remainder operator (otherwise known as the modulo operator) % is a binary operator (i.e., takes exactly 2 operands) and operates only on integer types (e.g., short, int, long, long long, etc). It ...
user avatar
  • 17.4k
7 votes

How to convert the digits of a number into elements of array?

If you want to store each digit of a number as an element of an array you can do the following. long long number = 1234567890; int digits[10]; for (int i = 0; i < 10; i++) { digits[i] = ...
user avatar
  • 1,359
6 votes

Credit GetLongLong Issue

No, you're using GetLongLong correctly not printing it correctly to stdout. "%llo" prints it in octal (i.e., base 8) instead of decimal (see more here https://www.cs.colostate.edu/~cs157/Fall15/Printf)...
user avatar
  • 109
5 votes

Finding the nth digit for credit card number

You can use arithmetic to loop through the numbers. In your example: 12345686868 % 10 = 8 which leads on to 12345686868 / 10 = 1234568686 so 1234568686 % 10 = 6 You can carry this on to access ...
user avatar
  • 18.3k
5 votes

Finding the products' digit and adding them individually

Here's some pseudo code: int accumulator = 0 for each digit in the card number { if the digit is at an odd position (position is not divisible by 2) { int factor = multiply the digit ...
user avatar
  • 2,625
5 votes

pset1 credit, need advice

You could take the number as a string, and access the individual digits as characters (subtract '0' or 48 to obtain their value), or parse the number as a long long int and use for example / and % in ...
user avatar
  • 20.7k
4 votes

credit.c project in hacker1 I keep having this error?

% operator is used to perform binary operation on integer or integer-like operands. It is used like this: int i,j; input i , j i = i % j; print i What you are trying to achieve is something similar ...
user avatar
  • 3,316
4 votes

credit - Why this AMEX and MASTERCARD numbers are considered invalid?

Found it: American Express starts with 34 or 37, Master Card starts with 51, 52, 53, 54 or 55. This makes those two numbers INVALID despite good checksum :)
user avatar
  • 153
3 votes

Pset 1 Credit Card ( help needed )

Good job reaching that point and giving a shot to the hacker pset too! Don't be dismayed if you don't succeed with the first try, keep trying! Now to your question. First of all when you declare a ...
user avatar
  • 7,326
3 votes

Finding the products' digit and adding them individually

You can use int number_of_digits = (int)log10(1234567) + 1; to get the position of the number of digits of your number. Or use the command int first_digit = (int)(1234567 / 1000000); int ...
user avatar
  • 1,156
2 votes

Why Does credit Fail Passing check50 Tests?

It can sometimes happen that a program works fine locally but fails check50. One common cause of this is using uninitialized variables and/or arrays. The block of memory grabbed locally may be ...
user avatar
  • 18.3k
2 votes

credit.c frustration

1- converting longlong to int causes imprecision since int store 4bytes(32bits) and longlong store 8bytes(64bits) of intger values so you don't need int or float(for floating point numbers) which both ...
user avatar
2 votes

pset1 credit, need advice

You need to play around with the modulus operator for a bit. I don't want to give away too much, but when I did it, I used the truncation of the operator to my advantage ;)
user avatar
  • 184
2 votes

long long to array, how did you do?

You can use a loop. As long as the number is not zero, process its last digit (%10), for example storing it in an array, then divide the number by 10, shifting decimal digits to the right. If you ...
user avatar
  • 20.7k
2 votes
Accepted

Credit.c is my nemesis

Your counting loop destroys the number. Destroy a copy instead. And stack variables are not initialised by default, so you end up with some arbitrary data as the array isn't filled at all due to your ...
user avatar
  • 20.7k
2 votes
Accepted

I have problems applying Credit in python

If you were to print(warehouse) after this line warehouse = ((c_number / 10)%10)*2 you'd discover that it returns a floating point number. Python doesn't do integer division like C unless you use ...
user avatar
  • 18.3k
2 votes
Accepted

Credit - Why Does it Fail Passing check50?

I feel your frustration, but you're looking in the wrong place. The number may pass the checksum calculation, but that isn't the problem. The number is only 10 digits. It should be either 13 or 16 ...
user avatar
  • 63.1k
2 votes
Accepted

pset1 | error: invalid operands to binary expression ('long long *' and 'int')

FindLength is a pointer, not a long long integer. This means that the code is trying to divide an address by an integer. FindLength = FindLength / 10; In this statement, FindLength contains the ...
user avatar
  • 63.1k
2 votes
Accepted

PSET1 - Credit HELP

Your algorithm is calculating the correct result. 74 is the octal representation of decimal number 60 (octal uses 8 as base while decimal uses 10). Use ld, not lo to print a long. 7 * 8^1 + 4 * 8^0 = ...
user avatar
  • 20.7k
2 votes
Accepted

problem with pset6 python credit

One thing first, and has higher precedence than or. A or B and C is same as A or (B and C). Keep that in mind when combining the two. In Python, pretty much like in C, firstTwo == 51 or 52 or 53 or ...
user avatar
  • 20.7k
2 votes

error at while loop cs50 Pset1 credit

The compiler is actually giving a warning, but the compiler flags are set up to treat all warnings as errors. It's still best to eliminate all warnings. Here's the reason. For clarity, let's ...
user avatar
  • 63.1k
2 votes
Accepted

Can't use get_long_long();

Does cs50.h have a function called get_long_long?. There is no function by that name listed in the CS50 Reference. And notice this from the spec: Now, get_long itself will reject hyphens (and more) ...
user avatar
2 votes
Accepted

Pset6 credit python Problem

The function get_int returns an integer and therefore if you wish to access each character by its corresponding index consider first casting it to a string.
user avatar
2 votes
Accepted

Why is this while loop generating errors

Assuming the error you are getting is not related to a linking error, which is common, then the error I assume you are getting is loop_check.c:14:15: warning: '&&' within '||' [-Wlogical-...
user avatar
2 votes
Accepted

PSET1 - Credit: Check50 says wrong what should be right

4111111111111113 isn't a valid card number because it doesn't come out correctly with the Luhn algorithm. It looks like you've got the part of the problem where you check the card's company, but it ...
user avatar
  • 699
2 votes
Accepted

I keep getting "expected EOF" when I use check50

When the code is run with the test data that check50 uses, 4111111111111113, it prints INVALID twice. check50 was expecting the program to end, not to print the second INVALID. The problem is obvious ...
user avatar
  • 63.1k
1 vote

style improvements suggestions for credit pset1

You can condense your switch statements where the cases all return the same output, for example: switch (second_digit) { case 1: case 2: case 3: case 4: case 5: printf("...
user avatar
1 vote

pset 1 credit.c invalid cards are valid by cs50

What about this 5105105105105100? 1*2 + 5*2 + 0*2 + 1*2 + 5*2 + 0*2 + 1*2 + 0*2 2 + 10 + 0 + 2 + 10 + 0 + 2 + 0 2 + 1 + 0 + 2 + 1 + 0 + 0 + 2 + 0 = 8 8 + 5 + 0 + 1 + 5 + 0 + 1 + 5 + 0 = 25 The ...
user avatar
1 vote
Accepted

pset 1 credit.c invalid cards are valid by cs50

371449635398431 is a valid AMEX card. From this post: 3 7 1 4 4 9 6 3 5 3 9 8 4 3 1 Okay, let’s multiply each of the underlined bold digits by 2 7*2 + 4*2 + 9*2 + 3*2 + 3*2 + 8*2 + 3*...
user avatar

Only top scored, non community-wiki answers of a minimum length are eligible