Sorry to make you go through the effort I edited the code and realized I was entering the values in the wrong order. It works with this new code now. And thank you for the help.
Code:
//Q3- Write a program to model a simple calculator. Each data line should consist of the next operation
//to be preformed from the list below and the right operand. Assume the left operand is the accumulator value
// (initial value of 0). You need a function scan_data with two output parameters that returns the operator
// and right operand scanned from the data line. You need a function do_next_op that performs the required operation.
// do_next_op has two input parameters (the operator and operand) and one input/output parameter (the accumulator). The valid operators are:
//
//+ add
//- subtract
//* multiply
/// divide
//^ power (raise left operand to the power of the right operand)
//q quit
//
//Your calculator should display the accumulator value after each operation. A sample run follows.
//+ 5.0
//Result so far is 5.0
//^ 2
//Result so far is 25.0
/// 2.0
//Result so far is 12.5
//q 0
//final result is 12.5
#include <stdio.h>
#include <math.h>
void scan_data ( float *x1, char *y1);
void do_next_op(float x, char y, float *z );
int main()
{
float value, *val,*tot, total;
char operand, *oper ;
val = &value;
tot = &total;
oper = &operand;
value = 0;
total = 0;
printf("+ add\n- subtract\n* multiply\n/ divide\n^ power \nq quit\n");
printf("enter in the form of your value then your operator. ie 5-.\n");
printf("0\n");
do
{
scan_data(val, oper );
printf("%c%.2f \n", *oper , *val);
do_next_op(value, operand, &total );
printf("result is %.2f \n", total);
}
while ( operand != 'q');
}
void scan_data ( float *x1, char *y1)
{
float x;
char y;
scanf("%f%c", &x, &y);
*x1 = x;
*y1 = y;
}
void do_next_op(float x, char y, float *z )
{
switch (y)
{
case '+':
{
*z = (*z + x);
break;
}
case '-':
{
*z = (*z - x);
break;
}
case '*':
{
*z = ((*z) * x);
break;
}
case '/':
{
*z = ((*z) / x);
break;
}
case '^':
{
*z = pow( *z , x);
break;
}
}
return ;
}