0

When I run this program from the msys command line, it tells opens a window called calc.exe (name of program) with the following error: calc.exe has encountered a problem and needs to close. We are sorry for the inconvenience.

here is my main function:

int main(int argc, char *argv[])
{
    int num1, num2, ans;

    hist_file_create();

    if (argc < 2 || argc > 2)
        kill("ERROR: Use: calc <operation_type>");

    char operation_type = argv[2][0];

    switch(operation_type)
    {
        // Addition
        case 'a':
            printf("Enter a number:\n");
            scanf("%d", &num1);
            printf("Enter a second number\n");
            scanf("%d", &num2);
            ans = num1 + num2;
            printf("%d + %d = %d", num1, num2, ans);
            break;

        // Operation type list
        case 'l':
           operation_type_list();
           break;

        default:
            kill("Invalid operation type. To see a list of recognised operation types, type l");
    }
    system("PAUSE");

}

I know it is still probably very messy, but right nnow I am just trying to make it work, and clean it up later. Any additional information you may need I will give.

4

1 回答 1

1

This condition makes sure that you have exactly 2 arguments or the program exists if (argc < 2 || argc > 2) kill("ERROR: Use: calc ");

However the next line is accessing the third argument char operation_type = argv[2][0];

Which does not exist. And the program is killed

于 2013-10-27T05:58:06.430 回答