C scanf无符号长值
我写了这段代码,但是案例2和案例3似乎有问题。正如标题中提到的,我认为它与无符号长操作有关,但我不明白它到底是什么 *编辑版本(scanf更改)C scanf无符号长值,c,long-integer,unsigned,operation,C,Long Integer,Unsigned,Operation,我写了这段代码,但是案例2和案例3似乎有问题。正如标题中提到的,我认为它与无符号长操作有关,但我不明白它到底是什么 *编辑版本(scanf更改) #包括 int main() { int引脚,inp,计数=0; 无符号dep=100,增加,收回; 开始:; 如果(计数>0) { printf(“\n”); } printf(“请输入您的PIN码:”); scanf(“%i”、&pin); 而(五) { 如果(pin==2014) { printf(“要查看您的存款,请按1。\n”); pr
#包括
int main()
{
int引脚,inp,计数=0;
无符号dep=100,增加,收回;
开始:;
如果(计数>0)
{
printf(“\n”);
}
printf(“请输入您的PIN码:”);
scanf(“%i”、&pin);
而(五)
{
如果(pin==2014)
{
printf(“要查看您的存款,请按1。\n”);
printf(“要在存款中加钱,请按2。\n”);
printf(“要从存款中取款,请按3。\n”);
printf(“要注销,请按4。\n”);
scanf(“%i”和&inp);
开关(inp)
{
案例1:
printf(“您剩余的存款是%i.\n\n”,dep);
打破
案例2:
printf(“输入要添加的金额:”);
scanf(“%u”,添加);
dep+=添加;
打破
案例3:
printf(“输入您要提取的金额。我们想提醒您,它应该是20的倍数。\n”);
scanf(“%u”,撤回);
如果(((撤回)%20==0)和((撤回您没有正确使用scanf)。
scanf(“%lu”,添加);
对于“%lu”
它需要指向无符号长整型的指针,但您传递的不是指针,也不是无符号长整型。
尝试:
scanf(“%u”,&add);
或更改add的类型。
我还建议检查scanf的返回值。
请参阅:未签名的
使用%u
,和scanf(“%u”,&add);
反向转机是邪恶的。使用for(;)
循环和返回
语句。使用gcc-Wall-W
启用有用的警告。while(5)在哪里来自while语句中的?5,它只是一个随机的真值。add
是一个无符号int
,使用%u
代替%d
对于scanf(“%lu”,draw”)同样的问题;
进行了您提到的更改,但这个程序仍然不起作用
#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:;
}