Create a data structure named product with a string element named name, an integ
ID: 3762702 • Letter: C
Question
Create a data structure named product with a string element named name, an integer element named count and a double element named price. Write a program that: Asks the user to enter the name of the product, the number of the product, and the price of the product. Store those values into a vector. Asks the user if they would like to enter again. If so, repeat steps a and b. Display all names of the products and their corresponding count and price. Display the total number of kinds of products, the total number of all products and the total price of all products.Explanation / Answer
It took a long time to solve - below is the code after testing.
#include<iostream>
#include<vector>
#include <algorithm>.
using namespace std;
struct product{
string productname;
int countt;
double price;
};
int main() {
vector<product> bag;
bool bagon =true;
while (bagon)
{
product p;
cout << "Product Name: ";
cin>>p.productname;
cout << " Number of Products: ";
cin>>p.countt;
cout << " Price: ";
cin>>p.price;
bag.push_back(p);
cout << " Would you like to add one more item? "; /* accepts 1 and 0 only*/
cin >> bagon;
}
/*Code for displaying all list*/
cout<<" List of all products selected: ";
int i=0;
for (i=0;i<bag.size();i++)
{
cout << bag[i].productname << " - " << bag[i].countt << " - " <<bag[i].price << " ";
}
/*Code for doing totals*/
vector<string> prodlist;
cout<<" Totals: ";
int qtycount=0, totalprice=0;
for (i=0;i<bag.size();i++)
{
qtycount= qtycount+ bag[i].countt ;
totalprice = totalprice + (bag[i].countt * bag[i].price);
prodlist.push_back(bag[i].productname);
}
prodlist.erase( unique( prodlist.begin(), prodlist.end() ), prodlist.end() );
cout << "Total No. of Product Types: " << prodlist.size();
cout << "Total Quantity = " << qtycount;
cout << "Total Price = " << totalprice;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.