``````#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 intruction();
accum = 0 ;

do
{
//get input
cout<< " : " ;
cin >> input;
cin >> num;
cin.ignore();
// 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 '/':
total /= num;
break;

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

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

}

void divide_by_zero ()
{
}``````

I don't know how to write the divide by zero function, and I don't know why the void function to display the instruction does not run too,

The program now run well, but when I input something weird like a,b,c + c,+d... etc . It runs to the definite loop

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Denniz

For hundreth time: you don't call void function like: `void myfunc()` but by simply typing: `myfunc()` And what do you want with divide_by_zero()?

I don't know how to write the divide by zero function, and I don't know why the void function to display the instruction does not run too,

Anything divide by zero will become infinity. So you need to cater for the case whereby the operator is "/" and the operand num is 0. If such case is met, instead of performing the computation, maybe you should displaying some error message.

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.