0

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:

Problem

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Please describe a specific problem/error and ask a specific question. We can't just debug the whole thing for you. Please review [How to ask](https://stackoverflow.com/help/how-to-ask) – kaylum May 31 '21 at 09:46
  • Welcome to StackOverflow! Please take the [tour] to learn how this site works. Your question is far too broad. -- Begin with baby steps, and walk your way through the task in small steps. Debug each new part before proceeding. -- In the course learn how to use a debugger, IDEs provide interfaces for this. If you don't have an IDE, search for one, and learn to use it. – the busybee May 31 '21 at 10:29
  • Welcome to SO. What you show as "problem" is not a problem. It is just description of your task to solve. For SO to help you, we need to know what specific problem you have solving that task. Which errors do you face etc.? – Gerhardh May 31 '21 at 11:30
  • Card numbers, phone numbers etc. are not really numbers. They just happen to consist of deciman digits but apart from that they are strings. If you start a phone number with `"0000"` this would not be detectably if you deal with numbers. You should handle such "numbers" as strings. If you for some strange reason insist to handle it as integers, you should check range of the integer type you are using. Can your type hold 16 digits? – Gerhardh May 31 '21 at 11:34
  • `int fist_one = atoi(num[0]);` you don't initialize `num`. – Gerhardh May 31 '21 at 11:40
  • I'm new to the platform, I'm sorry if my question doesn't abide by the community guidelines. I will keep your comments in mind for future reference. – Ignacio Balasch Sola May 31 '21 at 14:35
  • It is incredible that you don't get warnings from the compiler. If you do, you should heed them. – Armali May 31 '21 at 18:19

0 Answers0