C scanf 无符号长值

C scanf unsigned long values

这段代码是我写的,但是情况2和3好像有问题。正如标题中所述,我认为它与无符号长操作有关,但我无法理解它到底是什么。

*编辑版本(scanf 更改)。

#include <stdio.h>
int main()
{   
    int pin, inp, count=0;
    unsigned dep=100, add, withdraw;
    start:;
    if (count>0)
    {
        printf("\n");
    }
    printf("Please, input your PIN number:");
    scanf("%i", &pin);
    while (5)
    {
        if (pin==2014)
        {
            printf("To view your deposit, press 1.\n");
            printf("To add money to your deposit, press 2.\n");
            printf("To withdraw money from your deposit, press 3.\n");
            printf("To log off, press 4.\n");
            scanf("%i", &inp);
            switch(inp)
            {
                case 1:
                    printf("The remainder of your deposit is %i.\n\n", dep);
                    break;
                case 2:
                    printf("Enter the amount of money you want to add: ");
                    scanf("%u", add);
                    dep+=add;
                    break;
                case 3:
                    printf("Enter the amount of money you want to withdraw. We would like to remind you that it should be multiple of 20.\n");
                    scanf("%u", withdraw);
                    if(((withdraw)%20==0)&&(withdraw<dep))
                    {
                        dep-=withdraw;
                    }
                    else 
                    {
                        printf("We are sorry, but you either chose an invalid withdraw amount or you tried to withdrw more money than you have deposited.\n");
                    }
                    break;
                case 4:
                    printf("Logging off.\n");
                    goto end;
                    break;

            }   
        }
        else
        {
            printf("You entered an invalid PIN.");
            count++;
            goto start;

        }

    }
    end:;
}

您没有正确使用 scanf。 scanf("%lu", add); 对于 "%lu" 它需要一个指向 unsigned long int 的指针,但您传递的不是指针,也不是 unsigned long int。 尝试: scanf("%u", &add); 或更改添加的类型。 我还建议检查 scanf 的返回值。 参见:Value returned by scanf function in c