2016-09-23 143 views
0

我有車的一個文本文件的數組:尋找最大的數據結構

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 190.83 1 
2010 Toyota Corolla 50.36 1 

我試圖找到最大也就是浮動,但我無法找到它,以及發現汽車的租賃成本。迄今爲止我有這個,但仍然有麻煩。

#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; 

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

    menu(); 

    carInData.open(filename); 

    cin >> choice; 

    if(carInData.is_open()); 
    { 
     // read list of names into array 
     for(cout; count < carAmount; count++){  

      carInData >> carLib[count].year >> carLib[count].make >> carLib[count].model >> carLib[count].price >> carLib[count].available; 
      switch (choice){ 

       case 1: 
        if(carLib[count].available == 1) 
         cout << " Available "; 
        else 
         cout << " Unavailable "; 


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

       case 2: 
        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; 
        break; 

       case 3: 
        MostExpensiveIndex = count; 
        for(size_t carIndex = 0; carIndex < count; ++carIndex){ 
         if(carLib[carAmount].price <= mostExpensive) continue; 
         mostExpensive = carLib[carIndex].price; 
         MostExpensiveIndex = carIndex; 
        } 
        const car & carI = carLib[MostExpensiveIndex]; 

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

        break; 
      } 
     } 
    } 

    return 0; 
} 


void menu(){ 
    cout << " 1 - Show Cars\n"; 
    cout << " 2 - Rental Cost\n"; 
    cout << " 3 - Most Expensive Car\n"; 
} 

我的輸出顯示的是我所有的汽車而不是最大的汽車。這是輸出截圖

output

+2

什麼是你的問題有? – user3286661

+0

@ user3286661我的輸出顯示了我所有的汽車,而不是最大的汽車。 http://imgur.com/a/RJhZJ –

+0

@ user3286661 我看到我的括號是影響最大值的問題,但現在我的汽車不會顯示,當用戶按1時,它顯示長號碼。 –

回答

0

替換:

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

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

正確的代碼是:

#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; 
    float rentalCost = 0; 

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

    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: 
     for (int carIndex = 0; carIndex < carAmount; ++carIndex){ 
     if (carLib[carIndex].available == 1) 
      cout << " Available "; 
     else 
      cout << " Unavailable "; 


     cout << carLib[carIndex].year << " " << carLib[carIndex].make << " " << carLib[carIndex].model << " " << carLib[carIndex].price << " " << "\n"; 
     } 
     break; 

    case 2: 
     cout << " Enter car number and how many days " << "\n"; 
     cin >> count; 
     cout << " Days: "; 
     cin >> days; 
     cout << "\n" << "Car: "; 

     rentalCost = days * carLib[count].price; 
     cout << " Rental Cost for " << days << " days is " << rentalCost << endl; 
     break; 

    case 3: 


     MostExpensiveIndex = 0; 
     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: " << MostExpensiveIndex << " " << carI.year << " " << carI.make << " " << carI.model << " " << carI.price << "\n"; 

     break; 

    } 
    return 0; 
} 


void menu() 
{ 
    cout << " 1 - Show Cars\n"; 
    cout << " 2 - Rental Cost\n"; 
    cout << " 3 - Most Expensive Car\n"; 
} 
+0

我這樣做了,但由於某種原因,它顯示了列表頂部的汽車以及最大的汽車。 –

+0

@JohhnyB檢查答案中的正確代碼。 – user3286661

+0

我看到我的括號是影響最大值的問題,但現在當用戶按1時我的車不會顯示,它顯示長號碼。 @ user3286661 –