2016-01-31 66 views
-1

Im相當新的C,但我需要幫助這個爲什麼我的代碼不能運行?

#define PISS 73 
#include <stdio.h> 
int a, b, c; 
int killmeplease(int a, int b, int c); 
int main(void) 
{ 
    puts("WHATS YOUR AGE"); 
    //Gets int 'a' 
    scanf_s("%d", &a); 
    int killmeplease; 
     printf("Youre gonna die in %d years", b); 
     getchar(); 
     return 0; 


} 
int killmeplease(int a, int b, int c) 
{ 
    PISS - a = b; 
    return 0; 
} 

不要論斷。 不知道這是否是我錯過的東西,但不管它是不是讓代碼運行。 我可能把這個放在錯誤的選項卡中,但是如果你能幫助那很好。

+1

預處理之後,這個'PISS - A = B; '將是'73 - a = b;'這是無效的。它應該做什麼? –

+1

您已經被告知[最小完整示例](http://stackoverflow.com/help/mcve)。你認爲'73 - 1 = b'這行應該怎麼辦? – Beta

+0

@ 13x本來應該把用戶的年齡和從73中減去,看看他們有多少年的生活 –

回答

2

雖然73聽起來有點早不行了,這是你如何實現它:)

#define PISS 73 
#include <stdio.h> 
int killmeplease(int a); 

int main(void) 
{ 
    int a,b; 
    puts("WHATS YOUR AGE"); 
    scanf_s("%d", &a); 
    b=killmeplease(a); 
    printf("Youre gonna die in %d years", b); 
    getchar(); 
    return 0; 
} 
int killmeplease(int a) 
{ 
    return PISS - a; 
} 

該函數返回根據輸入a值。您也可以傳遞另一個指針並存儲它的返回值。

您還應該檢查返回值scanf_s()是否失敗。

+0

謝謝,它的工作。但我確實記得以不同的方式去做,但這是有效的。 –

+1

有多種方式可以做到這一點。但這可能是最簡單的方法。你也可以這樣做:'killmeplease(a,&b);',在函數中:'* b = PISS -a;',並且改變函數的原型並且適當的調用。 –

1

或者我們可以寫明智的名稱函數和變量,然後失誤可能會更明顯:

void yearsOfLifeLeft (int lifeExpectancy, int currentAge, int * yearsLeftPtr) { 
    *yearsLeftPtr = lifeExpectancy - currentAge; 
} 
... 

yearsOfLifeLeft(PISS, a, &b);  /* 'a' and 'b' are names which are not good */ 
            /* and PISS is just childish */ 

只是一個想法...

相關問題