+1 678 648 4277 
Table Of Contents
  • Interactive Menu-driven Program Using Array of Structs

Interactive Menu-driven Program Using Array of Structs

Write an interactive, menu-driven C++ program that uses an array of structs and separate functions to do the following:
1. Use a “do loop” or a “while loop” to display a menu with the following choices:
1 - Add Parts
2 - Sort Parts
3 - Display All Parts
4 - Display Parts to Reorder
5 - Display a Specific Part
6 - Exit
           Exit the loop when menu option 6 is selected. Use a default
          menu option to indicate an invalid menu option was entered.
2. Create and call the following functions from the menu:
• addParts – Allows the user to enter and validate:
a. A numeric Part ID – between 1 and 1000
b. A part description (use array of characters)
c. A numeric wholesale price – between $1.00 and $1,000.00
d. A numeric retail price – between 10% and 200% greater than the wholesale price
e. A numeric quantity-on-hand – between 0 and 100
f. A numeric reorder point – between 1 and 50
• sortParts – Sort all part records in ascending or descending order by partID. Use a bubble sort.
• displayParts - Display all parts information for each part with one record on each line, include a screen title and column labels, and align all column content with the column labels. Include a total column and line to display the total retail value of all parts in the inventory.
• reorderParts - Display all parts information for parts with quantity on hand less than or equal to the reorder point. Display all parts information for each part with one record on each line, include a screen title and column labels, and align all column content with the column labels
• displaySpecificPart - Display all parts information for the specific part selected by the user.
Use an appropriate title for all screens and appropriate column headings. Display output horizontally for menu options 3, 4, and 5.
Code Solution
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Define a struct for Parts
struct Part
{
 int id; // Part ID
 string description;
 double price;
 int retail_price;
 int quantity;
 int reorder_point;
};
// Identifiers
void displayMenu();
void sortParts(Part *arr, int n_parts, bool ascending);
void swap(Part *x, Part *y);
void displayParts(Part parts[], int n_parts);
void reorderParts(Part parts[], int n_parts);
void displaySpecificPart(Part parts[], int n_parts, int id);
int main()
{
 // Create array of Parts
 Part parts[100]; // maximum 100 parts
 int n_parts = 0;
    // Begin program
 int menu_option;
 int id, retail_price, quantity, reorder_point, sort_type;
 string description;
 double price;
 Part part;
 while (true)
 {
  // Display menu
  displayMenu();
  // Ask for option
  while (true)
  {
   cout << "Please enter your choice: ";
   cin >> menu_option;
   if (menu_option >= 1 && menu_option <= 6)
    break;
   else
    cout << "Please enter a valid option." << endl;
  }
  if (menu_option == 1) // add part
  {
   // Ask for id
   while (true)
   {
    cout << "Enter ID (1-1000): ";
    cin >> id;
    if (id >= 1 && id <= 1000)
     break;
    else
     cout << "Please enter a valid value between 1 and 1000" << endl;
   }
   cin.ignore();
   // Description
   cout << "Enter Description: ";
   getline(cin, description);
   // Wholesale price
   while (true)
   {
    cout << "Enter Wholesale Price ($1.00-$1,000.00): ";
    cin >> price;
    if (price >= 1.0 && price <= 1000.0)
     break;
    else
     cout << "Please enter a valid value between $1.00 and $1000.00" << endl;
   }
   // Retail price
   while (true)
   {
    cout << "Enter Retail Price (10% to 200% greater than Wholesale Price): ";
    cin >> retail_price;
    if (retail_price >= 1.1*price && retail_price <= 2.0*price)
     break;
    else
     cout << "Please enter a valid value between " << 1.0*price << " and " << 2.0*price << endl;
   }
   // Quantity
   while (true)
   {
    cout << "Enter quantity (0-100): ";
    cin >> quantity;
    if (quantity >= 0 && quantity <= 100)
     break;
    else
     cout << "Please enter a valid value between 0 and 100" << endl;
   }
   // Reorder point
   while (true)
   {
    cout << "Enter Reorder Point (1-50): ";
    cin >> reorder_point;
    if (reorder_point >= 1 && reorder_point <= 50)
     break;
    else
     cout << "Please enter a valid value between 1 and 50" << endl;
   }
   // Build part
   part.id = id;
   part.description = description;
   part.price = price;
   part.retail_price = retail_price;
   part.quantity = quantity;
   part.reorder_point = reorder_point;
   // Add to array
   parts[n_parts] = part;
   n_parts++;
   cout << "Part with ID " << id << " successfully added!" << endl;
  }
  else if (menu_option == 2) // sort parts
  {
   // Ask for sort type
   while (true)
   {
    cout << "Please specify if you want to sort in ascending or descending order (1 for Ascending, 2 for Descending): ";
    cin >> sort_type;
    if (sort_type >= 1 && sort_type <= 2)
     break;
    else
     cout << "Please enter a valid value between 1 and 2" << endl;
   }
   if (sort_type == 1)
   {
    // Sort ascending
    sortParts(parts, n_parts, true);
   }
   else
    sortParts(parts, n_parts, false);
   cout << "Array of Parts sorted!" << endl;
  }
  else if (menu_option == 3) // display all parts
  {
   displayParts(parts, n_parts);
  }
  else if (menu_option == 4) // reorder Parts
  {
   reorderParts(parts, n_parts);
  }
  else if (menu_option == 5) // display specific part
  {
   // ask for part id
   while (true)
   {
    cout << "Please enter the Part ID: ";
    cin >> id;
    if (id >= 1 && id <= 1000)
     break;
    else
     cout << "Please enter a valid value between 1 and 1000" << endl;
   }
   displaySpecificPart(parts, n_parts, id);
  }
  else if (menu_option == 6)
  {
   cout << "Good Bye!" << endl;
   break;
  }
  cout << endl;
 }
}
// Function to display menu
void displayMenu()
{
 cout << "1 - Add Parts" << endl;
 cout << "2 - Sort Parts" << endl;
 cout << "3 - Display All Parts" << endl;
 cout << "4 - Display Parts to Reorder" << endl;
 cout << "5 - Display a Specific Part" << endl;
 cout << "6 - Exit" << endl;
}
void sortParts(Part *arr, int n_parts, bool ascending)
{
 // Given an array of parts, sort in ascending mode
 if (n_parts == 1)
  return;
 // sort
 for (int i = 0; i < n_parts; i++)
 {
  for (int j = 0; j < n_parts; j++)
  {
   if (ascending)
   {
    if (arr[j].id > arr[j + 1].id)
     swap(&arr[j], &arr[i]);
   }
   else
   {
    if (arr[j].id < arr[j + 1].id)
     swap(&arr[j], &arr[i]);
   }
  }
 }
}
void swap(Part *x, Part *y)
{
 Part temp = *x;
 *x = *y;
 *y = temp;
}
void displayParts(Part parts[], int n_parts)
{
 // Print header
 cout << "|" << setw(2) << "ID" << setw(2) << "|" << setw(10) << "Description" << setw(2) << "|" << setw(10) << "Wholesale Price" << setw(2) << "|" << setw(2) << "Retail Price" << setw(2) << "|" << setw(2) << "Quantity" << setw(2) << "|" << setw(2) << "Reorder Point" << setw(2) << "|" << std::endl;
 cout << "|" << setw(2) << "--" << setw(2) << "|" << setw(10) << "-----------" << setw(2) << "|" << setw(10) << "---------------" << setw(2) << "|" << setw(2) << "------------" << setw(2) << "|" << setw(2) << "--------" << setw(2) << "|" << setw(2) << "-------------" << setw(2) << "|" << std::endl;
 for (int i = 0; i < n_parts; i++)
 {
  cout << "|" << setw(2) << parts[i].id << setw(2) << "|" << setw(10) << parts[i].description << setw(3) << "|" << setw(10) << parts[i].price << setw(7) << "|" << setw(10) << parts[i].retail_price << setw(4) << "|" << setw(5) << parts[i].quantity << setw(5) << "|" << setw(10) << parts[i].reorder_point << setw(5) << "|" << std::endl;
 }
}
void reorderParts(Part parts[], int n_parts)
{
 // Display only parts with quantity on hand less than or equal to the reorder point
 if (n_parts > 0)
 {
  // Print header
  cout << "|" << setw(2) << "ID" << setw(2) << "|" << setw(10) << "Description" << setw(2) << "|" << setw(10) << "Wholesale Price" << setw(2) << "|" << setw(2) << "Retail Price" << setw(2) << "|" << setw(2) << "Quantity" << setw(2) << "|" << setw(2) << "Reorder Point" << setw(2) << "|" << std::endl;
  cout << "|" << setw(2) << "--" << setw(2) << "|" << setw(10) << "-----------" << setw(2) << "|" << setw(10) << "---------------" << setw(2) << "|" << setw(2) << "------------" << setw(2) << "|" << setw(2) << "--------" << setw(2) << "|" << setw(2) << "-------------" << setw(2) << "|" << std::endl;
  for (int i = 0; i < n_parts; i++)
  {
   if (parts[i].quantity <= parts[i].reorder_point)
    cout << "|" << setw(2) << parts[i].id << setw(2) << "|" << setw(10) << parts[i].description << setw(3) << "|" << setw(10) << parts[i].price << setw(7) << "|" << setw(10) << parts[i].retail_price << setw(4) << "|" << setw(5) << parts[i].quantity << setw(5) << "|" << setw(10) << parts[i].reorder_point << setw(5) << "|" << std::endl;
  }
 }
 else
  cout << "There are not parts to be displayed." << endl;
}
void displaySpecificPart(Part parts[], int n_parts, int id)
{
 // display only part specified by id
 // Find part
 Part part;
 bool found = false;
 for (int i = 0; i < n_parts; i++)
 {
  if (parts[i].id == id)
  {
   found = true;
   part = parts[i];
   break;
  }
 }
 if (found)
 {
  cout << "|" << setw(2) << "ID" << setw(2) << "|" << setw(10) << "Description" << setw(2) << "|" << setw(10) << "Wholesale Price" << setw(2) << "|" << setw(2) << "Retail Price" << setw(2) << "|" << setw(2) << "Quantity" << setw(2) << "|" << setw(2) << "Reorder Point" << setw(2) << "|" << std::endl;
  cout << "|" << setw(2) << "--" << setw(2) << "|" << setw(10) << "-----------" << setw(2) << "|" << setw(10) << "---------------" << setw(2) << "|" << setw(2) << "------------" << setw(2) << "|" << setw(2) << "--------" << setw(2) << "|" << setw(2) << "-------------" << setw(2) << "|" << std::endl;
  cout << "|" << setw(2) << part.id << setw(2) << "|" << setw(10) << part.description << setw(3) << "|" << setw(10) << part.price << setw(7) << "|" << setw(10) << part.retail_price << setw(4) << "|" << setw(5) << part.quantity << setw(5) << "|" << setw(10) << part.reorder_point << setw(5) << "|" << std::endl;
 }
 else
 {
  cout << "A Part with that ID = " << id << " does not exits." << endl;
 }
}