Question: Write a four function calculator (+,-,*,/) that works with an arbitrary number of inputs. This means that instead of just taking two inputs it takes
Write a four function calculator (+,-,*,/) that works with an arbitrary number of inputs. This means that instead of just taking two inputs it takes numbers and operations until the user decides to quit (the program runs in an infinite loop). Give updates on the current total after each operation and allow the user the choice to continue or choose a new operation each time. Total starts at 0.0 and the menu choice 5 is used to signal end of computation. Place the add, subtract, multiply, and divide functionality inside independent functions. Implement these functions:

This is my code so far in C. However, what can I add, fix, remove inorder to get it to compile like the image.
The function prototypes have to remain as they are!
#include
// Function Prototypes
double add(double sum ,double value); double subtract(double sum ,double value); double multiply(double sum ,double value); double divide(double sum ,double value);
int main(void) { int n; char ch; double sum; double value; while(1) { printf("1: add "); printf("2: subtract "); printf("3: multiply "); printf("4: divide "); printf("5: quit "); printf("Enter operation choice "); scanf("%d", &n); //printf("enter the value:"); //scanf("%lf",&value); switch(n) { case 1: add(sum,value); printf("enter number to add: "); scanf("%lf", &value); break; case 2: subtract(sum,value); break; case 3: multiply(sum,value); break; case 4: divide(sum,value); break; case 5: exit(1); } } return 0; }
double add(double sum,double value) { sum = sum + value; //printf("the result: %d",sum); return sum;
} double subtract(double sum,double value) {
sum = sum - value; // printf("the result: %d",sum); return sum; } double multiply(double sum,double value) {
sum = sum * value; //printf("the result: %d",sum); return sum; } double divide(double sum,double value) { if(value == 0) { printf("zero not appilcable"); } else { sum = sum / value; //printf("the result: %d",sum); return sum; } }
1 add 2: subtract 3: multiply 4 divide 5: quit enter number to add: Total is: 5.000000 1 add 2: subtract 3: multiply 4 divide 5: quit enter number to multiply Total is 25.000000 1 add 2: subtract 3: multiply 4 divide 5: quit enter number to divide can not divide by 0, ignoring value and continuing Total is 25.000000 1 add 2: subtract 3: multiply 4 divide 5: quit Final total is: 25.000000
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
