将简单程序翻译成c ++时出错

时间:2014-10-21 00:52:15

标签: java c++

我是C ++和Java的新手。我成功地用Java编写了一个简单的计算器,但是我的C ++ - 翻译崩溃了Access violation on writing to 0

我知道这表示NULL - 指针有问题,但我看不到。它有时会运行但有时会崩溃所以我不知道如何跟踪和调试它。

C ++

int main() {
    bool success = false;
    double total = 0.0;
    string line = "text";
    int in = 1;
    do{
        cout << "\nPlease enter simple equation\n";
        double operands[2];
        getline(cin, line);
        char input [10];
        strcpy_s(input, line.c_str());
        int j = 0;
        int i = 0;
        while(i < sizeof(operands)){
            string str = "";
            if(input[j] <= 57 && input[j] >= 48 || input[j] == 46){
                while(input[j] <= 57 && input[j] >= 48 || input[j] == 46){
                    str += (input[j]);
                    if(j+1 < sizeof(input))
                        j++;
                    else
                        break;
                }
                operands[i] = stod(str);
                i++;
            }else
                j++;
        }
        for (int o = 0; o < sizeof(input); o++){
            switch (input[o]){
            case 43:
                total = operands[0] + operands[1];
                break;
            case 45:
                total = operands[0] - operands[1];
                break;
            case 42:
                total = operands[0] * operands[1];
                break;
            case 47:
                total = operands[0] / operands[1];
            }
        }
        if(total){
            cout << total;
        }else{
            cout <<"Your input is incorrect! Please enter a valid equation";
            cout<< "Ex. 1 + 1";
        }
    }while(!success);
}

爪哇

    Scanner in = new Scanner(System.in);
    boolean success = false;
    Double total = null;
    while (!success) {
        System.out.println("\nPlease enter simple equation: ");
        try {
            double[] operands = new double[2];
            String line = in.nextLine();
            char[] input = line.toCharArray();
            StringBuilder str;
            int j = 0;
            int i = 0;
            while (i < operands.length) {
                str = new StringBuilder();
                if (input[j] <= 57 && input[j] >= 48 || input[j] == 46) {
                    while (input[j] <= 57 && input[j] >= 48 || input[j] == 46) {
                        str.append(String.valueOf(input[j]));
                        if (j + 1 < input.length) {
                            j++;
                        } else
                            break;
                    }
                    operands[i] = Double.parseDouble(str.toString());
                    i++;
                } else
                    j++;
            }
            for (int o = 0; o < input.length; o++) {
                switch (input[o]) {
                case 43:
                    total = operands[0] + operands[1];
                    break;
                case 45:
                    total = operands[0] - operands[1];
                    break;
                case 42:
                    total = operands[0] * operands[1];
                    break;
                case 47:
                    total = operands[0] / operands[1];
                }
            }
            if (total != null) {
                System.out.println(line + " = " + total);
            }else{
                System.out.println("Your input is incorrect! Please enter a valid equation");
                System.out.println("Ex. 1 + 1");
            }
        } catch (Exception e) {
            System.out.println("Your input is incorrect! Please enter a valid equation");
            System.out.println("Ex. 1 + 1");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,sizeof关键字以字节为单位给出变量(或类型)的大小。这导致循环运行太多次,导致缓冲区溢出。 如果您将语句更改为

while (i < sizeof(operands) / sizeof (operands[0]) ){

这将使它仅循环两次。 (注意,您不需要将参数放在括号中的sizeof,除非它是一种类型)。

下一步应该是通过使用更多类似c ++的代码来了解如何使程序更简单(例如,不需要复制到char数组,因为您可以使用以下代码访问std :: string&#39; s字符数组访问运算符)。

注意:你也应该阅读评论,因为那里有很多好的建议。