Question

In: Computer Science

C++ Problem Use the following global structure declaration as an example only. You will need to...

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 –

  1. Add Items (i.e. Add Vehicle)

  2. 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.

Solutions

Expert Solution


// 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:


Related Solutions

In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: For this exercise you should be able to write a logical expression (i.e., with logical operators) which checks if some integer x consists of exactly 5 digits. Ex: 30498 and -14004 are 5-digit numbers, while 1098, -1 and 34 are not. Complete the intQ2(intQ2_input) function that takes an input integer...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: A positive integer number is said to be a perfect number if its positive factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number because 6=1+2+3. Complete the int Q6(intQ6_input, int perfect[])function that determines all perfect numbers smaller than or equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: (Pythagorean Triples) A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes only a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: Complete the int Q7a(intQ7_input) function takes a seven-digit positive integer as input and returns it reversed. For example, if the integer is 9806593, the program should print 3956089. You are not permitted to use any function of C standard library other than scanf()and printf().You are not permitted to use arrays...
In this example you are allowed to use from the C standard library only functions for...
In this example you are allowed to use from the C standard library only functions for input and output (e.g. printf(), scanf()) Complete the following functions using C programming language: intQ1_for() intQ1_while() intQ1_do() To compute the sum of all numbers that are multiples of 4, between 30 and 1000, in three different ways: with a for loop, a while loop and a do-while loop, accordingly. After each loop print the value on the screen. Return the total sum at the...
You are required to use only C for this problem. To begin, instead of using two...
You are required to use only C for this problem. To begin, instead of using two different variables for this problem, you are going to define a structure with two members; one representing the feet and the other one representing the inches. We will also use three functions; one to initialize a structure, one to check the validity of its values, and the last one to print them out. First, define your structure. Next, declare a structure of this type...
Python we need to create a game using only the library graphic.py and use only Structure...
Python we need to create a game using only the library graphic.py and use only Structure Functions only.
Give an example of a problem when you may need to use the central limit theorem...
Give an example of a problem when you may need to use the central limit theorem to compute thr cjances of an event and why
Use the following class for the following problem. The only purpose of the class is to...
Use the following class for the following problem. The only purpose of the class is to display a message both when the constructor is invoked and when the destructor is executed. class Trace { public: Trace(string n); ~Trace(); private: string name; }; Trace::Trace(string n) : name(n) { cout << "Entering " << name << "\n"; } Trace::~Trace() { cout << "Exiting " << name << "\n"; } Requirement: Extend the class Trace with a copy constructor and an assignment operator,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT