Reputation: 3
I am trying to write a small c-program to perform arithmetic operations using functions, but facing some error.
Can some one please have a look on this code and verify it, please!
Error:
--------------------Configuration: mingw5 - CUI Debug, Builder Type: MinGW--------------------
Checking file dependency...
50: error: cannot convert `div_t' to `int' in assignment
64: error: a function-definition is not allowed here before '{' token
69: error: a function-definition is not allowed here before '{' token
74: error: a function-definition is not allowed here before '{' token
79: error: a function-definition is not allowed here before '{' token
84: error: a function-definition is not allowed here before '{' token
90:2: warning: no newline at end of file
Complete Make Untitled25: 6 error(s), 1 warning(s)
CODE
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int add(int, int);
int sub(int, int);
int mul(int, int);
int divs(int, int);
int print(int);
int main(){
int ans,a,b,n;
printf("******************");
printf("\n1 ADD");
printf("\n2 SUB");
printf("\n3 MUL");
printf("\n4 DIV");
printf("\n5 EXT");
printf("\n******************");
printf("\n\n\nEnter Your Option(1-5):");
scanf("%d",&n);
// n=1;
printf("\nEnter Values a,b:");
scanf("%d%d",&a,&b);
// a=1;b=2;
if(n==4&&b==0){
printf("\n\n Value of `B` Can't Be `0` for Division\n\n");
exit(EXIT_SUCCESS);
}
if(n>0&&n<6){
if(n==1){
ans=add(a,b);
// main();
}
if(n==2){
ans=sub(a,b);
// main();
}
if(n==3){
ans=mul(a,b);
// main();
}
if(n==4){
ans=divs(a,b);
// main();
}
if(n==5){
exit(EXIT_SUCCESS);
}
print(ans);
}
else {
printf("\nWorng Entry!\n ");
exit(EXIT_SUCCESS);
}
int add(int a,int b){
ans=a+b;
return(ans);
}
int sub(int a,int b){
ans=a-b;
return(ans);
}
int mul(int a,int b){
ans=a*b;
return(ans);
}
int divs(int a,int b){
ans=a/b;
return(ans);
}
int print(int ans){
printf("\n\n Answer = %d",ans);
return;
}
}
I am using C-Free Professional to write and compile C Programs. I have tried on-line C-Program Compilers also. But same error.
Upvotes: 0
Views: 6427
Reputation: 15934
You included:
#include<stdlib.h>
This contains a function div()
already which has a different return type to your div
function hence giving this error.
To solve this you will either have to rename you div
function or not use this include file.
For more info on the built in div
see: http://www.tutorialspoint.com/c_standard_library/c_function_div.htm
Also you will need to take all of your functions and move them to outside main
. Right now you have the following:
int main(){
...
int add(int a,int b){
ans=a+b;
return(ans);
}
...
}
which needs to be changed to
int add(int a,int b){
return( a+b); //note there is no "ans" now
}
int main(){
...
...
}
Upvotes: 1
Reputation: 4231
div()
is part of the standard library, and defined in stdlib.h
, which you include in your program. You will have to use a different name for your function, for example my_div
.
There are some more errors in your code - most important, standard C does not have nested functions.
Upvotes: 1
Reputation: 41749
div already exists as a standard C function, and returns a different type. See here
You'll need to use a different name for your div.
Upvotes: 0