Question

In: Computer Science

Write a program that uses a structure to store the following inventory information in a Binary...

Write a program that uses a structure to store the following inventory information in a Binary file and access it using Random Access Method: 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 the file Display any record in the file Change any record in the file For exact menu option text please see the test cases below

Solutions

Expert Solution

Solution

Code

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <cctype>
using namespace std;

struct Inventory
{
   string description,Date;  
   int quantity;          
   double wholesalecost, RetailCost;  
};


void addnewrecord(Inventory &, fstream &);
void displayanyrecord(Inventory &, fstream &);
void changeanyrecord(Inventory &, fstream &);
long byteNum(int);
void showerror();
bool checkdate(string);

int main ()
{
   Inventory record;
   int Input;

   fstream File("inventory.dat", ios::out | ios::in | ios::binary);
   if (!File)
   {
       cout << "Error opening file.\n";
       return 0;
   }

   cout << " Inventory \n"
       << "Choose othe option:\n";
   cout << " 1. Adding new records to the file.\n";
   cout << " 2. Displaying any record in the file.\n";
   cout << " 3. Changing any record in the file.\n";
   cin >> Input;

   switch (Input)
   {
       case 1 : addnewrecord(record, File);
               break;
       case 2 : displayanyrecord(record, File);
               break;
       case 3 : changeanyrecord(record, File);
   }

   File.close();
   return 0;
}


void addnewrecord(Inventory &record, fstream &File)
{
   File.seekp(0L, ios::end);
   cout << "Enter the following inventory information:\n";
   cout << "Item description: ";
   cin.ignore();
   getline(cin, record.description);
   do
   {
       cout << "Date in the format MM/DD/YYYY: ";
       cin.ignore();
       getline(cin, record.Date);

       if (checkdate(record.Date) == 0)
       {
           cout << checkdate(record.Date) << endl;
           cout << "Error! Invalid date format.\n";
       }

   } while (checkdate(record.Date) == 0);
   do
   {
       cout << "Quantity :";
       cin >> record.quantity;
       if (record.quantity < 0)
           showerror();
   } while (record.quantity < 0);
   do
   {
       cout << "Wholesale cost: ";
       cin >> record.wholesalecost;
       if (record.quantity < 0)
           showerror();
   } while (record.wholesalecost < 0);
   do
   {
       cout << "Retail cost: ";
       cin >> record.RetailCost;
       if (record.RetailCost < 0)
           showerror();
   } while (record.RetailCost < 0);
   File.write(reinterpret_cast<char *>(&record), sizeof(record));
}


void displayanyrecord(Inventory &record, fstream &File)
{
   int recNum;

   cout << "Enter the record number: ";
   cin >> recNum;
   if (recNum < 1)
       recNum = 1;
   recNum--;
   File.seekg(byteNum(recNum), ios::beg);
   File.read(reinterpret_cast<char *>(&record), sizeof(record));
   cout << "Record number: " << (recNum + 1) << endl;
   cout << "Item description: ";
   cout << record.description << endl;
   cout << "Date : ";
   cout << record.Date << endl;
   cout << "Quantity :";
   cout << fixed << showpoint << setprecision(2);
   cout << record.quantity << endl;
   cout << "Wholesale cost: ";
   cout << record.wholesalecost << endl;
   cout << "Retail cost: ";
   cout << record.RetailCost << endl;
}


void changeanyrecord(Inventory &record, fstream &File)
{
   int recNum;

   cout << "Enter the record number: ";
   cin >> recNum;
   if (recNum < 1)
       recNum = 1;
   recNum--;
   File.seekg(byteNum(recNum), ios::beg);
   File.read(reinterpret_cast<char *>(&record), sizeof(record));
   cout << "Enter the following inventory information:\n";
   cout << "Item description: ";
   cin.ignore();
   getline(cin, record.description);
   do
   {
       cout << "Date in the format MM/DD/YYYY: ";
       cin.ignore();
       getline(cin, record.Date);
       if (!checkdate(record.Date))
       {
           cout << checkdate(record.Date) << endl;
           cout << "Error! Invalid date format.\n";
       }
   } while (!checkdate(record.Date));
   do
   {
       cout << "Quantity :";
       cin >> record.quantity;
       if (record.quantity < 0)
           showerror();
   } while (record.quantity < 0);
   do
   {
       cout << "Wholesale cost: ";
       cin >> record.wholesalecost;
       if (record.quantity < 0)
           showerror();
   } while (record.wholesalecost < 0);
   do
   {
       cout << "Retail cost: ";
       cin >> record.RetailCost;
       if (record.RetailCost < 0)
           showerror();
   } while (record.RetailCost < 0);
   File.seekp(byteNum(recNum), ios::beg);
   File.write(reinterpret_cast<char *>(&record), sizeof(record));
}


long byteNum(int recNum)
{
   return sizeof(Inventory) * recNum;
}


void showerror()
{
   cout << "Error the number should be greater than 0.\n";
}

bool checkdate(string date)
{
   if ( date.length() == 9)
       date = "0" + date;

   if (date.length() != 10)
       return false;
  
   for (int i = 0; i < date.length(); i++)
   {
       if (i == 2 || i == 5)
       {
           if (date[i] != '/')
           {
               return false;
           }
           continue;
       }

       if (!isdigit(date[i]))
           return false;
   }
   return true;
}

Screenshot

---

all the best


Related Solutions

Write a program that uses a structure to store the following weather data for a particular...
Write a program that uses a structure to store the following weather data for a particular month: Total Rainfall High Temperature Low Temperature Average Temperature. The program should have an array of 12 structures to hold weather data for an entire year. When the program runs, it should ask the user to enter data for each month. (The average temperature should be calculated.) Once the data are entered for all the months, the program should calculate and display the average...
Write a program that uses a structure to store the following data on a company division...
Write a program that uses a structure to store the following data on a company division in c++ Division Name (such as East, West, North, or South) First-Quarter Sales Second-Quarter Sales Third-Quarter Sales Fourth-Quarter Sales Total Annual Sales Average Quarterly Sales The program should use four variables of this structure. Each variable should represent one of the following corporate divisions: East, West, North, and South. The user should be asked for the four quarters’ sales figures for each division. Each...
Write a program that uses a structure to store the following data: (Remember to use proper...
Write a program that uses a structure to store the following data: (Remember to use proper formatting and code documentation) Member Name Description name student name idnum Student ID number Scores [NUM_TESTS] an array of test scores Average Average test score Grade Course grade Declare a global const directly above the struct declaration const int NUM_TESTS = 4; //a global constant The program should ask the user how many students there are and should then dynamically allocate an array of...
Write a program that uses a structure to store the following data about a customer account:...
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 array of at least 5 structures. It should let the user enter data into the array, change the contents of any element and display all the data stored in the array. The program should have a menu-driven interface and use functions as appropriate. Input validation:...
11.7: Customer Accounts Write a program that uses a structure to store the following data about...
11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Prompts And...
C++ 11.7: Customer Accounts Write a program that uses a structure to store the following data...
C++ 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account: Customer name Customer address City State ZIP code Telephone Account balance Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the data stored in the array. The program should have a menu-driven user interface. Prompts...
Write a program for a beauty store that uses an InputBox to ask the guest for...
Write a program for a beauty store that uses an InputBox to ask the guest for a membership code. Embed this in a Do loop so that the user has to keep trying until the result is a valid membership code that starts with “Beauty” (case insensitive) and is followed by 4 digits with the final digit equal to either 6 or 8. Then use a message box to display the input promotion code and inform the user that the...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Assume you need to write a Java program that uses a binary search algorithm to search...
Assume you need to write a Java program that uses a binary search algorithm to search a sorted array for a given value. 1. Write a Java pseudocode that uses recursion to accomplish the task. Here is a hint. When you are searching for a particular value in an array, there are two possible outcomes. 1) The value is found and the array index of that value is returned. 2) The value is not found and we return -1. (5...
Write a program that uses the defined structure and all the above functions. Suppose that the...
Write a program that uses the defined structure and all the above functions. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT