2016-09-23 7 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 –

関連する問題