Im making a C calculator using switch case. It has a endless while loop b ut after the first loop it stops working? Here is my code:
#include <stdio.h>
int main() {
char opr;
double x, y;
int i = 1;
while (i < 2) {
printf("\nPick an operator (+, -, *, /): ");
scanf("%c", &opr);
getchar();
printf("\nPick 2 numbers ");
scanf("%lf %lf", &x, &y);
switch (opr) {
case '+':
printf("\n%.1lf + %.1lf = %.1lf\n", x, y, x + y);
break;
case '-':
printf("\n%.1lf - %.1lf = %.1lf\n", x, y, x - y);
break;
case '*':
printf("\n%.1lf * %.1lf = %.1lf\n", x, y, x * y);
break;
case '/':
if (y == 0) {
printf("\nY can't be 0. Choose a new value: ");
scanf("%lf", &y);
}
printf("\n%.1lf / %.1lf = %.1lf\n", x, y, x / y);
break;
default:
printf("\nPick a valid operator.\n");
}
printf("\n***************************************************\n");
}
}
After the first loop it says "Pick a valid operator" even when i do. Help appreciated because im a newbie.