I made this do while loop calculator program in C for our school activity, but it only work once. I expect it to work every time the user inputs 1 in the prompt.
When I run the code, the program works as expected. It functions as calculator but when I try to put the number "1" in the prompt at the end, it will loop but in the "Do you want to continue part only.
Here is the code:
#include <stdio.h>
int main() {
int again;
double num1, num2;
const double PI = 3.1415;
char op;
do {
printf("Enter '+' to use addition.\n");
printf("Enter '-' to use subtraction.\n");
printf("Enter '*' to use multiplication.\n");
printf("Enter '/' to use division.\n");
printf("Enter 'c' to calculate the circumference of a circle.\n");
printf("Enter 'a' to calculate the area of a circle.\n");
printf("Enter what operation to do: ");
scanf("%c", &op);
switch(op) {
case '+':
printf("Enter a number: ");
scanf("%lf", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("The answer is: %lf", num1 + num2);
break;
case '-':
printf("Enter a number: ");
scanf("%lf", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("The answer is: %lf", num1 - num2);
break;
case '*':
printf("Enter a number: ");
scanf("%lf", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("The answer is: %lf", num1 * num2);
break;
case '/':
printf("Enter a number: ");
scanf("%lf", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);
printf("The answer is: %lf", num1 / num2);
break;
case 'c':
printf("Enter radius: ");
scanf("%lf", &num1);
printf("The answer is: %lf", 2 * PI * num1);
break;
case 'a':
printf("Enter radius: ");
scanf("%lf", &num1);
printf("The answer is: %lf", PI * num1 * num1);
break;
}
printf("\nDo you want to continue? Type '1' to continue and '2' to stop. \n");
scanf("%d", &again);
}while(again == 1);
return 0;
}
Sorry if I can't explain the problem well, English is not my native language so please bear with me.