Question

Inventory Program (C++) Write a program that uses a structure to store the following inventory data...

Inventory Program (C++)

Write a program that uses a structure to store the following inventory data in a file:

- Item Description

-Quantity on Hand

-Wholesale cost

-Retail cost

-Date Added to Inventory

The program should have a menu that allows the user to perform the following tasks:

-Add new records to file

-Display any record in the file

-Change any record in the file

Input Validation: The program should not accept quantities, or wholesale or retail costs, less than 0. The program should not accept dates that the programmer determines are unreasonable.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution

 #include <iostream> #include <fstream> #include <cctype> using namespace std; const int DESCRIPTIONSIZE = 50, DATASIZE = 10; struct strinventory {   char description[DESCRIPTIONSIZE];      int qty;                                        int wholesaleCost;                              int retailCost;                                 char dateAdded[DATASIZE];        }; // function declaration void createstrinventory(strinventory &, fstream &, int &); int displaymenu(); void newRecord(strinventory &, fstream &, int &); void displayRecord(strinventory &, fstream &, int ); void changeRecord(strinventory &, fstream &, int ); //main function int main() {       int totRecords = 0;                     int chocice = 0;                        // creating the file if the file already exists         fstream inventoryFile("Inventory.dat", ios::in | ios::out | ios::binary | ios::trunc);  strinventory product;           if (!inventoryFile)     {               cout << "Error opening the file"<<endl;     }       createstrinventory(product, inventoryFile, totRecords);                 while (chocice != 4)    {               chocice = displaymenu();                switch (chocice)                {               case 1:                         newRecord(product,inventoryFile,totRecords);                    break;          case 2:                         displayRecord(product,inventoryFile,totRecords);                        break;          case 3:                         changeRecord(product, inventoryFile, totRecords);                       break;          case 4:                         break;          default:                        cout << "Invalid choice\n";               }       }       cout << "Program end\n";          // close the file       inventoryFile.close();  return 0; } // *=== END MAIN FUNCTION ===* // function definitions ================================================== // ====== FUNCTION DEFINITION ====== // let user fill in info for a product // user can create as many records as necessary void createstrinventory(strinventory &product, fstream &fileName, int &records) {         char choiceaddrec;                      int totRecords = 0;     do      {               cout << "Enter the details of the product:\n";            cout << "Item Description: ";             cin.getline(product.description, DESCRIPTIONSIZE);              do {                    cout << "Quantity on hand: ";                     cin >> product.qty;                       cin.ignore();           } while (product.qty < 0);           do {                    cout << "Wholesale cost: ";                       cin >> product.wholesaleCost;                     cin.ignore();           } while (product.wholesaleCost < 0);                 do {                    cout << "Retail cost: ";                  cin >> product.retailCost;                        cin.ignore();           } while (product.retailCost < 0);                            do{                     cout << "Date added to inventory (MM/DD/YYYY): ";                         cin.getline(product.dateAdded, DATASIZE);               } while (!(ispunct(product.dateAdded[2])) || !(ispunct(product.dateAdded[5])));                                 fileName.write(reinterpret_cast<char *>(&product), sizeof(product));                          cout << "Do you want to write another record? (y for yes)\n";             cin >> choiceaddrec;              cin.ignore();           ++totRecords;   } while (choiceaddrec == 'Y' || choiceaddrec == 'y');   records += totRecords; } int displaymenu() {    int choice;     cout << "\n\n =================================\n";       cout << "\tMenu\n";       cout << "Choose a chocice:\n";    cout << "1) add new records to the file\n";       cout << "2) display any record in the file\n";    cout << "3) change any record in the file\n";     cout << "4) Quit\n";      cout << "\n =================================\n\n";       cin >> choice;    cin.ignore();   return choice; } void newRecord(strinventory &product,fstream &fileName, int &records) {    fileName.clear(); // clear flags        int numRecordAdded = 0; // number of new records added  char choiceaddrec;              // To hold Y or N       // go to the end of the file to append the new record   fileName.seekg(0L, ios::end);           cout << "\n\tAdd new records\n";  do      {               cout << "Enter the following data about a product:\n";            cout << "Item Description: ";             cin.getline(product.description, DESCRIPTIONSIZE);              do {                    cout << "Quantity on hand: ";                     cin >> product.qty;                       cin.ignore();           } while (product.qty < 0);           do {                    cout << "Wholesale cost: $";                      cin >> product.wholesaleCost;                     cin.ignore();           } while (product.wholesaleCost < 0);                 do {                    cout << "Retail cost: $";                         cin >> product.retailCost;                        cin.ignore();           } while (product.retailCost < 0);            do{                     cout << "Date added to strinventory (MM/DD/YYYY): ";                      cin.getline(product.dateAdded, DATASIZE);               } while (!(ispunct(product.dateAdded[2])) || !(ispunct(product.dateAdded[5])));                                 fileName.write(reinterpret_cast<char *>(&product), sizeof(product));                          cout << "Do you want to write another record? (y for yes)\n";             cin >> choiceaddrec;              cin.ignore();           ++numRecordAdded;       } while (choiceaddrec == 'Y' || choiceaddrec == 'y');    records += numRecordAdded; } void displayRecord(strinventory &product, fstream &fileName, int records) {       fileName.clear(); // clear flags        cout << "\n\tDisplay a record\n";         int recNum;             // seek the file to the beginning       fileName.seekg(0L, ios::beg);   cout << "You have " << records << " records" << endl;   cout << "Which record would you like to display? ";       cin >> recNum;    cin.ignore();   --recNum; // minus 1 to seek properly   // seek to the record, store(read it), and display it   fileName.seekg(sizeof(product) * recNum, ios::beg);     fileName.read(reinterpret_cast<char *>(&product), sizeof(product));   cout << "Displaying record #" << (recNum + 1) << endl << endl;  cout << "Item description: " << product.description << endl;  cout << "Quantity on hand: " << product.qty << endl;  cout << "Wholesale cost: " << product.wholesaleCost << endl;  cout << "Retail cost: " << product.retailCost << endl;        cout << "Date added to strinventory (MM/DD/YYYY): " << product.dateAdded << endl;     // seek the file back to the beginning  fileName.seekg(0L, ios::beg); } void changeRecord(strinventory &product, fstream &fileName, int records) {      fileName.clear(); // clear flags        int recCh;              // record to change     cout << "\n\tChange a record\n";  cout << "You have " << records << " records" << endl;   cout << "Which record would you like to change? ";        cin >> recCh;     cin.ignore();   --recCh; // minus 1 to seek properly    // seek to the record   fileName.seekg(sizeof(product) * recCh, ios::beg);      // let the user change the record       cout << "Changing record #" << (recCh + 1) << endl << endl;     cout << "Enter the following data about a product:\n";    cout << "Item Description: ";     cin.getline(product.description, DESCRIPTIONSIZE);      do {            cout << "Quantity on hand: ";             cin >> product.qty;               cin.ignore();   } while (product.qty < 0);   do {            cout << "Wholesale cost: $";              cin >> product.wholesaleCost;             cin.ignore();   } while (product.wholesaleCost < 0);         do {            cout << "Retail cost: $";                 cin >> product.retailCost;                cin.ignore();   } while (product.retailCost < 0);    do{             cout << "Date added to strinventory (MM/DD/YYYY): ";              cin.getline(product.dateAdded, DATASIZE);       } while (!(ispunct(product.dateAdded[2])) || !(ispunct(product.dateAdded[5])));         // rewrite over record in the file      fileName.write(reinterpret_cast<char *>(&product), sizeof(product));  // seek the file back to the beginning  fileName.seekg(0L, ios::end);   cout << "The file has been rewritten\n"; } 

Screenshot

Enter the following data about a product: Item Description: Pepsi Quantity on hand: 15 Wholesale cost: $5 Retail cost: $6 Dat

Add a comment
Know the answer?
Add Answer to:
Inventory Program (C++) Write a program that uses a structure to store the following inventory data...
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
  • please write in C++ Write a program that uses a structure to store the following inventory...

    please write in C++ Write a program that uses a structure to store the following inventory data in a file: The data can be either read from a text file or the keyboard Item name (string) Quantity on hand(int) Wholesale cost(double) Retail Cost(double) The program should have a menu that allows the user to perform the following tasks: Add new records to the file Display any record in the file User will provide the name of the item or the...

  • Write a C++ program that uses a structure to store the following inventory information in a...

    Write a C++ program that uses a structure to store the following inventory information in a file: ⦁   Item description, Quantity on hand, Wholesale cost, Retail cost, and Date added to inventory. ⦁   Use a char array for item description and date. ⦁   The program should have a menu that allows the user to perform the following tasks: i.   Add a new record at the end of the file. ii.   Display any record in the file. iii.   Change any record...

  • Write a program that uses a structure to store the following data about a customer account: Name Address City, sta...

    Write a program that uses a structure to store the following data about a customer account: Name Address City, state, and ZIP Telephone number Account Balance Date of last payment The program should use an vector of at least 20 structures. It should let the user enter data into the vector, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Input Validation: When the data for...

  • Write a program that keeps track of a speakers’ bureau. The program should use a structure...

    Write a program that keeps track of a speakers’ bureau. The program should use a structure to store the following data about a speaker: (this is in C++) Name Telephone Number Speaking Topic Fee Required The program should use a vector of structures. It should let the user enter data into the vector, change the contents of any element, and display all the data stored in the vector. The program should have a menu-driven user interface. Input Validation: When the...

  • Write a C program Design a program that uses an array to store 10 randomly generated...

    Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...

  • Need this program in C#. Thank you 3/19 Lab11 PartC Dur Scenario/Summary Write a windows console...

    Need this program in C#. Thank you 3/19 Lab11 PartC Dur Scenario/Summary Write a windows console application that holds data about an item in a retail store. Your class should be named Retailltem and should hold data about an item in a retail store. The class will have the following member variables. Description- string holding the description of the item, unitsOnHand - int that holds the number of units in inventory Price - double that holds the price of the...

  • Write a C program that takes inventory data from a file and loads a structure of up to 100 items ...

    Write a C program that takes inventory data from a file and loads a structure of up to 100 items defined as: struct item { int item_number; char item_name[20]; char item_desc[30]; float item_price; } I called mine: struct item inventory[100]; (you may use any name you wish) I will let you decide the appropriate prompts and edit messages. You will read the data from the data file and store the info in an array. Assume no more than 100 records...

  • Customer Accounts This program should be designed and written by a team of students. Here are...

    Customer Accounts This program should be designed and written by a team of students. Here are some suggestions: Write a program that uses a structure to store the following information about a customer account: • Name • Address • City, state, and ZIP • Telephone number • Account balance • Date of last payment The structure should be used to store customer account records in a file. The program should have a menu that lets the user perform the following...

  • You are to write a program IN C++ that asks the user to enter an item's...

    You are to write a program IN C++ that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: if the an item's wholesale cost is 5.00 and its markup percentage is 100%, then the item's retail price is 10.00 If an item's wholesale cost is 5.00 and its markup percentage is 50%, then the item's retail price is 7.50 Program design specs. You should have 5...

  • Write a program (C++) that shows Game sales. The program should use a structure to store...

    Write a program (C++) that shows Game sales. The program should use a structure to store the following data about the Game sale: Game company Type of Game (Action, Adventure, Sports etc.) Year of Sale Sale Price The program should use an array of at least 3 structures (3 variables of the same structure). It should let the user enter data into the array, change the contents of any element and display the data stored in the array. The program...

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