0
make calculator that exit if user entering letter in C languange
I want to make a simple calculator using switch cases and functions. The operators used are addition, subtraction, multiplication, and division. Each case will execute its own function from the arithmetic operation. The program will be completed if the input provided is not a number (it can be letters or other characters). how to make it?
5 Antworten
+ 5
You better show us your trial
0
/* C Program to Create Simple Calculator using Switch Case */
#include <stdio.h>
float add(float a, float b);
float sub(float a, float b);
float multi(float a, float b);
float div(float a, float b);
int main()
{
char Operator;
float num1, num2, result = 0;
menu:
printf ("Enter num 1 = ");
scanf ("%f%c",&num1);
printf ("Enter Operator = ");
scanf (" %c",&Operator);
printf ("Enter num 2 = ");
scanf ("%f",&num2);
switch(Operator)
{
case '+':
result = add(num1, num2);
break;
case '-':
result = sub(num1, num2);
break;
case '*':
result = multi(num1, num2);
break;
case '/':
result = div(num1, num2);
break;
default:
printf("\n You have enetered an Invalid Operator ");
break;
}
printf("\n The result of %.2f %c %.2f = %.2f\n\n", num1, Operator, num2, result);
goto menu;
return 0;
}
float add(float a, float b)
{
return a + b;
}
float sub(float a, float b)
{
return a - b;
}
float multi(float a, float b)
{
return a * b;
}
float div(float a, float b)
{
return a / b;
}
0
how to do the command to check user input,if its letter,it will exit?
0
In default case instead of break use exit(0); or return 0;
And also in 1st scanf you have extra %c, remove that...
0
what if i want, when i entering letter in num1 and num2 variable it will exit too? if i change the default () to be exit() or return (0) it will exit when i only enterring letter in operator variable?