1. ## simple calculator

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:
//
//- 	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, total;
char operand ;
value = 0;
total = 0;

printf("+ 	add\n- 	subtract\n* 	multiply\n/ 	divide\n^ 	power \nq	quit\n");
printf("0\n");
do
{
scan_data(&value, &operand );
printf("%c%.2f \n", operand , value);
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("%c%f", &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 ;
}```
This Is my code and the question is commented at the beginning. I'm supposed to use pointers not arrays. It keeps telling me within my scan_data function that y becomes corrupt which I'm assuming will happen to x as well.

2. Code:
```void scan_data ( float *x1, char *y1)
{
float x;
char y;
scanf("%c%f", &x, &y);

*x1 = x;
*y1 = y;
}```

3. Thank you. Didn't see that. My program still wont work though. It wont change the value of total and when multiplying or dividing it goes into infinite loop.

4. scanf leave a newline in the input buffer. The second time it gets called, you get a newline put in your char variable, and after that it just gets worse. You need to flush the input buffer after using it.

Try modifying your scan data function like so:

Code:
```void scan_data ( float *x1, char *y1)
{
char garbage;
scanf("%c%f", y1, x1); //You don't actually need all those extra variables
while((garbage=getchar())!='\n' && garbage!=EOF); //discards characters from the input buffer until it either eats a newline, or runs out of input
}```
I haven't tested this, but it should do the trick.

5. 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:
//
//- 	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 ;
}```