Jenny
Jenny

Reputation: 545

error translating a simple program to c++

I am very new to C++ and Java. I succeeded in writing a simple calculator in Java, but my C++-translation crashes with an Access violation on writing to 0.

I know that indicates a problem with a NULL-pointer, but I do not see where. Also it sometimes runs but sometimes crashes so I have no idea how to trace and debug it.

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);
}

Java

    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");
        }
    }
}

Upvotes: 1

Views: 142

Answers (1)

The Dark
The Dark

Reputation: 8514

As mentioned in the comments, the sizeof keyword gives the size of a variable (or type) in bytes. This is causing your loop to run too many times, leading to a buffer overflow. If you change the statement to

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

that will make it loop only twice. (note you don't need to put the parameters to sizeof in brackets, unless it is a type).

The next step should be to see how to make the program simpler by using more c++ like code (for example, no need to copy to a char array as you can access the std::string's characters using the array access operator).

Note: You should also read the comments, as there are a lot of good suggestions there.

Upvotes: 1

Related Questions