Hot answers tagged

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
8 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,369
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
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,326
3 votes

hacker1 - credit - Problems on some card numbers

Multiply every other digit by 2, starting with the number's second-to-last digit, and then add those products' digits together. Given a number that consists of an odd (i.e., not divisible by 2) ...
user avatar
  • 17.4k
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.4k
2 votes

problems with long long variable type

No need to initialize card_number to 0 before you initialize it to GetLongLong(). Your variable doesn't really store a different value. Actually, you don't print your value the correct way. To print ...
user avatar
  • 17.4k
2 votes
Accepted

hacker1 mario runs properly but fails check50

Based off what I read in the pset description here, the highest columns of the pyramids should be only one # wide instead of two. So for an input of 5, the pyramid should be:
user avatar
  • 481
2 votes
Accepted

hacker1 mario: output appear correct, but won't pass check50

Looking at your output, it appears that you have an extraneous leading space on each line. For instance, the first line should be #**#\n, not *#**#\n (spaces replaced with * for clarity) If this ...
user avatar
  • 64.1k
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
Accepted

Infinite looping in hacker 1 - water

I like that you don't want to use CS50's tools and use tools that you are going to have available everywhere outside of CS50. I was reviewing your code. I tried to solve it with a fflush(stdin) line, ...
user avatar
2 votes

Infinite looping in hacker 1 - water

Yes, don't use scanf. You have a library that helps you avoid using scanf. The reason is probably related to the fact that scanf still keeps the string you input in its buffer and it won't empty it, ...
user avatar
  • 1,677
1 vote

PSET 1 Hacker Mario

I had the exact same issue. The output looks correct but it is not. Just ask yourself if you really need what you called "spaces2"
user avatar
1 vote

Credit Card Test Confusion

Taking the first one through the example in the spec: 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*2 That ...
user avatar
1 vote

What is wrong with my hacker mario.c code for pset1?

I would pick small numbers for h (e.g., 1 and 2), determine what the expected results should look like and follow the logic of my code using a pencil and a paper. for example, when h == 1, I should ...
user avatar
  • 17.4k
1 vote

Hacker1 seems to run properly, but won't pass check50

I had to change my printf function in order to make a new line after each's line last block.
user avatar
1 vote

credit.c frustration

To be frank with you, your program has serious flaws that will not allow it to work in any case. But just to answer your question: why it always prints INVALID4 It's because of this line int ...
user avatar
  • 7,326
1 vote

credit.c help too debug please

By the time you get to line 48 number[i] = creditcardnumber % 10; creditcardnumber is 0 (since you changed it in your counting the digits loop). So that array bit will simply fill with zeros. Next:...
user avatar
  • 18.4k
1 vote

Why isnt my program working? credit.c

You have multiple syntax mistakes. For example: multipliednumber[2|4|6|8|10|12|14|16] = number[2|4|6|8|10|12|14|16] * 2; is not a valid expression in C. What exactly are you trying to achieve? ...
user avatar
  • 7,326
1 vote
Accepted

Processing the separated digits using Luhn's Algorithim

If you are trying to make sure that, if you have 2 digits, you are summing them as individuals, this one line equation will handle that: c1 % 10 + c1 / 10; Examples. If c1 = 5 (ie, only one ...
user avatar
  • 18.4k
1 vote
Accepted

Infinite looping

Well len can't possibly be in the same time both 15 and 16 and 13 (which is what your condition implies). Instead use while ( len != 15 && len != 16 && len != 13); Edit Let's start ...
user avatar
  • 7,326
1 vote

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

Why not input the number as a string to begin with? (This would also make it easier to check the length is right - you seem to be comparing the size of the number, rather than its length, with 10). ...
user avatar
1 vote

errors I cant understand in credit.c can anyone explain?

A couple of other things: int i,a,b; declares a new int called i which isn't the one you are incrementing. Instead you should say for (int i = 14; etc) For what it's worth, at this stage in the ...
user avatar
  • 18.4k
1 vote

hacker1 mario runs properly but fails check50

The problem is in spaces at the second half of pyramid. I have all strings of my output the same length. But check50 wants them to end right after the last # symbol.
user avatar
1 vote
Accepted

Code Debugging: Help?

let's run through the first example: for (int i = 0; i < number_of_digits / 2; i++) { val_1 = (cc_number % mod_val)/n; agg_val_1 = agg_val_1 + val_1 * 2; mod_val = mod_val + 100; ...
user avatar
  • 18.4k
1 vote
Accepted

Why is the last digit of my credit card number wrong in hacker1?

pow() returns a double and so does floor(). Working with doubles is not the best thing to do since they're not precise. You could lose some data and we don't really have to work with them at all here. ...
user avatar
  • 17.4k
1 vote
Accepted

hacker1 - credit - Problems on some card numbers

Multiply every other digit by 2, starting with the number's second-to-last digit, and then add those products' digits together.
user avatar
  • 26

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