我已經不知疲倦地嘗試了這個權利,但似乎沒有任何工作。我做的很多事情都會讓我遇到一個錯誤:「控制可能會達到非無效功能的結束」。介紹性的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;
}
}
有一個'while'循環運行一次然後返回沒有多少意義。讓它成爲'如果'。無論如何,如果'YEAR_NUM == 4',你不會返回任何東西。 – chris