0
#include <stdio.h> #include <cs50.h> int main(void) { long creditcardno; do { creditcardno = get_long("enter"); } while (creditcardno < 1000000000000 || creditcardno > 9999999999999999); int i = 0; long cc = creditcardno; while (cc > 0) { cc = cc / 10; i++; } printf("%d", i); if (i == 14) printf ("invalid"); return 0; int sum1 = 0; int mod1 = 0; int mm = creditcardno; do {mod1 = mm%10; mm = mm/10; sum1 = sum1 + mod1; } while (mm > 0); printf ("sum of the digits is %d", sum1); } 

I could get output as invalid if 14 digit number is entered. But the last chunk of the problem seems not working.

int sum1 = 0; int mod1 = 0; int mm = creditcardno; do {mod1 = mm%10; mm = mm/10; sum1 = sum1 + mod1; } while (mm > 0); printf ("sum of the digits is %d", sum1); 

I intend to get sum of all the digits before moving to the exact requirements of the credit card problem with CS50.

As part of continuing with the problem, now that I can calculate no. of digits of entered credit card, mark as invalid if a 14 digit no. entered, I am facing issue with figuring out first two digits of entered credit card no. I am once again pasting the full code:

#include <stdio.h> #include <cs50.h> int main(void) { long creditcardno; do { creditcardno = get_long("enter"); } while (creditcardno < 1000000000000 || creditcardno > 9999999999999999); int i = 0; long cc = creditcardno; while (cc > 0) { cc = cc / 10; i++; } printf("%d\n", i); if (i == 14) { printf("invalid"); return 0; } int sum1 = 0; int mod1 = 0; int sum2 = 0; int mod2 = 0; int d1 = 0; int d2 = 0; long mm = creditcardno; do { mod1 = mm%10; mm = mm/10; sum1 = sum1 + mod1; mod2 = (mm %10) * 2; mm = mm/10; d1 = mod2%10; d2 = mod2/10; sum2 = sum2 + d1 + d2; } while (mm > 0); printf("sum of the digits %d\n", sum1 + sum2); 

Till the above, it seems the program is working. From here on, help needed.

long firsttwodigits = creditcardno; do { firsttwodigits = firsttwodigits/10; } while (firsttwodigits > 100); printf("First two digits are%d", firsttwodigits); } 

Not getting the program compiled after adding the last chunk. Here is help50 message:

Asking for help...

credit.c:52:39: error: format specifies type 'int' but the argument has type 'long' [-Werror,-Wformat] printf("First two digits are %d", firsttwodigits); ~~ ^~~~~~~~~~~~~~ %ld

Be sure to use the correct format code (e.g., %i for integers, %f for floating-point values, %s for strings, etc.) in your format string on line 52 of credit.c. ~/pset1/ $

I am not sure if to use %i or %d for printf format. Also seen on some tutorials %li. And if this issue that is creating problem in this chunk.

Update: Indeed by replacing %i with %li, the program is displaying the first two digits.

Help regarding the next leg of the problem is on this new thread: Credit problem: Classifying into Amex/Visa/Mastercard

1
  • It never executes. May be time to break out debug50. Remember to use {} around if body. Commented Oct 1, 2021 at 12:59

1 Answer 1

1

int mm = creditcardno; This should be a long, it is likely overflowing. You can add some printf for mm and mod1 to help you debug it.

8
  • By replacing int mm with long mm, this error message (help50): By "undeclared identifier," clang means you've used a name mm on line 25 of credit.c which hasn't been defined. If you mean to use mm as a variable, make sure to declare it by specifying its type, and check that the variable name is spelled correctly. Commented Oct 2, 2021 at 6:52
  • 1
    We would have to see what exactly you have on line 25 or the whole current code. Maybe this helps: // return 0; // you don't want to stop here with your main function Commented Oct 2, 2021 at 19:35
  • 1
    And then: int sum1 = 0; int mod1 = 0; long mm = creditcardno; do {mod1 = mm%10; mm = mm/10; sum1 = sum1 + mod1; } while (mm > 0); printf ("sum of the digits is %d", sum1); Commented Oct 2, 2021 at 19:38
  • 1
    The curly braces must surround the body of the if, not the whole if . <pre> <code> if i is 14 { print invalid STOP } <pre><code> The starting curly brace { would be after if condition and the ending one after return 0; else do nothing and continue processing if (i == 14) { printf ("invalid"); return 0; } //if i is not 14, continue int sum1 = 0; int mod1 = 0; long mm = creditcardno; Commented Oct 3, 2021 at 19:51
  • 1
    sorry for the formatting, I cannot seem to get it working but hope you get the point. Commented Oct 3, 2021 at 19:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.