I'm building a calculator for my first project in C and everything worked fine until I implemented division. It keeps giving me the error "conflicting types for div
", even though the function is exactly the same as all the others.
#include <stdio.h>
#include <stdlib.h>
int num1;
int num2;
char op;
int res;
int poscount;
int negcount;
int add(num1, num2) {
res = num1 num2;
printf("Result: %d\n", res);
if(res >= 0)
poscount = 1;
else
negcount = 1;
return res;
}
int sub(num1, num2) {
res = num1 - num2;
printf("Result: %d\n", res);
if(res >= 0)
poscount = 1;
else
negcount = 1;
return res;
}
int mul(num1, num2){
res = num1 * num2;
printf("Result: %d\n", res);
if(res >= 0)
poscount = 1;
else
negcount = 1;
return res;
}
int div(num1, num2) {
res = num1 / num2;
printf("Result: %d\n", res)
return res;
}
int main() {
printf("Number: ");
scanf("%d", &num1); //Zahl 1
while(op != '=') {
printf("\nOperator: ");
scanf(" %c", &op); //Operator
if (op == '=') { //Überprüfung ob User weiterrechnen will
printf("Final result: %d\n", res);
printf("Number of positive: %d\n", poscount);
printf("Number of negatives: %d\n", negcount);
break;
}
printf("\nNumber: ");
scanf("%d", &num2); //Zahl
if(op == ' ') {
num1 = add(num1, num2); //Addition
}
else if(op == '-'){
num1 = sub(num1, num2); //Subtraktion
}
else if(op == '*'){
num1 = mul(num1, num2); //Multiplikation
}
else if(op == '/'){ //Divisionen
if(num2 == 0) {
printf("Division by 0."); //Ausnahme Division durch 0
printf("No new result.");
}
else {
num1 = div(num1, num2);
}
else {
printf("Unknown operator");
}
}
return 0;
}
That's my code. Any ideas?
CodePudding user response:
Your function div
has the same name as the C standard library function div
. The latter returns a value of the type div_t
, and not an int
, hence the error reporting "conflicting types".
The solution, and a good general practice, is to not have a function with the same name as one from the C standard library. Here, for example, name your function myDiv
. I understand that this is an exercise in programming; otherwise you would want to use the standard library functions instead of reimplementing them.
Some additional advice:
- Do not use global variables if their use can reasonably be avoided. This is the case here. In addition to that, do not reuse the variable names within functions (this is called shadowing).
- Initialize
poscount
andnegcount
to 0. - Initialize
op
to some placeholder value (here, not=
). Better yet, restructure the code so thatop
is assigned from the input first and evaluated afterwards. - Do not rely on the default type of variables being
int
, but declare the type explicitly. - The variable 'res' appears to be unused. Isn't the result stored in
num1
? - Use the pre-increment operator (
= 1
. - Use a switch-statement instead of multiple else-if's.
- Using English comments will make your code more easily understood by a wider audience.
Working code with some (not all) fixes applied:
#include <stdio.h>
#include <stdlib.h>
int poscount = 0;
int negcount = 0;
int add(int num1, int num2) {
int res = num1 num2;
printf("Result: %d\n", res);
if (res >= 0)
poscount;
else
negcount;
return res;
}
int sub(int num1, int num2) {
int res = num1 - num2;
printf("Result: %d\n", res);
if (res >= 0)
poscount;
else
negcount;
return res;
}
int mul(int num1, int num2){
int res = num1 * num2;
printf("Result: %d\n", res);
if (res >= 0)
poscount;
else
negcount;
return res;
}
int myDiv(int num1, int num2) {
int res = num1 / num2;
printf("Result: %d\n", res);
return res;
}
int main() {
int num1;
printf("Number: ");
scanf("%d", &num1); //Zahl 1
char op = 'X'; // placeholder value
while (op != '=') {
printf("\nOperator: ");
scanf(" %c", &op); //Operator
if (op == '=') { //Überprüfung ob User weiterrechnen will
printf("Final result: %d\n", num1);
printf("Number of positive: %d\n", poscount);
printf("Number of negatives: %d\n", negcount);
break;
}
printf("\nNumber: ");
int num2;
scanf("%d", &num2); //Zahl
if (op == ' ') {
num1 = add(num1, num2); //Addition
}
else if (op == '-'){
num1 = sub(num1, num2); //Subtraktion
}
else if (op == '*'){
num1 = mul(num1, num2); //Multiplikation
}
else if (op == '/'){ //Divisionen
if (num2 == 0) {
printf("Division by 0."); //Ausnahme Division durch 0
printf("No new result.");
}
else {
num1 = myDiv(num1, num2);
}
}
else {
printf("Unknown operator");
}
}
return 0;
}