0

I am currently doing the problem set 1 credit in cs50 program. I managed to write the code. It's compiles fine however once I enter the credit card number it stops, i must type any letter to get the program gives the answer. I tried to debug the code I get:

/usr/bin/ld: /tmp/ccyh9dGF.o: in function `main':
/home/ubuntu/pset1/credit/credit.c:12: undefined reference to `log10'
/usr/bin/ld: /tmp/ccyh9dGF.o: in function `credit':
/home/ubuntu/pset1/credit/credit.c:222: undefined reference to `get_long_long'
/usr/bin/ld: /home/ubuntu/pset1/credit/credit.c:225: undefined reference to `log10'
/usr/bin/ld: /home/ubuntu/pset1/credit/credit.c:225: undefined reference to `log10'
collect2: error: ld returned 1 exit status

Although I use math.h, cs50.h and stdio.h

Thank you in advance for your response.

Here is the code:

# include <cs50.h>
# include <stdio.h>
# include <math.h>


long credit(void);


int main(void)
{
     long long  num = credit();
     long long  num2 = num;
     int c = log10(num), i, sum , sum1, sum2 = 0, digits[c], divid[c], remind[c], total[c], cards[c];
     scanf("%d", &c);
     for ( i = 0; i <= c ; i++)
     {

          digits[c-i] =  num % 10;
          num = num /10;
          scanf("%d", &digits[c-i]);

     }

     if ( c % 2 != 0)

      {
           for ( i = 0 ; i <= c ; i++)
          {

                    if (  i % 2 == 0 )
                    {
                        digits[i] = digits [i] * 2;

                    }

                    else
                    {
                         digits[i] = digits [i];

                    }


          }
      }

      else
      {
           for ( i= 0 ; i <= c ; i++)
           {

                   if ( i % 2 != 0)
                   {
                    digits [i] = digits[i] * 2;

                   }

                   else
                   {
                        digits[i] = digits[i];

                   }


           }
      }

     for (  i = 0; i <= c; i++ )
     {
       remind[i] = digits[i] % 10;
       
     }
     //printf("\n");


     for (i = 0; i <= c; i++)
     {
         divid[i] = digits[i] / 10;
        
     }
     

     for ( i = 0; i <= c; i++ )
     {
           total[i]  = remind[i] + divid[i];
          
     }
      


     for ( i = 0; i <= c ; i++ )
     {
          sum = sum + total[i];

     }
        

 // recreate the card's number in a form of an array

 for ( i = 0; i <= c ; i++)
     {
          cards[c-i] =  num2 % 10;
          num2 = num2 /10;
          scanf("%d", &cards[c-i]);
     }
    
 // check the nature and the validity of a card

      int cards1 = cards[1];
  

 if  (sum % 10 == 0 && cards[0] == 3)
 {
     if (c == 15 )
     {
     switch (cards [1])
     {
         case 4: printf("AMEX");
         break;
         case 7: printf("AMEX");
         break;
     }
     }
     //return 0;
    printf("AMEX");
 }

 else if (cards[0] == 5 && sum % 10 == 0)
 {
     if (c == 16)
     {
     switch (cards [1])
     {
         case 1: printf("MASTERCARD");
         break;
         case 2: printf("MASTERCARD");
         break;
         case 3: printf("MASTERCARD");
         break;
         case 4: printf("MASTERCARD");
         break;
         case 5: printf("MASTERCARD");
         break;
    }
    }
    //return 0;
    printf("MASTERCARD\n");
 }

else if (cards[0] == 4 && sum % 10 == 0)
{
     switch (c)
     {
         case 13: //printf("VISA\n");
          break;
         case 16: //printf("VISA\n");
          break;

     }
      printf("VISA\n");

}

 else
 {
     printf("INVALID\n");
 }
  printf("\n");




}


// get number of digits of an integer

  long  credit(void)
  {
      long long  n;
      do
      {

          n = get_long_long("Number: ");

       }
       while (log10(n)  < 12 || log10(n) > 16);

       return n;
  }
2
  • Getting a program to successfully compile is only part of the battle. All that means is that the code is probably syntactically correct. Now, it has to actually run correctly. (Many new programmers think that a program that compiles is 99% of the battle. That's far from the truth.) Now, it's about debugging and verifying that it works correctly. Having said that, we really need to see some code to evaluate it.
    – Cliff B
    May 4 at 22:55
  • @CliffB the code is added to my question. Thank you in advance.
    – Mana Tahir
    May 5 at 7:48
0

The problems lie here:

 scanf("%d", &c);
 for ( i = 0; i <= c ; i++)
 {

      digits[c-i] =  num % 10;
      num = num /10;
      scanf("%d", &digits[c-i]);

 }

The scanf() function calls for user input. The call to get_long_long() in your credit() function gets all the needed user input. There is no need for any additional user input.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Cliff B , indeed the problem was because of the scanf command. thanks a lot to all of you
    – Mana Tahir
    May 6 at 9:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .