Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

You are working for a computer hardware store and you want to write a program th

ID: 3567746 • Letter: Y

Question

You are working for a computer hardware store and you want to write a program that handles their price list. You are keeping your data in a dynamically allocated array of structures:

struct Part {

string partNo;

string partName; double price;

};

Part* parts = new Part[n];

Your program starts by printing a menu (a separate function) in the middle of the screen: ]

1. Add a new part

2. Find the price for a particular part

3. Exit If the choice returned to main() is 1),

b) Redo the above question with a vector instead a dynamically allocated array, i.e. vector parts

Explanation / Answer

#include <array>
#include <vector>
#include <iostream>
#include <string>

using namespace std;

struct Part {
   string partNo;
   string partName;
   double price;
};

int n = 10;
Part *parts = new Part[n];
int numused = 0;

void doublesize(){
   n *= 2;
   Part *newarr = new Part[n];
   for (int i = 0; i < numused; ++i){
       newarr[i] = parts[i];
   }
   parts = newarr;
}

void addpart(){
   Part newone;
   cout << "Enter part Number: ";
   cin >> newone.partNo;
   cout << "Enter Part Name: ";
   cin >> newone.partName;
   cout << "Enter Price: ";
   cin >> newone.price;
   if (numused == n){
       doublesize();
   }
   parts[numused++] = newone;
}

double getprice(string p_no){
   for (int i = 0; i < numused; ++i){
       if (p_no == parts[i].partNo){
           return parts[i].price;
       }
   }
   return -1;
}

int main(){
   int option;
   string pnum;
   while (true){
       cout << "Enter 1 to exit" << endl;
       cout << "Enter 2 to add a new part" << endl;
       cout << "Enter 3 to get price for a perticular part" << endl;
       cout << "Choose a option: ";
       cin >> option;
       cout << endl;
       switch (option){
       case 1:
           return 0;
       case 2:
           addpart();
           break;
       case 3:
           cout << "Enter part No to get the price: ";
           cin >> pnum;
           option = getprice(pnum);
           if (option == -1){
               cout << "Invalid part id" << endl;
           }
           else{
               cout << "Price is: " << option << endl;
               break;
           }
       default:
           cout << "Invalid option" << endl;
       }
       cout << endl << endl;
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote