In: Computer Science
C++ Problem
Use the following global structure declaration as an example only. You will need to create your own data structure based on a database of your own design. For example, movies in a video collection, houses for sale, business appointments, etc. Your data structure should include a minimum of four fields. At least one field should be string, and one field should be numeric (int, or double).
Example Only
struct VEHICLE
{
string make;
string model;
int year;
int miles;
double price;
};
In main() create an array that has room that has room for 50 database records(i.e. an array of structures). Your program should support the following general menu choices –
Add Items (i.e. Add Vehicle)
List All Items (i.e. Show All Vehicles)
Q. Quit
Your program should implement a separate function to add items, and display items. For example,
void addVehicle(VEHICLE arr[], VEHICLE addItem, int pos);
adds vehicle item that is a structure to the pos position in the dealer array.
void displayAll(VEHICLE arr[], int n);
displays all vehicle info for the number of vehicles stipulated (i.e. 0 to n-1)
Notes: You are welcome to create one or more additional functions to support your program. For example, showMenu, or getMenuChoice might be useful. Also, you might create a function to collect information, for example, a getVehicleInfo function could populate a structure that was passed ByRef. Please note, in the example above, addVehicle is passed a structure variable in addition to the array. In this approach, addVehicle should take the passed structure variable and add it to the dealer array at the position stipulated. You will need to use an index variable that your program can use to keep track of the next available position in the dealer array. This index variable should be local to main(). So NO global variables please. You will need to update or increment this index variable everytime you call addVehicle. You can use this variable when calling addVehicle, and displayAll.
// C++ program to create and implement a data structure based on a
database of VEHICLES
#include <iostream>
#include <string>
using namespace std;
// define the sructure representing the VEHICLE
struct VEHICLE
{
string make;
string model;
int year;
int miles;
double price;
};
// function declaration
void addVehicle(VEHICLE arr[], VEHICLE addItem, int
&pos);
void displayAll(VEHICLE arr[], int n);
int getMenuChoice();
VEHICLE getVehicleInfo();
int main() {
VEHICLE vehicles [50];
int size = 0;
int choice;
choice = getMenuChoice(); // get the menu option of
the user
// loop continues until the user quits
while(choice != 3)
{
if(choice == 1) // add a
vehicle
{
cout<<"Enter details of the vehicle: "<<endl;
VEHICLE v =
getVehicleInfo();
addVehicle(vehicles, v,size);
}else // display all vehicles
{
displayAll(vehicles,size);
}
choice = getMenuChoice();
}
return 0;
}
// function to add a vehicle in the array
void addVehicle(VEHICLE arr[], VEHICLE addItem, int &pos)
{
if(pos < 50) // if it has room to add
{
arr[pos] = addItem; // add the
vehicle at the end
pos++; // increment the size
}else // array full
cout<<"Array is
full"<<endl;
}
// function to display all the vehicles present in the
database
void displayAll(VEHICLE arr[], int n)
{
for(int i=0;i<n;i++)
{
cout<<"\nVehicle
"<<(i+1)<<":";
cout<<"\nMake:
"<<arr[i].make<<"\nModel:
"<<arr[i].model<<"\nMiles:
"<<arr[i].miles<<"\nPrice:
"<<arr[i].price<<"\nYear:
"<<arr[i].year<<endl;
}
}
// function to get and return the menu option of the user
int getMenuChoice()
{
int choice;
// display the menu choices
cout<<endl<<"Menu"<<endl;
cout<<"1. Add Vehicle"<<endl;
cout<<"2. Show All Vehicles"<<endl;
cout<<"3 Quit"<<endl;
cout<<"Enter your choice(1-3): ";
cin>>choice; // input of menu choice
// validates choice and re-prompts until choice is
valid
while(choice < 1 || choice > 3)
{
cout<<"Invalid
choice"<<endl;
cout<<"Enter your
choice(1-3): ";
cin>>choice;
}
return choice; // return choice
}
// function to create and input the vehicle information and
return the vehicle object created by those information
VEHICLE getVehicleInfo()
{
VEHICLE vehicle;
cout<<"Make: ";
cin.ignore(100,'\n');
getline(cin,vehicle.make);
cout<<"Model: ";
getline(cin,vehicle.model);
cout<<"Miles: ";
cin>>vehicle.miles;
cout<<"Price: ";
cin>>vehicle.price;
cout<<"Year: ";
cin>>vehicle.year;
return vehicle;
}
//end of program
Output: