Question
Coding in C++ This is the assignment. I am also uploading my written code that I have already. I am struggling with figuring out how
Coding in C++
This is the assignment. I am also uploading my written code that I have already. I am struggling with figuring out how to only show the available cars in option 2 and 5. I am also struggling with how to sort them like in the example with [0]: [1]: etc.
Any help would be appreciated
MY CODE HERE:
#include
#include
using namespace std;
void copy (char a[10] , char b[10])
{
for (int i = 0; i
a[i] = b[i];
}
struct car_types //the structure of the car variables
{
int year; //the year is an int variable
char make[10]; //char character array of 10 for make of car
char model[10]; //char character array of 10 for model of car
float price; //float variable for price because dealing with decimals
bool available; //true or false for availability
};
void sort_cars(struct car_types data[],int n) //function for sorting the cars by the data from the input file using the structure of the car_types
{
struct car_types temp;
for (int i = 0; i { for (int j =i+1; j { if (data[i].price > data[j].price) { temp.year = data[i].year; copy(temp.make,data[i].make); copy(temp.model,data[i].model); temp.price = data[i].price; temp.available = data[i].available; data[i].year = data[j].year; copy(data[i].make,data[j].make); copy(data[i].model,data[j].model); data[i].price = data[j].price; data[i].available = data[j].available; data[j].year = temp.year; copy(data[j].make,temp.make); copy(data[j].model,temp.model); data[j].price = temp.price; data[j].available = temp.available; } } } } void printing_to_terminal(struct car_types data[],int n) //function that prints the options to the terminal { for (int i = 0; i { cout if (data[i].available == 0) //the if/else statements that see if the car is avaiable or not, if it sees that the data has an ending with a "0" it will return false.Else it returns true cout else cout } } void print_output(struct car_types data[],int n, int num) { for (int i = 0; i { if (data[i].available == 1) { cout } } } void input_filename(struct car_types data[]) { char filename[20]; ifstream fin; char avail[7]; cout cin >> filename; fin.open("Cars.txt"); int count = 0; while (fin >> data[count].year >> data[count].make >> data[count].model >> data[count].price >> avail) { if (avail[0] == 'f') data[count].available = false; else data[count].available = true; count++; } } void output_file(struct car_types data[], int n) { char filename[20]; ofstream fout; cout cin >> filename; fout.open(filename); int count = 0; for (int i = 0; i { fout if (data[i].available == false) { fout } else { fout } } } void number_of_days(struct car_types data[],int n) { int num_days; sort_cars(data,5); cout cin >> num_days; print_output(data,n,num_days); } void rent_prices(struct car_types data[], int n) { int index; int num; cout cin >> index >> num; if (data[index-1].available == true) { data[index-1].available = false; cout } else { cout } } int main() { struct car_types data[5]; int choice; do { cout cout cout cout cout cout cout cout cin >> choice; switch (choice) { case 1: input_filename(data); break; case 2: printing_to_terminal(data, 5); break; case 3: output_file(data,5); break; case 4: sort_cars(data,5); break; case 5: number_of_days(data,5); break; case 6: rent_prices(data,5); break; } } while (choice != 7); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started