質問

The program is supposed to get letters both uppercase and lowercase from user and each letter is assigned to a value.

Example. S is 0, K is 1 and E is 2.
The totalvalue formula is ((A*10)+(B))*10^C

A - first input B - 2nd input C is the 3rd input.
The function int lettervalue(char letter) is supposed to return the value of the letter. I used switch construct for assigning the letter value.

Everytime i compile I get error: conflicting types for 'pow'. Does the pow function accept variables, and is there something I should change in function int lettervalue(char letter)?

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

int pow(int x, int y);

int lettervalue(char letter)
{
    int val;
    switch (letter){
        case 'S':
        case 's':
            val = 0;
            break;
        case 'K':
        case 'k':
            val = 1;
            break;
        case 'E':
        case 'e':
            val =2;
            break;
    }
    return val;
}

int main(void)
{   int valueA, valueB, valueC, totalvalue;
    char A, B, C;
    printf("Please input the letter: ");
    scanf("%c%c%c", &A, &B, &C);                
    valueA = lettervalue(A)*10;
    valueB = lettervalue(B);
    valueC = lettervalue(C);
    totalvalue = (valueA+valueB)*pow(10, valueC);
    printf("The total value is %d", totalvalue );

    return 0;
}
役に立ちましたか?

解決

Code unnecessarily provides a pow() prototype, which is inconsistent with the one in math.h.

#include <math.h>
// math.h provides: double pow(double x, double y);

Eliminate int pow(int x, int y);.

Change code

// totalvalue = (valueA+valueB)*pow(10, valueC);
totalvalue = (valueA+valueB)* ((int)pow(10, valueC));

Or

double total;
total = (valueA+valueB)*pow(10, valueC);
printf("The total value is %.0lf", total );

Be sure to link in the math library to avoid 'undefined reference to pow'.

"undefined reference to `pow'" even with math.h and the library link -lm

他のヒント

if you already use math.h, you don't need to declare function pow. So removing line int pow(int x,int y); should help.

If you want to create your own simple pow function you don't need to add the library math.h, and define function pow as follows:

int pow(int x, int y)
{
  int result=1,i;
  for(i=1;i<=y;i++)
    result *=x;

  return result;
}

p/s: as the pow function grow very quickly, I suggest you use long value instead of int

The pow() function is defined in math.h as:

double pow (double base, double exponent);

The prototype your using for pow() makes confliction between arguments types as you changed them to int. You couldn't change the input and output types this way. You simply do it by casting the arguments and the returned value but this is not necessery as the conversion is done implicitly.

Just remove the pow() prototype.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top