2016-11-25 221 views
-2

我寫了這段代碼,但情況2和3似乎有問題。正如標題中指出的那樣,我認爲它與未簽名的長操作有關,但我不明白它到底是什麼。C scanf無符號長整型值

*編輯的版本(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:; 
} 
+1

'unsigned'使用'%u'和'的scanf( 「%U」,&add);' – Danh

+0

落後的goto是邪惡的使用'爲(;;)'循環和'return'聲明。 – chqrlie

+0

使用'gcc -Wall -W'來啓用有用的警告 – chqrlie

回答

2

您沒有正確使用scanf。 scanf("%lu", add); 對於"%lu"它期望一個指向unsigned long int的指針,但是你傳遞的不是一個指針而不是一個無符號的long int。 嘗試: scanf("%u", &add); 或更改添加的類型。 我也建議檢查scanf的返回值。 見:Value returned by scanf function in c

+0

'%lu'期望指向** unsigned ** long int – Danh

+0

'add'是'unsigned int',使用'%u'而不是'%d' – Danh

+0

'scanf(「%lu」,撤回);' – chqrlie

相關問題