介绍性C++程序

Introductory C++ program

本文关键字:程序 C++ 介绍性      更新时间:2023-10-16

我不知疲倦地试图做到这一点,但似乎什么都不起作用。我所做的很多事情都会导致一个错误"控制可能到达非无效函数的末尾"。

基本上,我们创建了一个程序来输出天然气使用统计数据。我坚持的是:"天然气价格将在4年内从规定的初始值上涨到规定的最终值,然后在未来4年内保持在较高的值。"

我觉得应该有一个循环或函数,但每次我把NUM_YEARS设为int而不是const时,不管程序告诉我"Control可能会到达非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;
}
}

您需要在gasPrice中返回for循环之外的内容。编译器表示,while条件中的任何一个都有可能不满足,在这种情况下都没有可返回的值。

另一方面,while循环的编写方式没有多大意义。只需让他们发表if声明。

我认为,如果将"return currentPrice;"从这两个while循环中移除,它将解决问题。

函数gasPrice在for循环之外没有返回语句。由于函数的返回类型不是void,因此如果没有返回语句,则函数的行为将是未定义的。