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

Problems finding out total sum using data structures and arrays

I have this code that has several funtions and am almost done, I just am having trouble finding the rental cost on my program.
My program reads a text file of cars and the rental cost as shown here:

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


The float character is the price ( rental cost )

However I want the user to input the car number ( 1-10) choose how many days and output the rental cost. I am just having trouble how it would read the input of the car the user wants. This is my main code, but what I need is to tell if case 3 needs work.

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

Answer

Unless I'm misunderstanding something with how your code is suppose to work, I think case 3 works fine. HOWEVER, it's case 4 that you should be worried about

      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;

You can see my comments there. I would change this to

      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;

Note that carLib increments from 0->9, but your user might think it counts from 1-10. It might help when you print option 2 (all car information) that you include the car ID number that you're expecting from the user.

Also a few more questions for you

  • You're storing the rental cost as an int (not sure if that's by design), so just remember that the days*price computation will get rounded.
  • The rentalCost is incrementing to be bigger and bigger, so I'm guessing the user is renting multiple cars?
  • You should maybe do a check as to whether the user can actually rent a car before increasing their rental cost.
Comments