That is a beginner question to understand where I might be wrong using the switch statement.
The task is as follows:
Write a program that accepts two integer values typed in by the user. Display the
result of dividing the first integer by the second, to three-decimal-place accuracy.
Remember to have the program check for division by zero.
To test whether the second operand is a 0, I have used switch statement (I know I could have done that with if…else). However, the program will still accept 0 as the operand I am unsure why this happens. If it tries to calculate the division by 0, the program will simply fail. Could you please give me a hint as to where I might be wrong? My code is below:
#include <stdio.h>
int main (void)
{
int a, b;
printf ("Type two integer values: ");
scanf ("%i %i", &a, &b);
switch (a/b)
{
case 'b == 0':
printf ("\n The divisor can't be 0.\n");
break;
default:
printf ("The result of dividing %i by %i is %.3f\n", a, b,(float) a / b);
break;
}
return 0;
}
Thanks for your help!
Also note, the expressions following
casemust be constants such as:etc
There.. that should fix it.