在 while 循环中执行 scanf 后代码崩溃

标签 c function

问题出在这个函数中。它应该将两个变量的输入验证为整数。我做错了什么? O__O 谢谢 :)

我使用了 if else 语句来检查变量 valid 的变化,以便在给出正确的输入后退出循环。即使我输入了正确的值,代码仍然会崩溃。

void input(int *n1, int *n2, char *opt)
{
    int valid = 0;
    int v2 = 0;
    char choice;
    int a, b;


    while (v2  == 0)
    {
        printf("Enter first number: \n");
        if(scanf("%d", &a) == 1)
        {
            while(v2 == 0)
            {
                printf("Enter second number: \n");
                if(scanf("%d", &b) == 1)

                {
                    v2 =1;
                    getchar();
                }
                else
                {
                    getchar();
                    printf("Invalid input!\n");
                }
            }
getchar();          
        }
        else
        {
            getchar();
            printf("Invalid input!\n");
        }
    }



    while( valid == 0)
    {
        printf("Addition -> 1\nSubtraction -> 2\nMultiplication -> 3\nDivision -> 4\nReset -> R\nExit -> E\n");
        scanf("%c", &choice);
        if (choice == 'r' || choice == 'e')
        {
            choice = toupper(choice);
        }
        if ((choice  == '1') ||  (choice  == '2') ||  (choice  == '3') ||  (choice  == '4') ||  (choice  == 'R') ||  (choice  == 'E'))  
        {
            valid = 1;
        }
        else
        {
            printf("Invalid input!\n\n");
        }
    }
    *opt = choice;
    *n1  = a;
    *n2  = b;
}

这里是完整的代码供引用。较早的答案能够解决崩溃问题。现在,要么循环没有退出,要么它不能正常工作。

#include <stdio.h>
#include <ctype.h>

int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);
void input(int *n1, int *n2, char *opt);


int main(void)
{
    int n1, n2, ret;
    char opt;

    start:
    input(&n1, &n2, &opt);

    switch(opt)
{
            case '1': 
                ret = add(n1, n2);
                printf("The sum is %d\n", ret);
                break;
            case '2':
                ret = subtract(n1, n2);
                printf("The difference is %d\n", ret);
                break;
            case '3': 
                ret = multiply(n1, n2);
                printf("The product is %d\n", ret); 
                break;              
            case '4': 
                ret = divide(n1, n2);
                printf("The quotient is %d\n", ret);
                break;
            case 'R':
                goto start;
                break;
            case 'E':
                printf("Goodbye!\n");
                return 0;
                break;
    }
    goto start; 
}


void input(int *n1, int *n2, char *opt)
{
    int valid = 0;
    int v2 = 0;
    char choice;
    int a, b;


    while (v2  == 0)
    {
        printf("Enter first number: \n");
        if(scanf("%d", &a) == 1)
        {
            while(v2 == 0)
            {
                printf("Enter second number: \n");
                if(scanf("%d", &b) == 1)

                {
                    v2 =1;
                    getchar();
                }
                else
                {
                    getchar();
                    printf("Invalid input!\n");
                }
            }
getchar();          
        }
        else
        {
            getchar();
            printf("Invalid input!\n");
        }
    }



    while( valid == 0)
    {
        printf("Addition -> 1\nSubtraction -> 2\nMultiplication -> 3\nDivision -> 4\nReset -> R\nExit -> E\n");
        scanf("%c", &choice);
        if (choice == 'r' || choice == 'e')
        {
            choice = toupper(choice);
        }
        if ((choice  == '1') ||  (choice  == '2') ||  (choice  == '3') ||  (choice  == '4') ||  (choice  == 'R') ||  (choice  == 'E'))  
        {
            valid = 1;
        }
        else
        {
            printf("Invalid input!\n\n");
        }
    }
    *opt = choice;
    *n1  = a;
    *n2  = b;
}


int add(n1, n2)
{
    int result;
    result = (n1+n2);
    return result;
}

int subtract(n1, n2)
{
    int result;
    result = (n1-n2);               
    return result;
}

int divide(n1, n2)
{
    int result;
    result = (n1/n2);
    return result;
}

multiply(n1, n2)
{
    int result;
    result = (n1*n2);
    return result;
}

最佳答案

改变

    if(scanf("%d", a) != 0)

    if(scanf("%d", &a) == 1)
                   //  ^^^^ This is the right check
           //     ^^^ Missing &

scanf 如果未能分配给第一个接收参数,则返回 EOF。在这种情况下,如果数据成功读入 &a,它将返回 1

同理,改

            if(scanf("%d", b) != 0)

            if(scanf("%d", &b) == 1)
                       // ^^^  ^^^^

关于在 while 循环中执行 scanf 后代码崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32794198/

相关文章:

r - 高级 R 函数调用

c++ - 参数中的指针和引用

Javascript 语句的执行顺序

php - 警告 : strpos(): Empty needle in . .....wordpress 插件

c - 将语言编译为 C 是个好主意吗?

javascript - 简化一个工作函数来替换特定的单词?

javascript - 单击按钮后使用参数激活 javascript 函数

c - struct sscanf 无法正常工作?

c++ - 设计一个快速的 "rolling window"文件阅读器

从一个内存复制到另一个内存,跳过 C 中的常量字节