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

using namespace std;

void instruction();
int calculator (char);
void divide_by_zero ();
float do_next_op (char, float, float);

int main()
{ 
	//input
	char input;
	float accum,num;

	// instructions ();
void instruction();
	accum = 0 ;

	do
	{ 
		//get input 
		cout<< " : " ;
		cin >> input;
		cin >> num;
//EIDEN
//		cin.ignore(); -> don't have to put ignore in this line
// 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 : " ;
	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 '/': 
        float  divide_by_zero (char op,  float num);
// CHECK DIVIDE BY ZERO
		 
      break;
		case '^':
			total = pow (total,num);
			break ;

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

	return total;
}

float  divide_by_zero (char op,  float num);
switch (op) 
{
       case '/';
       if (num == 0 )
       	{
				cerr << "Divide by zero" << endl;
			}
			else total /= num;
			break;
			return total;
        }

I tried to insert the function divide by zero, but I dont' kbnow why it doe snot run, and also the instruction function....
really confused right now

Do you know the difference between declaring a function and calling a function?

To declare a function, you do this:

void instruction();
void divide_by_zero (char, float);
float do_next_op (char, float, float);

To call a function, you do this:

instruction();
divide_by_zero (op, num);
accum = do_next_op (input, num, accum);

Notice the difference. When declaring a function, like what you do at the beginning of your code, you specify the function name, the return type, and the type of parameter to pass to the function.

When calling a function, you just write the function name, and the ACTUAL variables which you want to pass into the function.

Also, remember the function declaration must MATCH the function definition.


Your last portion of codes have a few problems:

float  divide_by_zero (char op,  float num);
switch (op) 
{
       case '/';
       if (num == 0 )
       	{
				cerr << "Divide by zero" << endl;
			}
			else total /= num;
			break;
			return total;
        }

1. Take out the semicolon that follows the function. You need open and close braces to specify the scope of the function. Like this:

float  divide_by_zero (char op,  float num)
{
  // Your code here

  return total;
}

2. All your open and close braces within the function are in a mess. Go settle it.

3. Go look up the syntax for switch statement.

4. If I were you, I would not check for "/" in the divide_by_zero function because you already check that in the calling function. I would check whether num is zero in the CALLING function. If num is zero, I will call divide_by_zero function and display the error message. Else, I would proceed with the normal divide operation inside the CALLING function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.