Create a program for a simple shopping application, where a user will be shown a menu from which he/she can select any product to purchase or enter zero (0) to exit the program as shown below:
cpp programming exercise with solution |
As shown above, each product will also show its cost per item in the brackets. That Menu should be continuously shown to the user (so that the user can select multiple products (OR even the same product again) until and unless the user enters a zero (0), like
cpp programming exercise with solution |
When the user selects any product, then the program should ask the user to input the desired quantity of that product to be bought, as shown in the image above.
As discussed earlier, this process should continue and the user should be shown the Menu
again and again so that he/she may select multiple products.
Finally, when the user enters a zero (0) to exit the program, then a detailed description
should be shown that should include
- all the purchased products
- their quantity (for each product)
- the subtotal cost (of each product bought)
- and the total cost (of all the products)
A sample working is shown in the following image
cpp programming exercise with solution |
#include <iostream>
using namespace std;
int main()
{
int choice;
int quantity;
int purchase_products;
int books_purchased = 0;
int t_shirts_purchased = 0;
int shoes_purchased = 0;
int books_price = 500;
int t_shirts_price = 700;
int shoes_price = 1000;
int total_cost;
do
{
cout<<"Press 1. Books (Per item price is 500)\n";
cout<<"Press 2. T-Shirts (Per item price is 700)\n";
cout<<"Press 3. Shoes (Per item price is 1000)\n";
cout<<"Press 0. Exit\n";
cout<<"Press any number to buy any product or press 0 to exit: ";
cin>>choice;
if (choice > 0 && choice < 4)
{
if (choice == 1)
{
cout<<"You have selected Books\n";
cout<<"Please enter quantity: ";
cin>>quantity;
purchase_products += quantity;
books_purchased += quantity;
}
if (choice == 2)
{
cout<<"You have selected T-Shirts\n";
cout<<"Please enter quantity: ";
cin>>quantity;
purchase_products += quantity;
t_shirts_purchased += quantity;
}
if (choice == 3)
{
cout<<"You have selected Shoes\n";
cout<<"Please enter quantity: ";
cin>>quantity;
purchase_products +=quantity;
shoes_purchased += quantity;
}
choice++;
}
else if (choice < 0 || choice > 3)
{
cout<<"Please press a valid button\n";
}
} while (choice > 0);
cout<<"All products purchased: "<<purchase_products<<endl;
cout<<"Books Purchased: "<<books_purchased<<endl;
cout<<"T-Shirts Purchased: "<<t_shirts_purchased<<endl;
cout<<"Shoes Purchased: "<<shoes_purchased<<endl;
cout<<"Cost of Books purchased: "<<500*books_purchased<<endl;
cout<<"Cost of T-Shirts purchased: "<<700*t_shirts_purchased<<endl;
cout<<"Cost of Shoes purchased: "<<1000*shoes_purchased<<endl;
total_cost = (500*books_purchased)+(700*t_shirts_purchased)+(1000*shoes_purchased);
cout<<"Total Cost: "<<total_cost;
return 0;
}