Question

1. in this programe i want add some products to be shown after choosing choise (...

1. in this programe i want add some products to be shown after choosing choise ( show all products ) before i add any products. let say 10 products have added to the program then when the user choose (show all products ) all the 10 products appear and the user going to choose one and that one must has its own name, price,and quantity after that the quantity will be reduce.

2. allow the user to buy products.

===========================================================

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

struct product

{

string name;

int quantity;

float price;

};

void show_products(product products[])

{

if (products[0].name.empty())

{

cout << "\nYou have not added any product yet." << endl;

return;

}

cout << "\n\nAll Products";

cout << "\n--------------------------------------------------------------------------------\n";

cout << left << setw(18) << "Product Number"

<< left << setw(30) << "Name"

<< left << setw(15) << "Quantity"

<< left << setw(15) << "Unit Price";

cout << "\n--------------------------------------------------------------------------------\n";

for (int i = 0; i < 30; i++)

{

if (products[i].name.empty())

break;

cout << left << setw(18) << i + 1

<< left << setw(30) << products[i].name

<< left << setw(15) << products[i].quantity

<< left << setw(15) << products[i].price << "\n";

}

cout << "--------------------------------------------------------------------------------\n";

}

int main()

{

int x, choice, i = 0;

product products[30];

do

{

cout << "\n1. Add products" << endl

<< "2. Show all products" << endl

<< "3. Update a product" << endl

<< "4. Delete a product" << endl

<< "5. Exit" << endl

<< "Enter your choice: ";

cin >> choice;

switch (choice)

{

case 1:

cout << "Enter product name: ";

cin >> ws; // Clean whitespaces

getline(cin, products[i].name);

cout << "Enter product quantity: ";

cin >> products[i].quantity;

cout << "Enter unit price: ";

cin >> products[i].price;

i++;

break;

case 2:

show_products(products);

break;

case 3:

cout << "Enter product number: ";

cin >> x;

cout << "Enter product name: ";

cin >> ws; // Clean whitespaces

getline(cin, products[x - 1].name);

cout << "Enter product quantity: ";

cin >> products[x - 1].quantity;

cout << "Enter unit price: ";

cin >> products[x - 1].price;

break;

case 4:

cout << "Enter product number: ";

cin >> x;

for (int y = x; y < 30; y++)

{

cout << "Product Name: " << products[y - 1].name << endl;

if (products[y].name.empty())

{

products[y - 1].name = "";

break;

}

products[y - 1] = products[y];

}

break;

case 5:

break;

default:

cout << "-----------------------------------------"<<endl;

cout << "You have not chosen any choice yet"<< endl;

cout << "Enter any key to return to the main menu "<<endl;

cin >> ws;

break;

}

} while (choice != 5);

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
/*  C++ Language Used  */
/* Product Management Program */
/* Update Functionality Added */
//File Inclusion
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

//struture to to store product details
struct product
{

   string name;
   
   int quantity;
   
   float price;

};

//Function to show prduct details
void show_products(product products[])
{

   //Checking is product is not empty
   if (products[0].name.empty())
   {
      cout << "\nYou have not added any product yet." << endl;
      return;
   }

   //Displaying Formated Messages
   cout << "\n\nAll Products";
   cout << "\n--------------------------------------------------------------------------------\n";
   cout << left << setw(18) << "Product Number"
   << left << setw(30) << "Name"
   << left << setw(15) << "Quantity"
   << left << setw(15) << "Unit Price";
   cout << "\n--------------------------------------------------------------------------------\n";
   
   //Displaying Product Description
   for (int i = 0; i < 30; i++)
   {
      if (products[i].name.empty())
         break;
         cout << left << setw(18) << i + 1
         << left << setw(30) << products[i].name
         << left << setw(15) << products[i].quantity
         << left << setw(15) << products[i].price << "\n";
   }
   cout << "--------------------------------------------------------------------------------\n";
}

//Driver Function
int main()
{
   //Data Variable
   int x, choice, i = 0;
   //Initialising First few object as hardcode    
   product products[30] = {
                     {"Laptop", 10, 2200},
                     {"HandPhone", 10, 1200},
                     {"Desktop", 10, 4000},
                     {"StudyTable", 10, 250},
                     {"SmartTv", 10, 800}
                     };
   
   /* Do-While Loop
      Iterate until user press 6
   */
   do
   {
      //Displaying Menu
      cout << "\n1. Add products" << endl    
      << "2. Show all products" << endl
      << "3. Update a product" << endl
      << "4. Delete a product" << endl
      << "5. Purchase a product" << endl
      << "6. Exit" << endl
      << "Enter your choice: ";

      //Taking choice       
      cin >> choice;
   
      //Switching for choice
      switch (choice)
      {
         case 1:       //Case for adding products
                  cout<<"\n";
                  cout<<"Current Stock";
                  show_products(products);
                  
                  cout << "\nEnter product name: ";
                  cin >> ws; // Clean whitespaces
                  getline(cin, products[i].name);
                  cout << "Enter product quantity: ";
                  cin >> products[i].quantity;
                  cout << "Enter unit price: ";
                  cin >> products[i].price;
                  i++;
                  break;

         case 2:       //Case for displaying products details
                  show_products(products);
                  break;
         
         case 3:       //Case for update a product
                  cout << "Enter product number: ";
                  cin >> x;
                  cout << "Enter product name: ";
                  cin >> ws; // Clean whitespaces
                  getline(cin, products[x - 1].name);
                  cout << "Enter product quantity: ";
                  cin >> products[x - 1].quantity;
                  cout << "Enter unit price: ";
                  cin >> products[x - 1].price;
                  break;
         
         case 4:       //Case for delete a product
                  cout << "Enter product number: ";
                  cin >> x;
                  for (int y = x; y < 30; y++)
                  {
                     cout << "Product Name: " << products[y - 1].name << endl;
                  
                     if (products[y].name.empty())
                     {
                        products[y - 1].name = "";
                        break;
                     }
                     products[y - 1] = products[y];
                  }
                  
                  break;
                  
         case 5:       //Case for purchase a product
                  cout<<"\n";
                  cout << "Enter product number: ";
                  cin >> x;
                  x = x - 1;
                  
                  //traversing in product array
                  for(int y = 0; y<30; y++)
                  {
                     //checking if entered number is same as prduct number
                     if(x == y)
                     {
                        //Displaying matched product details
                        cout<<"Product Found!";
                        cout<<"\nProduct Name : "<<products[x].name;
                        cout<<"\nProduct Price : "<<products[x].price;
                        cout<<"\nProduct Quantity : "<<products[x].quantity;
                        
                        //Procedure to purchase
                        int quan;
                        mark:
                           //Taking input of quantity
                           cout<<"\n\nEnter Quantity to purchased : ";
                           cin>>quan;
                           
                           //Checking if quantity available
                           if(quan < products[x].quantity)
                           {
                              cout<<"Product Purchased";
                              products[x].quantity = products[x].quantity - quan;
                           }
                           else
                           {
                              cout<<"Quantity Not Available";
                              goto mark;
                           }
                     }
                  }
                  
                  break;

         case 6:
                  break;
         
         default:   //Deafault Message
                  cout << "-----------------------------------------"<<endl;
                  cout << "You have not chosen any choice yet"<< endl;
                  cout << "Enter any key to return to the main menu "<<endl;
                  cin >> ws;
                  break;
         
         }
   
   } while (choice != 6);
   
   return 0;

}
/* Main Function Ends */

Output:


Add a comment
Know the answer?
Add Answer to:
1. in this programe i want add some products to be shown after choosing choise (...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • using the source code at the bottom of this page, use the following instructions to make...

    using the source code at the bottom of this page, use the following instructions to make the appropriate modifications to the source code. Serendipity Booksellers Software Development Project— Part 7: A Problem-Solving Exercise For this chapter’s assignment, you are to add a series of arrays to the program. For the time being, these arrays will be used to hold the data in the inventory database. The functions that allow the user to add, change, and delete books in the store’s...

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • i have two issues that i need help. 1- that in case three and two only...

    i have two issues that i need help. 1- that in case three and two only one line is being read from the file 2- my case 4 is not working correctly as it should as the output is only " Enter 1 for Trump, 2 for Warren:" #include <iostream> #include <cctype> // For the letter checking functions #include <fstream> // For file input #include <iomanip> // For setw #include <ctime> #include <cstdlib> // For exit and abs #include <errno.h>...

  • How can I make this compatible with older C++ compilers that DO NOT make use of...

    How can I make this compatible with older C++ compilers that DO NOT make use of stoi and to_string? //Booking system #include <iostream> #include <iomanip> #include <string> using namespace std; string welcome(); void print_seats(string flight[]); void populate_seats(); bool validate_flight(string a); bool validate_seat(string a, int b); bool validate_book(string a); void print_ticket(string passenger[], int i); string flights [5][52]; string passengers [4][250]; int main(){     string seat, flight, book = "y";     int int_flight, p = 0, j = 0;     int seat_number,...

  • I need help in my C++ code regarding outputting the enums in string chars. I created...

    I need help in my C++ code regarding outputting the enums in string chars. I created a switch statement for the enums in order to output words instead of their respective enumerated values, but an error came up regarding a "cout" operator on my "Type of Item" line in my ranged-based for loop near the bottom of the code. I tried casting "i.type" to both "i.GroceryItem::Section::type" and "i.Section::type", but neither worked. Simply put, if a user inputs 1 as their...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • I am trying to run this program in Visual Studio 2017. I keep getting this build...

    I am trying to run this program in Visual Studio 2017. I keep getting this build error: error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". 1>Done building project "ConsoleApplication2.vcxproj" -- FAILED. #include <iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; void showChoices() { cout << "\nMAIN MENU" << endl; cout << "1: Addition...

  • c++ programming : everything is done, except when you enter ("a" ) in "F" option ,...

    c++ programming : everything is done, except when you enter ("a" ) in "F" option , it does not work. here is the program. #include <iostream> #include <string> #include <bits/stdc++.h> #include <iomanip> #include <fstream> using namespace std; #define MAX 1000 class Inventory { private: long itemId; string itemName; int numberOfItems; double buyingPrice; double sellingPrice; double storageFees; public: void setItemId(long id) { itemId = id; } long getItemId() { return itemId; } void setItemName(string name) { itemName = name; } string...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT