Reputation: 3
I have to write a case for a '%' (modulus) function for a calculator.
When I compile, it tells me I have used the fmod
function incorrectly.
Without the case '%', the rest of the code works as it should.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h> // Originally missing
#define MAXOP 100
#define NUMBER '0'
int getop(char []);
void push(double);
double pop(void);
main()
{
int type;
double op2;
char s[MAXOP];
printf("\nEnter numbers below in the following format\nwith a space between each:\n");
printf("\tnum1 num2 operator\n");
printf("\tyou may use (+, -, /, *) for the operators\n");
while((type = getop(s)) != EOF)
{
switch(type)
{
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if(op2 != 0.0)
push(pop() / op2);
else
printf("error: zero divisor\n");
break;
case '%':
op2=pop();
if(op2 != 0.0)
push(fmod(pop(),op2));
else
printf("error: zero divisor");
break;
case '\n':
printf("\t%.8g\n", pop());
break;
default:
printf("error: unknown command %s\n", s);
break;
}
}
return 0;
}
I looked up the fmod
function, and I still can not figure out why it will not work. Any thoughts?
I added the math.h header, and the error message I am getting now is:
/tmp/ccgiVhWI.o: In function `main':
calc.c:(.text+0x14f): undefined reference to `fmod'
collect2: ld returned 1 exit status
Upvotes: 0
Views: 131
Reputation: 755016
You are missing #include <math.h>
so the compiler thinks fmod()
is a function returning int
, or it is complaining that it knows about fmod()
but you've not declared it properly, or thereabouts.
You would do better to include the exact compiler error message (unless there's a very long path for the file name, in which case truncating the file name would be a courtesy).
Upvotes: 2