im new to programming and im a first year in college, my teacher gave me and my group mates a task to create a console calculator but we run into a problem since this is our first problem.
the problem is that if the input does not satisfied or met any condition in my if statement it loops the error.
here is the code:
#include <stdbool.h>
double calculate(double number1, char operation, double number2);
bool isInteger(double val)
{
int truncated = (int)val;
return (val == truncated);
}
int main()
{
double number1, number2, result;
char operation;
int validator = 0, operationValid = 0;
while(validator == 0)
{
fflush(stdin);
printf("Enter number and operator to solve:\n\nExapmple [a + b][a - b][a * b][a / b]\n");
scanf("%lf %c %lf", &number1, &operation, &number2);
while(operationValid == 0)
{
fflush(stdin);
if(operation == '+' || operation == '-' || operation == '*' || operation == '/')
{
if(isInteger(number1) && isInteger(number2))
{
result = calculate(number1, operation, number2);
printf("%.1lf %c %.1lf = %.1lf", number1, operation, number2, result);
operationValid = 1;
validator = 1;
}
else
{
printf("Invalid Inputed numbers!");
operationValid = 0;
validator = 0;
}
}
else
{
printf("Invalid Operation!");
operationValid = 0;
validator = 0;
}
}
}
}
double calculate(double number1, char operation, double number2)
{
double a, b, c;
char x;
a = number1;
b = number2;
x = operation;
if(x == '+')
{
c = a + b;
}
else if(x == '-')
{
c = a - b;
}
else if(x == '*')
{
c = a * b;
}
else if(x == '/')
{
c = a / b;
}
return c;
}
please if someone can provide help and maybe a little explanation. Thanks.