I copied a simple "calculator" example from my textbook that accepts 2 values and an operator. However, it does not check if the expression entered is valid. The code should prompt the user until a proper expression is entered.
How should I fix it with my style in mind? What is a better way of doing this?
/*
Input: A simple expression with 2 values and 1 operator
Format: value1 (operator) value2
Ex: 5*5
Output: Answer
*/
#include <stdio.h>
int main (void)
{
float value1, value2;
char operator = ' ';
//Issue Area, the while section does not work
do
{
printf ("Type in your expression:");
scanf ("%f %c %f", &value1, &operator, &value2);
}
while ((operator != '+' || operator != '-' || operator != '*' || operator != '/'));
//This is fine, code is for an else...if example in textbook
if (operator == '+')
printf ("%.2f\n",value1 + value2);
else if (operator == '-')
printf ("%.2f\n",value1 - value2);
else if (operator == '*')
printf ("%.2f\n",value1 * value2);
else if (operator == '/')
printf ("%.2f\n",value1 / value2);
return 0;
}