如何解决此代码中的分段错误?

How to resolve the segmentation fault in this code?

我正在尝试制作一个中缀计算器,目前我正在尝试将在字符数组中输入的数字转换为双精度数。

这是我的代码:


#include <iostream>
#include<cmath>
using namespace std;

int main()
{
    char exp[500];
    const int SIZE = 100;
    char temp[SIZE];
    char op;

    int strLen = 0, k, l, num = 0, fnum = 0;
    double number = 0;

    cin.getline(exp, 500,'\n');

    int i = 0, j = 0, fpoint=0;

    cout << exp;

    for (i = 0, j = 0; exp[j] != 0; i++)
    {
        if (i % 2 == 0)
        {
            for (int m = 0; exp[m] != ','; m++) //stopped working
                temp[m] = exp[m];
            
            cout << temp;
            
            for (k = 0; k < SIZE && temp[k] != 0; k++)
            {
                strLen = k;
                if (temp[k] == '.')
                    fpoint = k + 1;
            }

            cout << fpoint<<endl;


            cout << "strLen" << strLen;

            for (k = 0; k <= fpoint; k++)
            {
                num = num + ((temp[fpoint - k] - '0') * pow(10, k));
            }

            for (k = fpoint + 1, l = 0; k <= strLen; k++, l++)
            {
                fnum = fnum + ((temp[strLen - l] - '0') * pow(10, l));
            }

            number = num + (fnum / pow(10, strLen - fpoint + 1));

            cout << number;

            j = j + strLen + 1;

        }
        else
        {
            char op = temp[j];
            cout << op;
        }
    }
    system("pause");
    return 0;
}


示例输入

2.5*3

它停止工作并给出分段错误作为标记位置上的错误。

如果没有 , 个字符,此行 for (int m = 0; exp[m] != ','; m++) //stopped working 将始终失败,因为 exp[m] != ',' 将始终等于 true,因此会超出行尾触发“分段错误”的 exp 数组。