2014-04-09 95 views
1

我已經不知疲倦地嘗試了這個權利,但似乎沒有任何工作。我做的很多事情都會讓我遇到一個錯誤:「控制可能會達到非無效功能的結束」。介紹性的C++程序

基本上,我們創建了一個輸出天然氣使用統計數據的程序。我所堅持的是: 「天然氣將在4年內從定義的初始價格上漲到確定的最終價格,然後在未來4年內保持固定在更高的價值水平。」

我覺得應該有一個循環或函數,但每次我使NUM_YEARS爲一個int而不是一個const,無論程序告訴我什麼'控制可能會達到非void函數的末尾'。

下面是程序:

#include <cstdlib> 
#include <iostream> 

using namespace std; 

const int MILES_PER_YEAR = 21000; 
const double CITY_PERCENT = 45.0; 
const double HIGHWAY_PERCENT = 55.0; 
const double CITY_MPG = 51.0; 
const double HIGHWAY_MPG = 45.0; 
const double USABLE_GAS = 9.0; 
const double INITIAL_PRICE = 3.359; 
const double FINAL_PRICE = 6.00; 
const int NUM_YEARS = 8; //This will be the total number of years 

double gasPrice(int day); 

int main(int argc, char * argv[]) { 
    cout << "Driving the Toyota Prius" << endl; 
    double daily_miles = MILES_PER_YEAR/365.0; 
    double daily_city_miles = daily_miles * CITY_PERCENT/100.0; 
    double daily_highway_miles = daily_miles*HIGHWAY_PERCENT/100.0; 
    double daily_gas_consumed = daily_highway_miles/HIGHWAY_MPG + 
    daily_city_miles/CITY_MPG; 
    double gas_in_tank = USABLE_GAS; 
    double price; 
    double amount_purchased; 
    double gallons_purchased; 
    double total_gas_purchases = 0; 

    for(int day = 0;day < 365*8; day++) { //If the day is less than the total number of days     in 8 years, add one day 
     cout << "Driving summary for day " << day << endl; 
     cout << " highway miles: " << daily_highway_miles << endl; 
     cout << " city miles : " << daily_city_miles << endl; 
     cout << " gas consumed : " << daily_gas_consumed << endl; 
     gas_in_tank = gas_in_tank - daily_gas_consumed; 
     cout << " gas in tank : " << gas_in_tank << endl; 

     if (gas_in_tank < 0.0) { 
     cout << " BUY GAS" << endl; 
     gallons_purchased = USABLE_GAS - gas_in_tank; 
     price = gasPrice(day); 
     cout << " price today is : " << price << endl; 
     cout << " Gallons purchased: " << gallons_purchased << endl; 
     cout << " fillup cost  : " << gallons_purchased * price << endl; 
     total_gas_purchases = total_gas_purchases + gallons_purchased * price; 
     cout << " total gas cost : " << total_gas_purchases << endl; 
     gas_in_tank = USABLE_GAS; 
     } 
    } 

    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

double gasPrice(int day, int YEAR_NUM) { 
if (int day=365) { //call YEAR_NUM, for day=365, increase YEAR_NUM by 1 
    YEAR_NUM++; 
    day = 0; 
} 
if (YEAR_NUM >= 4) { 
    double currentPrice = FINAL_PRICE; 
    currentPrice; 

} 
if (YEAR_NUM < 4) { //conditional price for the first four years 
    double dailyIncrease = (FINAL_PRICE - INITIAL_PRICE)/(NUM_YEARS * 365); 
    double currentPrice = (INITIAL_PRICE + day * dailyIncrease); 
    return currentPrice; 
} 
} 
+6

有一個'while'循環運行一次然後返回沒有多少意義。讓它成爲'如果'。無論如何,如果'YEAR_NUM == 4',你不會返回任何東西。 – chris

回答

2

您需要返回gasPrice東西是for循環之外。編譯器說有可能不會滿足while條件,在這種情況下沒有任何價值可以返回。

在另一個說明中,while循環沒有很好的寫作方式。只要讓他們if陳述。

+0

你可以檢查我編輯的代碼版本嗎?我用'if'語句替換了'for'循環,並盡我所能讓函數對我有意義。它仍然表明它可能會達到非空函數的結尾,儘管... – Ephexx

+0

'double gasPrice(int day,int YEAR_NUM) { if(int day = 365) { //調用YEAR_NUM,for day = 365,將YEAR_NUM增加1 YEAR_NUM ++; day = 0; (YEAR_NUM> = 4) { } double currentPrice = FINAL_PRICE; currentPrice; (YEAR_NUM <4){//前四年的條件價格 double dailyIncrease =(FINAL_PRICE - INITIAL_PRICE)/(NUM_YEARS * 365);如果(YEAR_NUM <4){//第一個四年的有條件價格 } } } double currentPrice =(INITIAL_PRICE + day * dailyIncrease); return currentPrice; } return 1.00;用合理的默認值//替換 }' 我添加了'返回1.00;'替換成一個合理的默認值 – edtheprogrammerguy

+0

所以,這個工作..和,而不是你的1.00,我說'回YEAR_NUM' 現在,它唯一不做的事情是讓過去四年使用'FINAL_PRICE'作爲靜態分配。我覺得我需要在'day = 365'時添加一個'YEAR_NUM'的聲明,但是我不知道該把代碼放在哪裏 - 我嘗試了多個位置。 – Ephexx

0

函數gasPrice for循環外沒有return語句。由於函數的返回類型不是無效的,因此如果返回語句不存在,函數的行爲將是未定義的。

1

我想如果你移動「return currentPrice;」在這兩個while循環中,它將解決這個問題。