0

I have reviewed multiple questions and answers for powers in C, or 4^2. I have wrote it out in different ways and still can't it right. As of now I get the message:

/tmp/cc9yBiPo.o: In function `main':
Functions2.c:(.text+0x2b8): undefined reference to `pow'
collect2: error: ld returned 1 exit status.

Here is my code so far, like I said I have tried different peoples answers but I am more confused more than anything.

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


int main()

{

int choice;
int a, b, c;
float d;


    do{
    printf("\t Menu \n");
    printf("1. Addition \n");
    printf("2. Subtraction \n");
    printf("3. Multiplication \n");
    printf("4. Division \n");
    printf("5. Modulo \n");
    printf("6. Eponents \n");
    printf("7. Exit \n");

    printf("Please choose a menu selection: \n");
    scanf("%d", &choice);

    switch(choice)
    {
    case 1:
    {
        printf("Enter two numbers to add: \n");
        scanf("%d%d", &a, &b);

        c = a + b;

        printf("The sum of the two numbers = %d \n", c);

    }break;

    case 2:
    {
        printf("Enter two numbers to subtract: \n");
        scanf("%d&d", &a, &b);

        c = a - b;

        printf("The difference between the two number = %d \n", c);

    }break;

    case 3:
    {
        printf("Enter two numbers to multiply: \n");
        scanf("%d%d", &a, &b);

        c = a * b;

        printf("The product of the two numbers = %d \n", c);
    }break;

    case 4:
    {
        printf("Enter two numbers to divide: \n");
        scanf("%d%d", &a, &b);

        d = (float) a/b;

        printf("The quotient of the two numbers = %f \n", d);

    }break;

    case 5:
    {
        printf("Enter two numbers to get a modulo: \n");
        scanf("%d%d", &a, &b);

        c = a % b;

        printf("The modulus of the two numbers = %d \n", c);
    }break;

    case 6:
    {
        printf("Enter a base: \n");
        scanf("%d", &a);

        printf("Enter an exponent: \n");
        scanf("%d", &b);

        c = pow(a, b);

        printf("The exponent of the two numbers = %d", c);
    }break;

    case 7:
    {
        printf("Thank you, you will now exit.");
    }break;

    default:
        printf("error \n");
    }
}   while (choice != 7);

return 0;
}
Simson
  • 3,373
  • 2
  • 24
  • 38
  • 3
    what is the command line of your compilation ? have you tried with `-lm` at the end of your command ? – dvhh Oct 28 '19 at 02:29
  • 2
    Maybe this will help: https://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c – happydave Oct 28 '19 at 02:29
  • 1
    I am not familiar with -lm, what does it do? – Frank Weirich Oct 28 '19 at 03:14
  • [“undefined reference to pow even with math.h and the library link -lm](https://stackoverflow.com/q/16344445/995714), [Why do you need an explicit `-lm` compiler option](https://stackoverflow.com/q/10371647/995714), [Why Am I Getting Link Errors When Calling Function in Math.h?](https://stackoverflow.com/q/103407/995714) – phuclv Oct 28 '19 at 05:36

2 Answers2

1

You have to link with the math library(libm).

Add -lm flag to your complication command to link with it.

Check this question.

Mostafa
  • 468
  • 3
  • 16
0

You have to change the data types of base & exponent to get correct result.

The syntax for the pow function in the C Language is:

double pow(double x, double y);

Again, change the data type from int to float in case of 4.

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


int main()

{

   double g,h,i;
   int choice;
   int a, b, c;
   float d,e,f;


   do{
        printf("\t Menu \n");
        printf("1. Addition \n");
        printf("2. Subtraction \n");
        printf("3. Multiplication \n");
        printf("4. Division \n");
        printf("5. Modulo \n");
        printf("6. Eponents \n");
        printf("7. Exit \n");

        printf("Please choose a menu selection: \n");
        scanf("%d", &choice);

        switch(choice)
         {
          case 1:
             {
                printf("Enter two numbers to add: \n");
                scanf("%d%d", &a, &b);

                c = a + b;

                printf("The sum of the two numbers = %d \n", c);

             }break;

         case 2:
             {
                printf("Enter two numbers to subtract: \n");
                scanf("%d&d", &a, &b);

                c = a - b;

                printf("The difference between the two number = %d \n", c);

             }break;

         case 3:
             {
               printf("Enter two numbers to multiply: \n");
               scanf("%d%d", &a, &b);

               c = a * b;

               printf("The product of the two numbers = %d \n", c);
             }break;

         case 4:
             {
               printf("Enter two numbers to divide: \n");
               scanf("%f%f", &e, &f);

               d = e/f;

               printf("The quotient of the two numbers = %f \n", d);

             }break;

         case 5:
              {
               printf("Enter two numbers to get a modulo: \n");
               scanf("%d%d", &a, &b);

               c = a % b;

               printf("The modulus of the two numbers = %d \n", c);
              }break;

          case 6:
              {
               printf("Enter a base: \n");
               scanf("%lf", &h);

               printf("Enter an exponent: \n");
               scanf("%lf", &i);

               g = pow(h, i);

               printf("The exponent of the two numbers = %lf", g);
              }break;

         case 7:
              {
               printf("Thank you, you will now exit.");
              }break;

        default:
               printf("error \n");
              }
              }   while (choice != 7);



        return 0;
              }

Hope this will help you.

Jagadish
  • 45
  • 6