Hi,
Currently working through Stroustrup - Programming Principles and having problem with a particular function he advises to use:
Code:
#include <iostream>
using namespace std;
void error(const string& s)
{
throw runtime_error(s);
}
int main()
{
cout << "Please enter an expression (we can handle +, -, / or *): ";
int lval = 0;
int rval;
char op;
cin >> lval;
if(!cin) error("no first operand");
while(cin>>op){
cin >> rval;
if(!cin) error("no second operand");
switch(op){
case '+':
lval+=rval;
break;
case '-':
lval-=rval;
break;
case '*':
lval *=rval;
break;
case '/':
lval /= rval;
break;
default:
cout << "Result: " << lval << '\n';
}
}
error ("bad expression");
system("pause");
return 0;
}
When I enter bad input to call the error function I get this message: Unhandled exception at 0x761c42eb in Accelerated C++.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0022fb54..
Anybody any idea why?
Thanks.