2016-09-24 42 views
-1

我有這個代碼有幾個功能,幾乎完成,我只是無法找到我的程序的租金成本。 我的程序讀取汽車的一個文本文件,租金成本如下所示:使用數據結構和數組發現總額的問題

2014 Toyota Tacoma 115.12 1 
2012 Honda CRV 85.10 0 
2015 Ford Fusion 90.89 0 
2013 GMC Yukon 110.43 0 
2009 Dodge Neon 45.25 1 
2011 Toyota Rav4 65.02 1 
2012 Mazda CX5 86.75 1 
2016 Subaru Outback 71.27 0 
2015 Ford F150 200.83 1 
2010 Toyota Corolla 50.36 1 

浮動字符是價格(租金)

不過,我希望用戶輸入車號(1 -10)選擇多少天並輸出租賃成本。我只是遇到了麻煩,它將如何讀取用戶想要的汽車輸入。這是我的主要代碼,但我需要告訴case 3是否需要工作。

#include <iostream> 
#include <fstream> 

using namespace std; 

struct car { 
    int year; 
    char make[10]; 
    char model[10]; 
    float price; 
    int available; 

} ; 

void menu(); 

// Main Function 
int main() 
{ 
// declare variables 
int carAmount = 10; 
int choice; 
car carLib[carAmount]; 
char filename[10]; 
ifstream carInData; 
float mostExpensive = 0; 
int MostExpensiveIndex; 
int count = 0; 
int days; 
int rentalCost = 0; 
bool menu1 = false; 

    //prompt user for input file 
    cout << " Enter file name: "; 
    cin >> filename; 

    // Start loop menu 
    while(menu1 = true){ 

    menu(); 

    carInData.open(filename); 

    cin >> choice; 

    if (carInData.is_open()) { 
    // read list of names into array 


     for (; count < carAmount; count++) { 

     carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available; 

     } 
    } 

    switch (choice) { 

    // Case 1 closes menu 
    case 1: 

    return 0; 
    break; 

    // Case 2 displays if car is available if 1, unavailable if 0 
    case 2: 

    // itterate through car array 
    for(count = 0; count < carAmount; count++){ 

    // Displays if car is available or not 
    if (carLib[count].available == 1) 
     cout << " Available "; 
    else 
     cout << " Unavailable "; 

    // Display Cars 
    cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << " " << "\n"; 
    } 
    break; 

    // Display only available cars 
    case 3: 

    // itterate through car array 
    for(count = 0; count < carAmount; count++){ 

    // Displays only available cars 
    if (carLib[count].available == 1){ 
     cout << " Available "; 

    // Display Cars 
    cout << carLib[count].year << " " << carLib[count].make << " " << carLib[count].model << " " << carLib[count].price << " " << "\n"; 
     } 
    } 
    break; 


    // Calculates rental cost 
    case 4: 
    cout << " Enter car number and how many days " << "\n"; 
    cout << " Days: "; 
    cin >> days; 
    cout << "\n" << "Car: "; 
    cin >> carLib[count].price; 
    rentalCost += days*count; 

    cout << " Rental Cost for " << days << " days is " << rentalCost << "\n"; 
    break; 

    // Finds most expensive car 
    case 5: 

    MostExpensiveIndex = count; 
    for (size_t carIndex = 0; carIndex < carAmount; ++carIndex) { 

     if (carLib[carIndex].price <= mostExpensive) continue; 
     mostExpensive = carLib[carIndex].price; 
     MostExpensiveIndex = carIndex; 

    } 

    const car & carI = carLib[MostExpensiveIndex]; 

    cout << " Most Expensive car is: " << " " << carI.year << " " << carI.make << " " << carI.model << " " << carI.price << "\n"; 

    break; 



     } 

    } 

    return 0; 
} 


void menu() 
{ 
    cout << " 1 - Exit program.\n"; 
    cout << " 2 - Show Cars\n"; 
    cout << " 3 - Show only available cars.\n"; 
    cout << " 4 - Rental Cost\n"; 
    cout << " 5 - Most Expensive Car\n"; 
} 
+0

也許如果你修正了隨機的,隨意的代碼縮進,一旦它的佈局合理,可讀,邏輯縮進,一個簡單的解決方案將變得明顯。 –

+0

@SamVarshavchik好的! –

回答

0

除非我誤解了代碼的工作原理,否則我認爲案例3正常工作。不過,情況4你應該擔心

 case 4: 
      cout << " Enter car number and how many days " << "\n"; 
      cout << " Days: "; 
      cin >> days; 
      cout << "\n" << "Car: "; 
      cin >> carLib[count].price; // WHY IS THE USER CHANGING THE PRICE? 
      rentalCost += days*count; // WHY IS THE PRICE "DAYS * (CAR ID #)" 

      cout << " Rental Cost for " << days << " days is " << rentalCost << "\n"; 
      break; 

你可以在那裏看到我的意見。我會改變這

 case 4: 
      cout << " Enter car number and how many days " << "\n"; 
      cout << " Days: "; 
      cin >> days; 
      cout << "\n" << "Car: "; 
      cin >> count ; 
      // Note the decrement of 'count' by one, since you expect the user 
      // to enter a number 1-10 
      // Should probably include a check that the 'count' is valid 
      rentalCost += days*carLib[count-1].price; 

      cout << " Rental Cost for " << days << " days is " << rentalCost << "\n"; 
      break; 

注意,從0-> 9,但您的用戶carLib增量可能會認爲這是1-10計數。當您打印選項2(所有汽車信息)時,它可能會有幫助,其中包含您期望從用戶那裏獲得的汽車ID號。你

  • 要存儲的租金成本

    另外幾個問題的int(不知道這是由設計),所以只記得days*price計算將得到圓角。

  • rentalcost越來越大,所以我猜測用戶租用多輛車?
  • 您應該檢查用戶在增加租賃費用之前是否可以租車。
+0

謝謝!是的,我只是改變了我的租金成本,以獲得確切的金額。我只是將每輛車註冊到一定數量,以便用戶知道。並且用戶想要檢查單獨租用每輛車。我注意到,如果用戶試圖檢查其他車型的價格將會增加前一輛車的價格,而不是我想要的。 –