Моя программа упала и перестала работать после достижения оператора scanf () в C - PullRequest
0 голосов
/ 04 июля 2018

Я просто строил простой калькулятор на C, используя CodeBlocks. После сборки и запуска все прошло гладко до оператора scanf (). После того, как я ввел 2 числа, чтобы моя программа сканировала и нажала, введите программу, которая вылетела, и отправил сообщение «C.exe прекратил работу». Пожалуйста, помогите. Вот код -

#include <stdio.h>
#include <stdlib.h>

int Calculator();

int main()
{
    Calculator();
    return 7;
}

int Calculator()
{
    int num1;
    int num2;
    int operation;

    printf("Hello. I am a calculator.\nChoose any operation you want to carry out- \n 1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division \n");
    scanf("%d", &operation);

    switch (operation)
    {
        case 1:
            printf("OK. You have chosen addition. \nEnter any 2 numbers to be added \n");
            scanf("%d", num1);
            scanf("%d", num2);
            printf("%d + %d is = %d. \n Thank you. ", num1, num2, num1 + num2);
            break;

        case 2:
            printf("OK. You have chosen subtraction. \nEnter any 2 numbers to be subtracted \n");
            scanf("%d", num1);
            scanf("%d", num2);
            printf("%d - %d is = %d. \n Thank you. ", num1, num2, num1 - num2);
            break;

        case 3:
            printf("OK. You have chosen multiplication. \nEnter any 2 numbers to be multiplied \n");
            scanf("%d", num1);
            scanf("%d", num2);
            printf("%d x %d is = %d. \n Thank you. ", num1, num2, num1 * num2);
            break;

        case 4:
            printf("OK. You have chosen division. \nEnter any 2 numbers to be divided \n");
            scanf("%d", num1);
            scanf("%d", num2);
            printf("%d ÷ %d is = %d. \n Thank you. ", num1, num2, num1 / num2);
            break;

        default:
            printf("\nHUH?\n\n");
            break;
    }
    return 20;
}

Ответы [ 2 ]

0 голосов
/ 04 июля 2018

Вы хотите

scanf("%d", &num1);
scanf("%d", &num2);

в каждом случае.

Кроме того, если вы хотите предотвратить другие сбои, следите за делением на ноль:)

0 голосов
/ 04 июля 2018

Ваши scanf звонки внутри switch неверны. Вам нужно &.

Изменение

scanf("%d", num1);
scanf("%d", num2);

до

scanf("%d", &num1);
scanf("%d", &num2);

Предлагаю вам проверить возврат scanf.

Попытайтесь понять это:

#include <stdio.h>
#include <stdlib.h>

int Calculator( void );

int main( void )
{
    Calculator();
}

int Calculator( void )
{
    int num1;
    int num2;
    int operation;

    printf("Hello. I am a calculator.\nChoose any operation you want to carry out- \n 1.Addition \n 2.Subtraction \n 3.Multiplication \n 4.Division \n");
    if ( scanf("%d", &operation) != 1 ){
        printf( "Error scanf() ==>> on Operation" );
        exit( 1 );
    }

    switch (operation)
    {
        case 1:
            printf("OK. You have chosen addition. \nEnter any 2 numbers to be added \n");
            if ( scanf("%d", &num1) != 1 ){
                printf( "Error scanf() ==>> switch - case 1 ==>> 1" );
                exit( 1 );
            }
            if ( scanf("%d", &num2) != 1 ){
                printf( "Error scanf() ==>> switch - case 1 ==>> 2" );
                exit( 1 );
            }
            printf("%d + %d is = %d. \n Thank you. ", num1, num2, num1 + num2);
            break;

        case 2:
            printf("OK. You have chosen subtraction. \nEnter any 2 numbers to be subtracted \n");
            if ( scanf("%d", &num1) != 1 ){
                printf( "Error scanf() ==>> switch - case 2 ==>> 1" );
                exit( 1 );
            }
            if ( scanf("%d", &num2) != 1 ){
                printf( "Error scanf() ==>> switch - case 2 ==>> 2" );
                exit( 1 );
            }
            printf("%d - %d is = %d. \n Thank you. ", num1, num2, num1 - num2);
            break;

        case 3:
            printf("OK. You have chosen multiplication. \nEnter any 2 numbers to be multiplied \n");
            if ( scanf("%d", &num1) != 1 ){
                printf( "Error scanf() ==>> switch - case 3 ==>> 1" );
                exit( 1 );
            }
            if ( scanf("%d", &num2) != 1 ){
                printf( "Error scanf() ==>> switch - case 3 ==>> 2" );
                exit( 1 );
            }
            printf("%d x %d is = %d. \n Thank you. ", num1, num2, num1 * num2);
            break;

        case 4:
            printf("OK. You have chosen division. \nEnter any 2 numbers to be divided \n");
            if ( scanf("%d", &num1) != 1 ){
                printf( "Error scanf() ==>> switch - case 4 ==>> 1" );
                exit( 1 );
            }
            if ( scanf("%d", &num2) != 1 ){
                printf( "Error scanf() ==>> switch - case 4 ==>> 2" );
                exit( 1 );
            }
            printf("%d ÷ %d is = %d. \n Thank you. ", num1, num2, num1 / num2);
            break;

        default:
            printf("\nHUH?\n\n");
            break;
    }
    return 20;
}

Вам следует пересмотреть свои return заявления от main и Calculator

...