This code when run in the CS50 Web IDE has expected results of running Luhn's Algorithm then correctly printing out the type of credit card used.
#include <stdio.h>
#include <string.h>
int main(void){
long ccNumber;
do{
printf("Insert CC Number: \n");
scanf("%ld", &ccNumber);
} while (ccNumber <= 0);
long ccCopy = ccNumber;
int sum;
int count = 0;
long divisor = 10;
char result[16];
while(ccCopy > 0){
int lastDigit = ccCopy % 10;
sum = sum + lastDigit;
ccCopy = ccCopy / 100;
printf("%i\n", sum);
}
ccCopy = ccNumber / 10;
while(ccCopy > 0){
int lastDigit = ccCopy % 10;
int timesTwo = lastDigit * 2;
sum = sum + (timesTwo % 10) + (timesTwo / 10);
ccCopy = ccCopy / 100;
}
ccCopy = ccNumber;
while(ccCopy != 0){
ccCopy = ccCopy / 10;
count++;
}
for(int i = 0; i < count - 2; i++){
divisor = divisor * 10;
}
int firstDigit = ccNumber / divisor;
int firstTwo = ccNumber / (divisor / 10);
if(sum % 10 == 0){
if(firstDigit == 4 && (count == 13 || count == 16)){
strcpy(result, "VISA");
} else if((firstTwo == 34 || firstTwo == 37) && count == 15){
strcpy(result, "AMEX");
} else if((firstTwo > 50 || firstTwo < 56) && count == 16){
strcpy(result, "MASTERCARD");
} else {
strcpy(result, "INVALID");
}
}
else {
strcpy(result, "INVALID lol");
}
printf("%i\n", sum);
printf("%s\n", result);
}
The issue is, when copy and pasted into VSCode the Sum is not calculated the same
These are the results from CS50 IDE:
Insert CC Number:
4012888888881881,
1,
9,
17,
25,
33,
41,
43,
43,
90,
VISA
And these are the results from VSCode with the exact same code copy and pasted:
Insert CC Number:
4012888888881881,
15774464,
15774472,
15774480,
15774488,
15774496,
15774504,
15774506,
15774506,
15774553,
INVALID lol
The original code did not have the printf showing sum in the first while loop I added it to debug the results.
This has left me very confused, considering the code is copy and pasted.
sum is not initialized to 0, so you are having undefined behaviour. Then you get different results depending on compiler, platform, weather....
You need to initialize all variables. int sum can be anything.
Initialize sum to 0.
int sum = 0;
Related
My code seems to work on some credit card numbers but for others it doesn't even run. I've tried to use long long for the credit card number but it didn't worked. I've spent like 2 hours trying to solve this issue but I can't figure it out. All help is welcomed :)
int main(void)
{
// Get credit card number
long num = get_cc_number();
// Check if the credit card number is valid
valid = check_sum(num);
// Check the length of the credit card number
length = check_length(num);
// Get the first two digits of the credit card number and first number of VISA
digits = get_first_digits(num);
digit_visa = digits / 10;
// Check if the card is American Express, Mastercard, Visa or Invalid
if (valid == 1)
{
if (length == 16)
{
if (digits <= 55 && digits >= 51)
{
printf("MASTERCARD\n");
}
else if (digit_visa == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
else if (length == 15)
{
if (digits == 34 || digits == 37)
{
printf("AMEX\n");
}
else
{
printf("INVALID\n");
}
}
else if (length == 13)
{
if (digit_visa == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
}
else
{
printf("INVALID\n");
}
}
long get_cc_number(void)
{
long cc_number;
cc_number = get_long("Credit Card Number: ");
return cc_number;
}
int check_sum(int num)
{
int num1 = num;
while (num1 >= 10)
{
sec_to_last = num1 % 100;
double_sec_to_last = sec_to_last * 2;
if (double_sec_to_last >= 10)
{
first_dig = double_sec_to_last / 10;
sec_dig = double_sec_to_last % 10;
first_sum += first_dig;
first_sum += sec_dig;
}
else
{
first_sum += double_sec_to_last;
}
num1 = num1 / 100;
}
int num2 = num;
while (num2 >= 10)
{
last = num2 % 10;
second_sum += last;
}
second_sum += first_sum;
if (second_sum % 10 == 0)
{
return 1;
}
else
{
return 0;
}
}
long check_length(long num)
{
long num_length = floor(log10(labs(num))) + 1;
return num_length;
}
long get_first_digits(long num)
{
long i = num;
while (i >= 100)
{
i /= 10;
}
return i;
}
A while back, I reviewed an issue like this where the user was getting tripped up on acquiring a credit card check digit. With that, I wrote a small proof-of-principle test program that allows validation of credit card numbers using the Luhn algorithm. Following, is that code snippet.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int check_valid(char * num)
{
int sum = 0;
int work = 0;
char card[20];
if ((strlen(num) %2 == 0)) /* Even numbers - do not need a leading zero */
{
strcpy(card, num);
}
else /* Odd numbers - add a leading zero to evaluate */
{
strcpy(card, "0");
strcat(card, num);
}
printf("Length of number is: %d\n", (int)strlen(num));
for (int i = 0; i < strlen(card); i++)
{
work = card[i] - '0';
if ((i %2) == 0)
{
work = (card[i] - '0') * 2;
if (work > 9)
{
work = work - 9;
}
}
sum = sum + work;
printf("Digit is: %d Value is: %d Sum is %d\n", (card[i]- '0'), work, sum);
}
return ((sum % 10) == 0);
}
int main()
{
char number[20];
int x = -1;
printf("Enter a number: ");
x = scanf("%s", number);
x = check_valid(number);
if (x == 0)
printf("Invalid\n");
else
printf("Valid\n");
return 0;
}
It doesn't identify the card issuer, just verifies that the number is valid.
As noted in the comments, one probably would want to utilize a string entry as this code snippet does rather than trying to utilize a very large integer. You might try going that route with a string as well. Give this a try and see if it allows you to progress.
I'm trying to solve a problem (as the title already state). I've actually learned that I can do it with modulo operator (%). But the first code that I wrote is using the while-loop, so I'm trying to finish the code.
This is the code
int main()
{
char arr[1000000];
int i = 0;
int sum = 0;
printf("type the number = ");
scanf("%s", arr);
while(arr[i] != '\0'){
sum = arr[i] + sum;
i++;
}
printf("the total number is = %d", sum);
so the problem is it's actually printing out some huge amount of number.. I guess it's because of the array is in char, can someone help me how do I changed the value into int ?
You need to substract from the digit code the code of '0'.
Here you have the both versions (I have added some logic to accept the numbers with + & - at there beginning):
int sumdigitsStr(const char *num)
{
int sum = 0;
int first = 1;
while(*num)
{
if(isdigit(*num)) {sum += *num - '0'; first = 0;}
else
if(first && (*num == '-' || *num == '+'))
{
first = 0;
num++;
continue;
}
else
{
sum = -1; break;
} //error string contains non digits
num++;
}
return sum;
}
int sumdigits(long long num)
{
int sum = 0;
do
{
sum += abs((int)(num % 10));
}while((num = num / 10));
return sum;
}
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n);
int d, sum = 0;
while (n != 0)
{
d = n % 10;
sum = sum + d;
n = n / 10;
}
printf("sum of digits is : %d", sum);
return 0;
}
I tried writing code for cs50 pset1's problem: credit.c(more comfortable) after week2. My code is given below. The problem is that 'sumx' and 'sumy' are just 0 and hence 'sum' is always equal to 0. So whenever I give a correct credit card number, it is just going to new line and program ends. How can I solve this problem and why are 'sumx' and 'sumy' not adding up to their respective sums as they should according to the algorithm?
My code is:
#include <cs50.h>
#include <stdio.h>
int main(void){
long long i;
do{
printf("Your credit card number:\n");
i = get_long_long();
}
while(i < 4e12 || i > 5.5e15);
int count = 0;
int n;
long long c = i;
while(i != 0){
n = i%10;
i = i/10;
count++;
}
int x[count];
for(int j = 0; j < count; j++){
x[j] = c%10;
i = c/10;
}
int sumx = 0;
for(int j = 0; j < count - 1; j += 2){
x[j] = x[j] * 2;
sumx = sumx + x[j];
printf("%i", sumx);
}
int sumy = 0;
for(int j = 0; j < count; j += 2){
sumy = sumy + x[j];
}
int sum;
sum = sumx + sumy;
if(sum%10 == 0){
if((count == 15 && x[14] == 3) && (x[13] == 4 || x[13] == 7)){
printf("AmEx\n");
}
else if((count == 16 && x[15] == 5) && (x[14] > 1 || x[14] < 5)){
printf("MASTERCARD\n");
}
else if((count == 13 && x[12] == 4) || (count == 16 && x[15] == 4)){
printf("VISA\n");
}
}
else{
printf("Invalid Number\n");
}
return 0;
}
//#include <cs50.h>
#include <stdio.h>
int main(void){
long long i=4111111111111111;
//Master: 5105105105105100;//16
//visa: 4111111111111111
printf("%lld\n",i);
//~ do{
//~ printf("Your credit card number:\n");
//~ i = get_long_long();
//~ }
//~ while(i < 4e12 || i > 5.5e15);
int count = 0;
long long c = i;
int k=0;
int x[100];//
while(c != 0){
x[k] = c%10;
c = c/10;
printf("%lld\n",c);
count++;
k++;
}
//k==count
printf("count:%d\n",count);
printf("k:%d\n",k);
// x[i] contains all the digits of credit card
printf("print x[i]\n");
for (int i=0;i<count;i++){
printf("%d ",x[i]);
}
printf("\n");
int addsum=0,x2prod=0;
for (int j=0; j<k; j+=2 ){
addsum += x[j];
}
printf("addsum:%d\n",addsum);
for (int j=1; j<k; j+=2 ){
if ( (2 * x[j]) > 9 ){ //have 2 digit
x2prod += (2 * x[j]) / 10;
x2prod += (2 * x[j]) % 10;
}
else // have one digit
x2prod += 2 * x[j];
}
printf("x2prod:%d\n",x2prod);
int sum;
sum = addsum + x2prod;
printf("\nsum: %d\n",sum);
if(sum%10 == 0){
if((count == 15 && x[14] == 3) && (x[13] == 4 || x[13] == 7)){
printf("AmEx\n");
}
else if((count == 16 && x[15] == 5) && (x[14] > 1 || x[14] < 5)){
printf("MASTERCARD\n");
}
else if((count == 13 && x[12] == 4) || (count == 16 && x[15] == 4)){
printf("VISA\n");
}
}
else{
printf("Invalid Number\n");
}
return 0;
}
I apply some correction on your code, store all credit card digits on x[]array in first while().
I checked code output with only three samples and by the way this is not a reliable version, try to catch any error.
As you read on my comment i don't any idea about that, but by perform simple search this link
show me what to do and decode it to your way.
So I'm working on this assignment. I tried running this code, and it ceases to work after reading in the second number. Here is a synopsis of it:
Write a C program that prompts the user to enter two positive integers. Your program should then display the number of carry operations that result from adding the two numbers and print the result of the addition. The input continues until the user enters 0 for the first number.
For instance, if the input is 123 and 456, there are 0 carry operations. If the input is 666 and 777, there are 3 carry operations.
Here's my code (nothing happens once it gets to the while loop):
#include <stdio.h>
#include <string.h>
// ------------------------
char dummy;
char response = 'y';
int userNum1, userNum2, sum;
int carry = 0;
int factor = 1;
int digit1 = 1;
int digit2 = 1;
int main(void)
{
while (response == 'y' || response == 'Y')
{
printf ("Enter the first number: ");
scanf ("%d", &userNum1);
printf ("Enter the second number: ");
scanf ("%c", &dummy);
scanf ("%d", &userNum2);
sum = userNum1 + userNum2;
while (userNum1 > 0 && userNum2 > 0)
{
while (digit1 > 0 && digit2 > 0)
{
digit1 = getNum (userNum1);
digit2 = getNum (userNum2);
factor = factor * 10;
carryTheOne (digit1, digit2);
}
}
printf ("The sum is %d and there were %d carry operations", sum, carry);
printf("\n");
printf ("Do you want to do another one?");
scanf ("%c", &dummy);
scanf ("%c", &response);
scanf ("%c", &dummy);
}
return 0;
}
int getNum(int num)
{
num = ((num % (factor * 10)) / factor);
return num;
}
int carryTheOne (int num1, int num2)
{
if ((digit1 + digit2) > 9)
carry++;
return carry;
}
Can someone help me here and let me know what I'm doing wrong? It just prints a blank line after reading the second number and never does anything else (doesn't terminate, just doesn't do anything).
EDIT: I changed the while loop to this:
while (userNum1 > 0 && userNum2 > 0)
{
while (digit1 > 0 && digit2 > 0)
{
digit1 = getNum (userNum1);
digit2 = getNum (userNum2);
factor = factor * 10;
carryTheOne (digit1, digit2);
userNum1 = (userNum1 - digit1) / 10;
userNum2 = (userNum2 - digit2) / 10;
}
}
It worked for 123 and 456, but didn't work for 666 and 777. Any reason why?
Remove that factor variable. I have made for edits:
1.Comment out
int factor = 1;
2.comment out
factor = factor * 10;
3.change
userNum1 = (userNum1 - digit1) / 10;
userNum2 = (userNum2 - digit2) / 10;
to
userNum1 = userNum1 / 10;
userNum2 = userNum2 / 10;
4.change:
int getNum(int num)
{
num = ((num % (factor * 10)) / factor);
return num;
}
to
int getNum(int num)
{
num = num % 10;
return num;
}
I need to make a blackjack program. The thing is, as you can see in the code, there's a loop for generating cards without repetition..
If I hit a key it'll print a line like:
"5-diamond"
then hit another key and for example it prints:
"8-clover"
So how do I add those two together without messing up with the code. Since I want to check the value of the sum of those two. What do I need to do?
int cards()
{
int card[51];
int used[51];
int x = 0;
int playerhand = 0;
int dealerhand = 0;
int sum = 0;
while(!kbhit())
x++;
srand(x % 100000);
for(int i = 0; i <= 51; i++)
used[i] = 0;
for(;;)
{
int w;
do
{
w = rand() % 52;
}
while(used[w] == 1);
used[w] = 1;
int value = w % 13 + 1;
if(value >= 2 && value <= 10)
printf("%d-", value);
else
{
if(value == 1)
printf("Ace ");
if(value == 11)
printf("Jack ");
if(value == 12)
printf("Queen ");
if(value == 13)
printf("King ");
}
int suit = (int)(w / 13);
if(suit == 0)
printf("Clover");
if(suit == 1)
printf("Spade");
if(suit == 2)
printf("Heart");
if(suit == 3)
printf("Diamond");
printf("\n");
getch();
}
}
You should compute the sum of the values and the count of Aces.
If a card is an Ace, add 11, if it is a King, Queen or Jack, add 10, otherwise add value.
If the sum is greater than 21 and you saw Aces, deduct 10 for each Ace until you fall back below 22.