我总是在scanf之后得到一个NULL值 [英] I always get a NULL value after scanf

查看:136
本文介绍了我总是在scanf之后得到一个NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是C中的代码:



This is the code in C :

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main()
{
    char opera;
    int num1, num2, x;
    printf("please enter an operation (+,-,*,/,%) \n");
    scanf_s("%c", &opera);
    if ((opera != '+') || (opera != '-') || (opera != '*') || (opera != '/') || (opera != '%'))
    {
        printf("your input is incorrect!\n");
    }
    else
    {
        printf("enter two numbers!\n");
        scanf_s("%d%d", &num1, &num2);
        if (opera == '+')
            printf("I am clever calculator %d+%d=%d\n", num1, num2, num1 + num2);
        if (opera == '-')
            printf("I am clever calculator %d-%d=%d\n", num1, num2, num1 - num2);
        if (opera == '*')
            printf("I am clever calculator %d*%d=%d\n", num1, num2, num1 * num2);
        if (opera == '/')
            printf("I am clever calculator %d/%d=%d\n", num1, num2, num1 / num2);
        if (opera == '%')
            printf("I am clever calculator %d%%d=%d\n", num1, num2, num1 % num2);
    }
    getchar();
    getchar();
    return 0;


}





无论我输入什么字符我都会得到'\0 '在本地菜单中。



No matter what character i type i always get a '\0' in the locals menu.

推荐答案

1)这

1) This
scanf_s("%d%d", &num1, &num2);



几乎不会起作用,因为它会搜索两个连续的整数,比如32345。它看到一个整数,怎么知道它是32345,32345还是其他?



2)你在编译吗?调试还是发布?在Release中,大多数变量都在寄存器中进行了优化,在CP上甚至有一篇关于Release中调试的好文章。请检查。



3)你没有告诉我们你看到的WHICH变量填充了\0所以我认为问题是在第二个scanf_s中,因为它是一个有明显[可能]缺陷的人。



希望有帮助

Denis



ADDENDUM:

您可能正在使用Unicode字符集或宽字符。在这种情况下,opera的第一个字节将始终为00H,因为ASCII集以Unicode格式映射,第一个字节为0.尝试禁止Unicode添加




will hardly work since it searches for two consecutive integers, like "32345". It sees one integer, how can it know if it was "3" "2345", "32" "345" or other?

2) Are you compiling in Debug or in Release? In Release most of the variables are optimized in registers, there is even a good article here on CP about debugging in Release. Please check.

3) You didn't tell us WHICH variable you see filled with "\0" so I assumed the problem is in the second scanf_s because it is the one with the obvious [possible] flaw.

Hope that helps
Denis

ADDENDUM:
You may be using Unicode charset or wide characters. In that case the first byte of opera will be always 00H because the ASCII set is mapped in Unicode leaving the first byte to 0. Try disallowing Unicode adding

#undef _UNICODE
#undef UNICODE





源文件的开头。



at the beginning of the source file.


我猜你的意思是: -



my guess is you mean here :-

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





所以num2为空?你必须单独读取数字,即: -





so num2 is null ? you have to read the numbers individually ie:-

scanf_s("%d", &num1);




scanf_s("%d", &num2);





iirc第一个%d读取所有输入到num1所以没有留给num2 - 从规范中,即使字符串中num1和num2之间的空格也会被吸收



iirc the 1st %d reads all the input into num1 so there's nothing left for num2 - from the spec even a space inbetween num1 and num2 in the string will be absorbed


这篇关于我总是在scanf之后得到一个NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆