c - 我的程序在到达 C 中的 scanf() 语句后崩溃并停止工作

标签 c switch-statement

我只是使用 CodeBlocks 用 C 语言构建了一个简单的计算器。构建并运行它之后,一切都很顺利,直到 scanf() 语句为止。在我输入 2 个数字供程序扫描并按 Enter 键后,程序崩溃并发送消息“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;
}

最佳答案

您在 switch 内的 scanf 调用是错误的。您需要&

更改:

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

您应该重新考虑 mainCalculator 中的 return 语句

关于c - 我的程序在到达 C 中的 scanf() 语句后崩溃并停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51174095/

相关文章:

c - libsasl2 在 OSX Yosemite 上是否损坏?缺少 sasl_client_done

c - 如何为具有 SRC、OBJ 和 BIN 子目录的 C 项目创建 Makefile?

无法将值存储在库中定义的结构上

c++ - 如果使用较小的数字,switch case 会更快吗?

Python "Switch-Case"替代显示错误

c - 结构数组作为参数传递给 read() 问题

c - 为什么在 C 中当 n>=34 时使用数组存储阶乘值会输出错误值

java - 为什么 final Byte 作为 switch 语句中的 case 不能编译?

带有 switch 语句的 Swift 范围错误

swift - 误用 guard 语句代替 nil 检查