I'm working on an exercise problem for a programming class and I'm a bit out of my depth debugging it. My main problem is that I have had to convert strings into integers and vice-versa and I don't understand well how that works. I'll attach a copy of the exercise and another of my code. Any and all help is appreciated.
CODE:
#include <cs50.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
string Company_Check(long number);
bool Luhn (long number);
int main(void)
{
//Prompt for card Number
long card = get_long("Number:");
//Applying Luhn's Algorithm
bool validity = Luhn(card);
if (validity==true) //The card passed the test
{
//Finding and printing company name
printf("\nCompany: %s\n", Company_Check(card));
} else //The card didn't pass the test
{
printf("INVALID (Luhn’s Algorith)\n");
}
}
string Company_Check(long number)
{
//Declaring variables
char num[30];
char char_first_two[3]; //This is where I'll save the first two digits of the number
int int_first_two;
char char_first_one[3]; //This is where I'll save the first one digit of the number
int int_first_one;
//Creating the two 'First digits' var
int first_two = 10*(atoi(num[0]))+ atoi([num[1]);
int fist_one = atoi(num[0]);
//Analizing cases
if (first_two == 34 || first_two == 37)
{
printf("American Express\n");
} else if (first_two == 51 || first_two == 52 || first_two == 53 || first_two == 54 || first_two == 55)
{
printf("Mastercard\n");
} else{
//Could be (1) Visa or (2) INVALID
if (first_one == 4)
{
printf("Visa\n");
} else
{
printf("INVALID\n");
}
}
}
bool Luhn (long number) //Done
{
int i; //Used in the for loops
int sum1; //Used to tally the sums in Step 4
int sum2; //Used to tally the sums in Step 4
int total; //Used to tally the sums in Step 4
int cod; //Counter of Digits
int lastnumber; //Used in Steps 5 and 6
char num[30]; //Card number & total in String format
char fot[30]; //First Every-Other number list
char sot[30]; //Second Every-Other number list
//1. Converting the long NUMBER to a string NUM
sprintf(num, "%d", number);
//2. Calculating the number of digits of the number
cod = 0;
for(i=0; num[i+1]!= '\0' ; i++)
{
cod++;
}
//3. Creating two CHAR arrays of the first and second every-other number list
if (cod%2==0)//number of digits is even
{
for(i=0; num[i+1]!= '\0' ; i++)
{
if (i%2==0) //We're dealing with the FIRST set of every other numbers
{
fot[i/2]=num[i];
}
else //We're dealing with the SECOND set of every other numbers
{
sot[((int)(i/2))+1]=num[i];
}
}
fot[(cod/2)+1]= '\0';
sot[(cod/2)+1]= '\0';
} else //number of digits is odd
{
for(i=0; num[i+1]!= '\0' ; i++)
{
if (i%2==0) //We're dealing with the SECOND set of every other numbers
{
sot[i/2]=num[i];
}
else //We're dealing with the FIRST set of every other numbers
{
fot[((int)(i/2))+1]=num[i];
}
}
sot[(cod/2)+1]= '\0';
fot[(cod/2)+1]= '\0';
}
//4. Sum Algorithm
sum1=0;
sum2=0;
total=0;
for(i=0 ; fot[i+1]!= '\0' ; i++)
{
sum1 =+ atoi(fot[i]);
}
for(i=0 ; sot[i+1]!= '\0' ; i++)
{
sum2 =+ atoi(sot[i]);
}
total = sum1 + sum2;
//5. Isolating the last number
//converting the total into a string (using var NUM because its no longer needed)
sprintf(num, "%d", total);
//Calculating the number of digits
cod = 0;
for(i=0; num[i+1]!= '\0' ; i++)
{
cod++;
}
//putting the last number in a var
lastnumber = atoi(num[cod-1]);
//6. Studying cases
if (lastnumber==0)
{
return true;
}
else
{
return false;
}
}
Problem: