如果用户除以 0,我在检查错误时遇到了一点麻烦。如果用户请求除以 0,结果字符串应该表明执行除以零是非法的。它应该是这样的:
您的操作是分裂:6/0 非法操作!
但是我不知道如何让它看起来像那样。到目前为止,我只能看到std::cout
“这是非法的”消息,但输出最终看起来像这样:
这是非法的!你的操作是:6 / 0=-858993460
我还尝试在 else 语句中将结果设置为不同的字符串。它只是忽略它。
我将如何最终编码?
void Simplecalc::setOperation(char oper, int op1, int op2)
{
operation = oper;
number1 = op1;
number2 = op2;
if(operation == '+')
{
answer = number1 + number2;
type ="addition";
}
else if(operation =='-')
{
answer = number1 - number2;
type ="subtraction";
}
else if(operation =='*')
{
answer = number1 * number2;
type = "multiplication";
}
else if(operation =='/')
{
if(number2 != 0)
{
answer = number1/number2;
type = "division";
}
else
{
cout<<"this is illegal!";
}
}
else if(operation =='%')
{
answer = number1%number2;
type ="modulus";
}
}
string Simplecalc::getResults()
{
stringstream ss;
ss <<"Your operation is "<<type<<": "<<number1<<" "<<operation<<" "<<number2<<"="
<<answer<<"\n";
results =ss.str();
return results;
}