Johhny B Johhny B - 2 months ago 6
C++ Question

Finding the max in an array of Data-Structures

I have a text file of cars:

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


I am trying to find the max which is the float but I am having trouble finding it as well as finding the rental cost of the cars. I have this so far but am still having trouble. Any advice would be really helpful!

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


My output is displaying all my cars instead of the maximum one.

Answer

Replace:

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

with

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

Correct code is:

#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 (; 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 < 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";
}