0
#include <math.h> 
#include <iostream> 

using namespace std;

void instruction();
float clean (char , float ,float );
float do_next_op (char, float, float);
float clean1 (char input, float num);
int main()
{ 
	//input
	char input;
	float accum,num;

	// instructions ();
void instruction();
float clean (char op1, float total,float num);
float clean1 (char input, float num);
	accum = 0 ;

	do
	{ 
		//get input 
		cout<< " : " ;
		cin >> input;
		cin >> num;

// CHECK NUMBER
		if (cin.fail())
		{
			cerr << "num is invalid" <<  ". Result so far : " << accum << endl <<endl;
			cin.clear();
			while (cin.get() != '\n') {}; // get all characters from buffer -> clear buffer
		}
		else // Do op
		{
			accum = do_next_op (input, num, accum);
			//display result
			cout << " Result so far : " << accum << endl;
		}

		cout << "  "<< endl;
	} // end not quit
	while (input != 'Q' && input != 'q' && input != '=');

	cout << "Final result : " << accum<< endl;
	system ("pause") ;  
	return 0 ;
}

//instruct user 
void instruction()
{ 
	cout <<" This program models a simple calculator which can add, subtract,multiply,divide, power" << endl;
	cout << " the calculator accumulate value after each operation " << endl;
	cout << " press Q to quit the program " << endl;
	cout << " Have fun with my program " << endl<< endl;
}

float do_next_op (char op, float num, float total)
{
	switch (op)
	{
		case '+' :
			total += num;
			break;

		case '-':
			total -= num;
			break;

		case '*':
			total *= num;
			break;

		case '/': 

// CHECK DIVIDE BY ZERO
			if (num == 0)
			{
				cerr << "Divide by zero" << endl;
			}
			else total /= num;
			break;

		case '^':
			total = pow (total,num);
			break ;

		default :
			cout << " syntax error"	<< endl;
	}

	return total;
}
//clean function

float clean (char op1, float num1, float total1)
{
      switch (op1)
      {
             case 'C': 
                  total1 = 0;
                  cout << " return to 0 - begin your calculation again" << endl;
                  cout << " : " ;
                  cin >> total1;
                  break ;
                       
                                }
                                return total1 ;
                                }  
float clean1 (char input, float num)

while ......
      (input != 'C' && input != 'c' );
cout << " return to 0 - begin your calculation again" << endl;
                  cout << " : " ;
                  cin >> num;

this time I tried to add the function which can clean return to the value 0, I tried 2 ways but neither of them run well, so I post from that you can help me. thank you

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Denniz
0

You don't need an extra function. Just add one more case option inside the function do_next_op.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.