2017-02-26 107 views
0

仍在試圖掌握如何組織語法。我有一個練習,我需要編寫一個調用函數的程序,並與用戶進行交互,他們進入一年,人口是輸出。我不確定我是否在正確的軌道上。函數調用麻煩

這是我有:

/*Calculate Gotham's population*/ 

#include <stdio.h> 

int get_population (int , double); 

int main (void){ 
    int t; 
    double population; 

    printf("Enter a year after 1990 > "); 
    scanf("%d", &t); 

    population = int get_population (t); 

    printf("Predicted Gotham City population for 2015 (in thousands):%f"); 

    return 0; 
} 

int get_population (int t, double P){ 
    double P = 52.966 + 2.184*t; 
    return P; 

    printf("Predicted Gotham City population for 2015 (in thousands):%d"); 
} 
+1

人口另一個文本= INT get_population(T); <=這裏不需要「int」,也可以將返回int的函數的結果賦值給double。 –

+0

我很漂亮,這不會編譯。這是行中的'int'是什麼?population = int get_population(t);'?另外函數get_population()中的printf不可訪問。並且你的函數參數像局部變量('P')一樣。 – kaetzacoatl

+0

不是int函數類型 – Sam

回答

2

你不需要在方法get_population兩個參數。你只需要通過year它,它應該返回計算的人口值。

而且調用該方法,並設定它的返回值給一個變量時,也沒有做好。

不需要printfget_population方法,因爲它不會被執行因爲控制將從它之前的方法返回。

更改您的代碼如下。

int get_population (int); 

int main (void){ 
    int year; 
    double population; 

    printf("Enter a year after 1990 > "); 
    scanf("%d", &year); 

    population = get_population (year); 

    printf("Predicted Gotham City population for %d (in thousands):%f", year, population); 

    return 0; 
} 
int get_population (int year){ 
    double population = 52.966 + 2.184* year; 
    return population; 
} 
+0

@gsamaras我用解釋更新了答案。如果我需要添加更多內容,請告訴我。謝謝 –

+0

謝謝,這是有道理的。 – Sam

+1

通常,打印的消息應以換行符結束。提示信息(「Enter ...」)是一個例外;用戶的輸入在那裏提供換行符。 –

1

有些事情已經在另一個答案中解釋過了,這只是一種不同的方法。

註釋原代碼:

/*Calculate Gotham's population*/ 

#include <stdio.h> 

//Your population calculation depends on the year only, so you only need one argument 
int get_population (int , double); 

int main (void){ 
    int t; 
    double population; //Can a not natural number of people exist? An int is better suited for this 

    printf("Enter a year after 1990 > "); 
    scanf("%d", &t); 

    //There is no need to cast the return of the function 
    //Also a cast to another type would be done by enclosing it with brackets 
    //Example: population = (int) get_population(t); 
    population = int get_population (t); 

    printf("Predicted Gotham City population for 2015 (in thousands):%f"); 

    return 0; 
} 

int get_population (int t, double P){ 
    double P = 52.966 + 2.184*t; 
    return P; 
    //The following code will not be executed, since the function has ended 
    printf("Predicted Gotham City population for 2015 (in thousands):%d"); 
} 

以下是我怎麼想「修復」你的代碼:

/*Calculate Gotham's population*/ 

#include <stdio.h> 
//Since the function only depends on the year, only 1 argument is needed 
int get_population (int); 

int main (void){ 
    //Since a not natural number of people can't exist, using ints 
    int year, population; 

    printf("Enter a year after 1990 > "); 
    scanf("%d", &year); 

    //Calling the function that our code knows returns an int and assigning it to a variable 
    population = get_population (year); 

    //To use variables in a printf, you'll use the '%' followed by a description(%d for (decimal) int, %c for char, %s for strings,...) 
    //And add those variables, in the right order, at the end 
    printf("Predicted Gotham City population for %d (in thousands):%d", year, population); 

    return 0; 
} 

//Since you only do the same thing every time, you can just return the calculation 
int get_population (int year){ 
    return (52.966 + 2.184*year); 
} 

如果你理解了變化,也許嘗試加入一些故障安全測試,也許輸出如果輸入年份是1990年<

+0

謝謝我欣賞有用的解釋。 – Sam

+0

通常,打印的消息應以換行符結束。提示信息(「Enter ...」)是一個例外;用戶的輸入在那裏提供換行符。 –