Hello all,
I am new to C and am doing the cs50 course from edX and I was tasked with creating a program to verify the checksum of a credit card using Luhn's algorithm.
Currently, I am trying to figure out how the heck to store a double digit integer in a single index of an array. Is that possible, or do I need to do something complex like an object array or something?
Also, you will see that I am using strings, or character arrays to store the user's credit card number input...
I realize this might seem stupid, but I just wanted an easy way to determine the length of the array, and strlen() makes that pretty easy.
I contemplated doing something hackish like storing the number's real value in ascii ... then converting later currently that's my best option but I'm looking for something more legit.
Any help at all is greatly appreciated!
For loop I am stuck on completing:
Code:
//init array to hold the doubled integers from eOther1 char deOther1[9];
//limiter for forloop
int length3 = strlen(eOther1);
//double every number in eOther1 and store it in deOther2
for(int t = 0; t < length3; t++){
printf("Here is eOther1: %c \n", eOther1[t]);
// printf("Here is deOther1: %c \n", deOther1[t]);
//performing an operation on a character requires you to set that character back to its integral value
int eOther1Holder = ((eOther1[t]) - '0');
printf("deOther1Holder: %i \n", eOther1Holder);
int deOther1Sum = (eOther1Holder * 2);
printf("here is deOther1Sum: %i \n", deOther1Sum);
//insert the sum of the double of eOther1 into its array, deOther1
deOther1[t] = deOther1Sum;
}
printf("Here is deOther1: %s \n", deOther1);
All my code so far: luhn-alg.c
Code:
#include <stdio.h>#include <string.h>
#include <cs50.h>
#define MAX_LIMIT 19
int main () {
char str[MAX_LIMIT];
int length;
//get input from user & store it in string/array
printf("enter credit #");
fgets(str, MAX_LIMIT, stdin);
length = strlen(str);
length -= 1;
//validation for # of digits
if(length < 13 || length > 16){
printf("Invalid\n %d \n", length);
return 0;
} else if(length == 14) {
printf("invalid\n %d \n", length);
return 0;
}
//create the array to copy str into
//char charArr[length + 1];
//strcpy(charArr, str);
printf("Length of string - %s is: %d \n", str, length);
// printf("%c", charArr[2]);
// start with the second to last digit of input and store every other number in eOther1[]
////make array eOther1
char eOther1[9];
//eOther1 indexer
int i = 0;
for(int length2 = (length - 2); length2 >= 0; length2 = (length2 - 2)){
////test
printf("1str[length2]:%c ---", str[length2]);
printf("1length2:%i --", length2);
printf("1i:%i ", i);
////copy the char from array str[length2] and insert into array eOther[i]
eOther1[i] = str[length2];
////test
printf("1eOther1[i]:%c \n", eOther1[i]);
i++;
}
printf("1Here is eOther1: %s \n", eOther1);
//init array to hold the doubled integers from eOther1
char deOther1[9];
//limiter for forloop
int length3 = strlen(eOther1);
//double every number in eOther1 and store it in deOther2
for(int t = 0; t < length3; t++){
printf("Here is eOther1: %c \n", eOther1[t]);
// printf("Here is deOther1: %c \n", deOther1[t]);
//performing an operation on a character requires you to set that character back to its integral value
int eOther1Holder = ((eOther1[t]) - '0');
printf("deOther1Holder: %i \n", eOther1Holder);
int deOther1Sum = (eOther1Holder * 2);
printf("here is deOther1Sum: %i \n", deOther1Sum);
//insert the sum of the double of eOther1 into its array, deOther1
deOther1[t] = deOther1Sum;
}
printf("Here is deOther1: %s \n", deOther1);
// start with the last digit and store every other number in eOther2
char eOther2[9];
//eOther2 indexer
int j = 0;
for(int length1 = (length - 1); length1 >= 0; length1 = (length1 - 2)){
////test
printf("2str[length1]:%c ---", str[length1]);
printf("2length1:%i --", length1);
printf("2i:%i ", j);
//copy the char from array str[length2] and insert into array eOther[i]
eOther2[j] = str[length1];
////test
printf("2eOther2[j]:%c \n", eOther2[j]);
j++;
}
printf("2Here is eOther2: %s \n", eOther2);
return 0;
}
//add all digits together in deOther1 and store in total
//add total to digits in eOther2
//add all of eOther2 together, store in gTotal
//if the last number of gTotal is 0, input number is legit