I am writing a program that will act as a simple calculator when it is complete. The instructions state to use one function that returns 2 output parameters and another to do the calculations. I am stuck trying to get my accumulator to keep the result. This version runs but the last input is always the output that is printed. Any hints?
Code:
#include <stdio.h>
#include <math.h>
//Function prototypes
void scan_data (char *opp, double *nump);
void do_next_op (char op, double num, double *accup);
int main (void)
{
//declare variables, not sure if the accumulator ACCU needs to be declared here
double num, accu;
char op;
//Start do while loop to accept input from user until Q or q is chosen
do
{
//Call functions until to get input and perform calculations
scan_data(&op, &num);
do_next_op (op, num, &accu);
}
while (op != 'q' && op != 'Q');
//Print end result when Do While is exited
printf ("The end result is %lf.\n", &accu);
}
//Get data function
void scan_data (char *opp, double *nump)
{
double num;
char op;
printf ("Please enter an operator and number: ");
scanf(" %c%lf", &op, &num);
while (op != '+' && op != '-' && op != '*' && op != '/' && op != '^' && op != 'q' && op != 'Q')
{
fflush (stdin);
printf ("Please enter an operator and Number: ");
scanf(" %c %lf", &op, &num);
}
*opp = op;
*nump = num;
}
//Here is where I think the problem lies, I reset the accumulator to 0 each time I enter the function, not sure how to fix it.
//I'm also not sure how to use the ACCU and *ACCUP as an input and output variable.
void do_next_op (char op, double num, double *accup)
{
double accu = 0;
if (op == '+')
{
*accup = accu + num;
printf ("The result so far is %lf\n", *accup);
}
else if (op == '-')
{
*accup = accu - num;
}
}