You are working for a computer hardware store and you want to write a program th
ID: 3568049 • Letter: Y
Question
You are working for a computer hardware store and you want to write a program that handles their price list. :
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), you call another function that inputs a new part and appends it to the array. If the choice is 2) you call another function that asks the user for a part number, finds that part in the array, and display the rest of the information. If the part number entered is not in the array, then the program displays an appropriate message. This keeps repeating until the user select choice 3).
struct Part {
string partNo;
string partName;
double price;
};
do the above question with a vector
vector <Part> parts
Explanation / Answer
#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct Part {
string partNo;
string partName;
double price;
};
Part addPart()
{
Part p = Part();
cout<<" Enter part number: ";
cin>>p.partNo;
cout<<" Enter partName: ";
cin>>p.partName;
cout<<" Enter price: ";
cin>>p.price;
return p;
}
double findPrice(string pNo, vector<Part> p)
{
for (vector<Part>::iterator it = p.begin() ; it != p.end(); ++it)
{
if(it->partNo == pNo)
return it->price;
}
return -1;
}
int main()
{
vector<Part> parts;
int choice;
string partNumber;
double price;
while(true)
{
cout<<" 1. Add a new part ";
cout<<"2. Find the price for a particular part ";
cout<<"3. Exit ";
cout<<" Enter your choice: ";
cin>>choice;
switch (choice)
{
case 1:
cout<<" ";
parts.push_back(addPart());
break;
case 2:
cout<<" Enter part number: ";
cin>>partNumber;
cout<<" ";
price = findPrice(partNumber, parts);
if(price < 0)
cout<<" NO ITEMS FOUND !! ";
else
cout<<" Price of part number "<<partNumber<<" is: "<<price<<" ";
break;
case 3:
return 0;
default:
return 0;
}
}
return 0;
}
-------------------------
output
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.