IT, Programming, & Web Development › Forums › CS50’s Introduction to Computer Science by Harvard University on Edx › Week 1: [C] – Data Types, Operators, Conditional Statements, Loops, and Command Line › Credit card problem: Unable to calculate the sum of entered credit card no
Tagged: correct format code, format code, infinite loop, while loop
- This topic is empty.
-
AuthorPosts
-
September 28, 2021 at 8:51 am #106::
#include <cs50.h> #include <stdio.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 <cs50.h> #include <stdio.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
Reply
https://cs50.stackexchange.com/questions/41972/credit-card-problem-unable-to-calculate-the-sum-of-entered-credit-card-no[learn_press_profile]
-
AuthorPosts
- You must be logged in to reply to this topic.