怎么对错误输入进行提示

写了一个中缀表达式计算器,要求对错误输入进行提示,比如计算后被除数是0、运算符多了、少了等等情况。我的思路是把中缀转后缀,然后计算,这部分已经写出来了,但是我不知道怎么提示错误TT
(我想知道思路,不过如果有现成的就可遇不可求啦😃)

img


#include <bits/stdc++.h>
#include <stack>
#include <cmath>
#include <string>
using namespace std;

int main()
{
    cout<<"Enter some infix expression,such as 1+1(must be integers,Ctrl+z to end):"
        <<endl;

start:    string line; //使用标号语句和跳转语句,当输入的表达式非法时可以便捷地跳回到此处,
                        //重新输入数据
    while(getline(cin,line))
    {
        stack<double> numStack;
        stack<int> expStack;

        expStack.push(-2);

        string s="";
        int prop=0,first=0,num=0; //prop表示运算符的优先级
        double val1=0.0,val2=0.0,result=0.0;
        char ch;

        //getline(cin,line);
        line+='#'; //自己设置停止时刻
        for(int i=0;line[i]!='#';++i)
        {
            ch=line[i];

            if(isdigit(ch))
                s+=ch;
            else
            {
                //当s不为空时才进行转换
                if(s!="")
                {
                    num=atoi(s.c_str());
                    numStack.push(num);
                    s=""; //将s清空
                }

                if(ch==')')
                {
                    while(expStack.top()!=-2&&expStack.top()!=-7) //还没到栈底,并且不是左括号时
                    {
                        first=expStack.top();
                        expStack.pop();

                        val1=numStack.top();
                        numStack.pop();
                        val2=numStack.top();
                        numStack.pop();

                        switch(first)
                        {

                        case 1:
                            result=val2+val1;
                            break;
                        case 2:
                            result=val2-val1;
                            break;
                        case 4:
                            result=val2*val1;
                            break;
                        case 5:
                            //当输入除数为0时可能存入val1的不为0,可能很接近0,只要val1足够接近0就认为是0
                            if(val1<0.0000001)
                            {
                                cerr<<"The divider can't be 0! input again:"<<endl;
                                //exit(1);
                                goto start;
                            }
                            result=val2/val1;
                            break;
                        }
                        numStack.push(result);
                    }

                    //将左括号删去
                    if(expStack.top()==-7)
                        expStack.pop();
                    else
                    {
                        cout<<"'(' and ')' can't match! input again:"<<endl;
                        goto start;
                        //exit(1);
                    }
                }

                else
                {
                    switch(ch)
                    {
                    case '+':
                            prop=1;
                            break;
                    case '-':
                            prop=2;
                            break;
                    case '*':
                            prop=4;
                            break;
                    case '/':
                            prop=5;
                            break;
                    case '(':
                            prop=7; //左括号栈外的优先级最高,可直接入栈

                    }
                    first=expStack.top();

                    //说明栈顶符号的优先级大于读入的优先级
                    if(prop-first<2)
                    {
                        expStack.pop(); //除去运算符栈栈顶元素

                        //取出两个操作数
                        val1=numStack.top();
                        numStack.pop();
                        val2=numStack.top();
                        numStack.pop();

                        switch(first)
                        {
                        case 1:
                            result=val2+val1;
                            break;
                        case 2:
                            result=val2-val1;
                            break;
                        case 4:
                            result=val2*val1;
                            break;
                        case 5:
                            if(0==val1)
                            {
                                cerr<<"The divider can't be 0! input again:"<<endl;
                                goto start;
                                //exit(1);
                            }
                            result=val2/val1;
                            break;
                        }
                        numStack.push(result); //将运算的结果重新压到操作数栈中
                    }

                    if(7==prop)
                        expStack.push(-7); //-7是左括号的标志
                    else
                        expStack.push(prop); //将新来的运算符压入运算符栈
                }
            }
        }

        //最后一个输入的操作数单独处理
        if(s!="")
        {
            num=atoi(s.c_str());
            numStack.push(num);
        }


        //到了expStack的栈顶

        while(expStack.top()!=-2)
        {

            first=expStack.top();

            //如果还剩左括号时,输出错误信息
            if(first==-7)
            {
                cout<<"'(' and ')' can't match! input again:"<<endl;
                goto start;
                //exit(1);
            }

            expStack.pop();

            val1=numStack.top();
            numStack.pop();
            val2=numStack.top();
            numStack.pop();

            switch(first)
            {

            case 1:
                result=val2+val1;
                break;
            case 2:
                result=val2-val1;
                break;
            case 4:
                result=val2*val1;
                break;
            case 5:
                if(val1<0.0000001)
                {
                    cerr<<"The divider can't be 0! input again:"<<endl; //除数不能为0
                    goto start;
                    //exit(1);
                }
                result=val2/val1;
                break;
            }

            numStack.push(result);

        }

        cout<<"result:"<<numStack.top()<<endl;
    }

    return 0;


}