2017-07-02 18 views
-4

我的家庭作業有問題。程序中的所有東西都可以運行,但for循環可以增加升級的價值。爲總價值添加數字的循環

的分配如下:

選擇汽車註銷該用戶的一些引擎升級之後。這些升級可以從0(不升級)到5最大升級。你也需要一個很好的循環。

升級是每水平$ 100加上下升級

實施例的成本:1級升級$ 100 實施例:2級升級$ 100 + $ 200 = $ 300 實施例:第3級升級$ 100 + $ 200 + $ 300 = $ 600 等等等等等等等等等等等等等等等等等等等等等等等等等等...

這是我到目前爲止的代碼:

#include <iostream> 
#include <iomanip> 

int main() { 
int quoteNum = 0; 
int carChoice = 0; 
double carCost = 0; 
double upgradeCost = 0; 
int upgradeChoice = 0; 
int engineLevel = 0; 
double totalCost = 0; 
std::cout << std::fixed << std::showpoint << std::setprecision(2); 
do { 
    std::cout << "Welcome to Bob's used car lot and chop shop!\n"; 
    std::cout << "As you can see we have quite a few to choose from!\n"; 
    std::cout << "Which one would you like?: \n"; 
    std::cout << "[1] 2005 Volkswagen Beetle ($8,000)\n"; 
    std::cout << "[2] 2008 Pontiac G6 ($8,581)\n"; 
    std::cout << "[3] 2004 Chevy S-10 ($10,500)\n"; 
    std::cout << "[4] 2016 Jeep Patriot ($15,209)\n"; 
    std::cout << "[5] 2012 Jeep Wrangler Sport ($24,390)\n"; 
    std::cin >> carChoice; 
} while (carChoice <= 0 || carChoice > 5); 
switch (carChoice) { 
    case 1: 
     carCost = 8000.00; 
     break; 
    case 2: 
     carCost = 8581.00; 
     break; 
    case 3: 
     carCost = 10500.00; 
     break; 
    case 4: 
     carCost = 15209.00; 
     break; 
    case 5: 
     carCost = 24390.00; 
     break; 
} 
    do { 
     std::cout << "Quote:{" << quoteNum++ << "} "; 
     std::cout << " Car($" << carCost << ")"; 
     std::cout << " E(" << engineLevel << ")"; 
     std::cout << " Upgrades($" << upgradeCost << ")" << std::endl; 
     std::cout << "Do you want to upgrade your car?\n"; 
     std::cout << "[-/+1] Downgrade/Upgrade Engine\n"; 
     std::cout << "[ 2] Clear all upgrades\n"; 
     std::cout << "[ 3] Reset car\n"; 
     std::cout << "[ 4] Buy Car!!!\n"; 
     std::cout << "What would you like to do?: \n"; 
     std::cin >> upgradeChoice; 
     if (upgradeChoice == -1) { 
      engineLevel--; 
      upgradeCost--; 
     } 
     if (upgradeChoice == 1) { 
      for (upgradeCost = 0; upgradeCost <= 5; upgradeCost++) { 
       upgradeCost = 100.00; 
      } 
     } 
     if (upgradeChoice == 2) { 
      engineLevel = 0; 
      upgradeCost = 0.00; 
     } 
     if (upgradeChoice == 3) { 
      std::cout << "Welcome to Bob's used car lot and chop shop!\n"; 
      std::cout << "As you can see we have quite a few to choose " 
        "from!\n"; 
      std::cout << "Which one would you like?: \n"; 
      std::cout << "[1] 2005 Volkswagen Beetle ($8,000)\n"; 
      std::cout << "[2] 2008 Pontiac G6 ($8,581)\n"; 
      std::cout << "[3] 2004 Chevy S-10 ($10,500)\n"; 
      std::cout << "[4] 2016 Jeep Patriot ($15,209)\n"; 
      std::cout << "[5] 2012 Jeep Wrangler Sport ($24,390)\n"; 
      std::cin >> carChoice; 
      std::cin.ignore(); 
      switch (carChoice) { 
       case 1: 
        carCost = 8000.00; 
        break; 
       case 2: 
        carCost = 8581.00; 
        break; 
       case 3: 
        carCost = 10500.00; 
        break; 
       case 4: 
        carCost = 15209.00; 
        break; 
       case 5: 
        carCost = 24390.00; 
        break; 
      } 
     } 
     if (upgradeChoice == 4) { 
      totalCost = carCost + upgradeCost; 
      std::cout << "Bill: Car($" << carCost << ")"; 
      std::cout << " Upgrades($" << upgradeCost << ")"; 
      std::cout << " Total ($" << totalCost << ")\n"; 
      std::cout << "Press ENTER to continue"; 
      std::cin.ignore(); 
      std::cin.get(); 
      return 0; 
     } 
    } while (upgradeChoice >= -1 || upgradeChoice <= 4 && upgradeChoice != 0); 
} 

你們有什麼建議嗎?

+0

這是方式太寬泛。你有什麼特別的問題?你需要什麼幫助? – Carcigenicate

+0

@Carcigenicate我需要有關UpgradeChoice和englineLevel循環的幫助。當用戶輸入1到5次時,我需要程序添加100的增量,並在用戶輸入-1時刪除更改。 – Hansel

+0

好的。你試圖完成什麼?什麼地方出了錯?這仍然太寬泛。 – Carcigenicate

回答

1

我使用類來解決這個任務,我建議你開始學習面向對象編程,因爲它是編程的一個非常重要的部分,即使你沒有在學校開始學習。它使你的開發方式更容易看透。

對於這個項目,我使用了C++ 11和GNU C/C++編譯器。

雖然它可能包含錯誤或常規錯誤,但對不起!

包括頭:

#include <iostream> 
#include <string> 
#include <vector> 
#include <sstream> // std::stringstream 

助手:

class Car { 
    USHORT year, engine_level; 
    std::string name; 
    float price, upgrade_cost; 

public: 
    Car(USHORT year, std::string name, float price) { 
     this->year = year; 
     this->name = name; 
     this->price = price; 

     engine_level = 1; 
     upgrade_cost = 0.0; 
    } 

    std::string to_string() { return convert_string(year) + " " + name + " " + " ($" + convert_string(price) + ")"; } 

    friend std::ostream& operator<<(std::ostream& os, Car car) { 
     os << car.to_string() << std::endl; 
     return os; 
    } 

    std::string get_engine_level() { return "E(" + convert_string(engine_level) + ")"; } 
    std::string get_price() { return "Car($" + convert_string(price) + ")"; } 
    std::string get_upgrade_cost() { return "Upgrades($" + convert_string(upgrade_cost) + ")"; } 

    void downgrade() { 
     if(engine_level > 1) { 
      engine_level--; 
      upgrade_cost -= (100 + (engine_level * 100)); 
     } else { 
      std::cout << "You can't downgrade your car from level 1!" << std::endl; 
     } 
    } 

    void upgrade() { 
     engine_level++; 
     upgrade_cost += (100 + (engine_level * 100)); 
    } 

    void clear_upgrades() { engine_level = 1; upgrade_cost = 0.0; } 

    std::string get_total_cost() { return "Total ($" + convert_string(price + upgrade_cost) + ")"; } 
}; 

using USHORT = unsigned short; 

template <typename T> 
std::string convert_string(const T value) { 
    std::stringstream stream; 
    stream << value; 
    return stream.str(); 
} 

// note: use std::to_string instead of convert_string, if your compiler supports it 

爲代表的經銷商的車輛,車輛類

類爲代表的經銷商和車間:

class CarDealerAndWorkshop { 
    std::vector<Car> cars_for_sale; 

public: 
    CarDealerAndWorkshop(std::vector<Car> cars_for_sale) { this->cars_for_sale = cars_for_sale; } 

    void welcome_message() { 
     std::cout << 
      "Welcome to Bob's used car lot and chop shop!\n" 
      "As you can see we have quite a few to choose from!\n" 
      "Which one would you like?: "; 
    } 

    void upgrade_message() { 
     std::cout << 
      "Do you want to upgrade your car?\n" 
      "[-/+1] Downgrade/Upgrade Engine\n" 
      "[ 2] Clear all upgrades\n" 
      "[ 3] Reset car\n" 
      "[ 4] Buy Car!!!\n" 
      "What would you like to do?: "; 
    } 

    void show_cars() { 
     int counter = 1; 
     for (auto car : cars_for_sale) { 
      std::cout << "[" << counter++ << "] "; 
      std::cout << car; 
     } 
    } 

    unsigned number_of_cars() { return cars_for_sale.size(); } 

    bool validate(const USHORT number) { return number <= number_of_cars() && number >= 0; } 

    Car* get_car_by_num(const USHORT num) { return &cars_for_sale[num]; } 

    void buy(const int number) { cars_for_sale.erase(cars_for_sale.begin() + number); } 

    bool are_there_cars() { 
     if(number_of_cars() == 0) { 
      std::cout << "We are very sorry, but we have ran out of cars!\n"; 
      return false; 
     } 
     return true; 
    } 
}; 

類爲代表的客戶:

class Customer { 
    Car* car_choice; 
    std::string name; 

public: 
    Customer(std::string name) { this->name = name; } 
    void set_car_choice(Car* car) { car_choice = car; } 
    Car* get_car_choice() { return car_choice; } 
}; 

最後的客戶端代碼:

int main() { 
    CarDealerAndWorkshop Bobs_Dealership({ 
     {2005, "Volkswagen Beetle", 8000}, 
     {2008, "Pontiac G6", 8581}, 
     {2004, "Chevy S-10", 10500}, 
     {2016, "Jeep Patriot", 15209}, 
     {2012, "Jeep Wrangler Sport", 24390} 
    }); 

    Customer customer("Bill"); 

    Bobs_Dealership.welcome_message(); 

    bool is_quit_dealer = false; 

    while(!is_quit_dealer) { 

     if(!Bobs_Dealership.are_there_cars()) 
      is_quit_dealer = true; 

     USHORT car_choice; 

     do { 
      Bobs_Dealership.show_cars(); 
      std::cin >> car_choice; 
     } while (!Bobs_Dealership.validate(car_choice-1)); 

     customer.set_car_choice(Bobs_Dealership.get_car_by_num(car_choice-1)); 

     unsigned quote_num = 0; 
     bool is_quit_workshop = false; 
     short upgrade_choice; 

     while(!is_quit_workshop) { 

      std::cout << "Quote:{" << quote_num++ << "} "    << std::endl; 
      std::cout << customer.get_car_choice()->get_price()  << std::endl; 
      std::cout << customer.get_car_choice()->get_engine_level() << std::endl; 
      std::cout << customer.get_car_choice()->get_upgrade_cost() << std::endl; 

      Bobs_Dealership.upgrade_message(); 
      std::cin >> upgrade_choice; 

      switch(upgrade_choice) { 
       case -1: customer.get_car_choice()->downgrade();  break; 
       case 1: customer.get_car_choice()->upgrade();  break; 
       case 2: customer.get_car_choice()->clear_upgrades(); break; 
       case 3: is_quit_workshop = true; break; 
       case 4: 
        std::cout << customer.get_car_choice()->get_price() << std::endl; 
        std::cout << customer.get_car_choice()->get_upgrade_cost() << std::endl; 
        std::cout << customer.get_car_choice()->get_total_cost() << std::endl; 
        std::cout << "Press ENTER to continue" << std::endl; 
        std::cin.sync(); 
        std::cin.get(); 
        Bobs_Dealership.buy(car_choice-1); 
        is_quit_workshop = true; 
      } 
     } 
    } 

    return 0; 
} 
+0

好的開始。如果可能的話,我想給你一些提示。正如您提到的C++ 11,您可以使用['using'](http://en.cppreference.com/w/cpp/language/type_alias)而不是'typedef',[基於範圍的循環](http ://en.cppreference.com/w/cpp/language/range-for)和['std :: to_string'](http://en.cppreference.com/w/cpp/string/basic_string/to_string)你的'convert_string'。開始使用[成員初始值設定項列表](http://en.cppreference.com/w/cpp/language/initializer_list),除非您確實需要刷新輸出,否則更喜歡''\ n''到'std :: endl'。 –

+0

謝謝!幫我解決了我的問題:) – Hansel

+0

@Bob__感謝鮑勃的好意見!是的,我想使用'std :: to_string',但我的MinGW不以某種方式支持它,我需要很快更新它。 – Adam