I am writing a Prefix Calculator program and because of the way prefix is structured, it is difficult to deal with dividing by zero. Fortunately, C++ has a built in exception if it divides by zero. How would I handle the following exception so that it returns a personalized message to the console rather than popping this window up? I NEED THIS WINDOW TO NOT POP UP WHEN I DIVIDE BY ZERO.
template<class T>
T PrefixCalculator<T>::eval(istringstream& input) { //this function needs to throw an exception if there's a problem with the expression or operators
char nextChar = input.peek();
//handles when invalid characters are input
if(nextChar > '9' || nextChar == '.' || nextChar == ',' || nextChar < '*' && nextChar != 'ÿ') {
throw "invalidChar";
}
//this while loop skips over the spaces in the expression, if there are any
while(nextChar == ' ') {
input.get(); //move past this space
nextChar = input.peek(); //check the next character
}
if(nextChar == '+') {
input.get(); //moves past the +
return eval(input) + eval(input); //recursively calculates the first expression, and adds it to the second expression, returning the result
}
/***** more operators here ******/
if(nextChar == '-') {
input.get();
return eval(input) - eval(input);
}
if(nextChar == '*') {
input.get();
return eval(input) * eval(input);
}
if(nextChar == '/') {
input.get();
return eval(input) / eval(input);
}
/****** BASE CASE HERE *******/
//it's not an operator, and it's not a space, so you must be reading an actual value (like '3' in "+ 3 6". Use the >> operator of istringstream to pull in a T value!
input>>nextChar;
T digit = nextChar - '0';
return digit;
//OR...there's bad input, in which case the reading would fail and you should throw an exception
}
template<class T>
void PrefixCalculator<T>::checkException() {
if(numOperator >= numOperand && numOperator != 0 && numOperand != 0) {
throw "operatorError";
}
if(numOperand > numOperator + 1) {
throw "operandError";
}
}