【问题标题】:Variable is automatically becoming zero after character input输入字符后变量自动变为零
【发布时间】:2024-01-19 11:18:02
【问题描述】:

在这个程序中,num1 值在 if-else 条件内变为零。我得到了错误的答案,因为变量自动变为 0。为什么会这样?如果我将 num1 设为 4,将 num2 设为 3。我得到的附加值是 3,它应该是 7 对。请帮忙解决

#include<stdio.h>
void main()
{
    int num1, num2, answer;
    char type;

    printf("Enter num1\n");
    scanf("%d",&num1);
    printf("Enter num2\n");
    scanf("%d",&num2);
    printf("type a for addition or s for subtraction\n");
    scanf("%s",&type);

    if(type == 'a')
    {
        answer = num1 + num2;
        printf("answer is %d \n",answer);
    }
    else if(type == 's')
    {
        answer = num1 - num2;
        printf("answer is %d \n",answer);
    }
    else
    {
        printf("enter a or s");
    }
}

【问题讨论】:

  • scanf("%s",&amp;type); 调用 未定义的行为 - 您正在尝试将一个以 null 结尾的字节字符串读入单个字符
  • 使用scanf(" %c",&amp;type);%c 之前的空格会跳过可选的空格。
  • addsub 在 if else 语句中未定义。你的代码是如何编译的?

标签: c if-statement char


【解决方案1】:
  • 使用%c 进行字符输入。
  • 在 if-else 语句中使用 answer 变量。
#include<stdio.h>
void main()
{
    int num1, num2, answer;
    char type;

    printf("Enter num1\n");
    scanf("%d",&num1);
    printf("Enter num2\n");
    scanf("%d",&num2);
    printf("type a for addition or s for subtraction\n");
    scanf(" %c",&type);

    if(type == 'a')
    {
        answer = num1 + num2;
        printf("answer is %d \n",answer);
    }
    else if(type == 's')
    {
        answer = num1 - num2;
        printf("answer is %d \n",answer);
    }
    else
    {
        printf("enter a or s");
    }
}

【讨论】:

  • 对不起,我尝试了多种方法,上传时我没有更改(添加和子)。我只运行带有答案变量的代码。你的代码有效。谢谢。你能解释一下scanf中%c之前的空格吗,
  • %c 之前的空格会删除所有空格(空格、制表符或换行符)。以前的输入可以将\n 留在缓冲区中,因此我们需要在输入字符之前将其删除。
  • 好的,谢谢,但为什么将 num1 设为 0。
【解决方案2】:

解决方案 1:当接收来自用户的类型变量输入时,您可以使用 %c 而不是 %s。

解决方案2:您可以使用 %s 来获取类型变量的输入值。但是在 if 和 else if 语句中进行比较时,您可以使用 C String 模块的内置函数 Snippet:=if(type.equals('a'))。在这种情况下,您应该使用 equals() 方法进行比较

#include<stdio.h>
void main()
{
    int num1, num2, answer;
    char type;

    printf("Enter num1\n");
    scanf("%d",&num1);
    printf("Enter num2\n");
    scanf("%d",&num2);
    printf("type a for addition or s for subtraction\n");
    scanf("%c",&type);

    if(type == 'a')
    {
        answer = num1 + num2;
        printf("answer is %d \n",add);
    }
    else if(type == 's')
    {
        answer = num1 - num2;
        printf("answer is %d \n",sub);
    }
    else
    {
        printf("enter a or s");
    }
}

【讨论】:

    【解决方案3】:

    printf("answer is %d \n", add); 中使用的add 变量未在您的程序中定义。 sub 也不是。它应该给出一个编译错误。您是否关闭了编译错误?试试这个代码...

    #include<stdio.h>
    void main()
    {
        int num1, num2, answer;
        char type;
    
        printf("Enter num1\n");
        scanf("%d",&num1);
        printf("Enter num2\n");
        scanf("%d",&num2);
        printf("type a for addition or s for subtraction\n");
        scanf("%c",&type);
    
        if(type == 'a')
        {
            answer = num1 + num2;
            printf("answer is %d \n",answer);
        }
        else if(type == 's')
        {
            answer = num1 - num2;
            printf("answer is %d \n",answer);
        }
        else
        {
            printf("enter a or s");
        }
    }
    

    【讨论】: