### Dividsion by zero not show error

Hello can someone help i'm having some problems whit my cocculator if i divide 1/0 or 2/0 etc. It isn't write an error its shows 0. Maybe someone can give advise what and in which place i have to write .

#include <stdio.h>
#include <stdlib.h>

{return a+b;};

int substraction(int a, int b)
{return a-b;};

int division(int a, int b)
{return a/b;};

int multiplication(int a, int b)
{return a*b;};

int main() {
int a,b,rez;
char c;
for(;;) {

printf("Welcome to coculator.\n");
printf("Please choose an option by entering the number, press q to quit\n");
printf("2-Substraction\n");
printf("3-Division\n");
printf("4-Multiplication\n");
printf("--------------------------------------------------------------------------\n");

char num;
scanf("%d", &num);

printf(" Enter the first number:\n");
scanf("%d", &a);
printf("Enter the second number:\n");
scanf("%d", &b);

if (num == 1){
printf("%d\n",rez);
}
else if (num == 2){
rez=substraction(a,b);
printf("%d\n",rez);
}
else if (num == 3){
rez=division(a,b);
printf("%d\n",rez);

}
else if (num == 4){
rez=multiplication(a,b);
printf("%d\n",rez);
}
else {
printf("You chose the wrong number! Try another one.");
}
scanf("%c", &c);
if (c == 'q')
break;
Last edited on

printf("3-Division\n");

However then in the seria of if-else statements the following code corresponds to selection with number 3

else if (num == 3){
rez=multiplication(a,b);
printf("%d\n",rez);

}

So instead of division you perform multiplication.
Last edited on
Thanks i change my mistake maybe you now how to show error when i divided whit 0?

Last edited on
I'm not really familiar with this language but I'm basic c++ you can just make an if statement like

If(number/0)
Cout<<"error, divided by zero"<<endl;
Maybe this will help u
Still problem is not solved :/ but still thanks
This is straight forward.

You simply cannot divide by 0 - so there is no point in carrying out the
divison function.

 ``1234567891011121314`` ``````else if (num == 3) { if (b ==0) { printf ("Not possible to divide by zero"); } else //only carry out the division if denominator is not zero { rez=division(a,b); printf("%d\n",rez); } }``````

Thank you very much :)
Try to avoid infinite loops like this:

`for(;;) {`

You would be much better to make use of a bool variable and test that in a while loop condition.

 ``123456789`` ``````bool Quit = false; while(!Quit) { // your code //user wants to quit Quit = true; } ``````

There are valid situations to use infinite loops, but this isn't one of them.
Topic archived. No new replies allowed.