Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ random acesss files, structures Objectives :Create a loop with a menu of opt

ID: 3576315 • Letter: C

Question

C++ random acesss files, structures

Objectives:Create a loop with a menu of options, write to and read from a binary file, use a struct to store a record's data, figure out which C++ libraries are required

Overview:This assignment involves the use of a struct and a random access binary file. You will create the user interface which lets the user store and retrieve records. Each record in the file has the following fields:

id: integer

price: double

quantity: integer

Requirements:Implement the following pseudocode:

Sample Run:

Explanation / Answer

#include<iostream>
#include<fstream>
using namespace std;

struct Record
{
   int id;
   double price;
   int quantity;
};
int main()
{
   //Declare any needed local variables.
   int numRecs = 0,n;
   char choice;
   //Create a variable to store one record.
   struct Record rec;
  
   ifstream in;
  
   ofstream out;
   if (!in.is_open())
   {
       //open the file "rafile.bin" for binary output
       out.open("rafile.bin", ios::binary | ios::out);
       //if the file opened then
       if (out)
       {
           //close the file
           out.close();
           //open the file "rafile.bin" for binary input and output
           in.open("rafile.bin", ios::binary | ios::in);
           out.open("rafile.bin", ios::binary | ios::out);

       }
       else
       {

           //display an error message(file could not be opened)
           cout << "File cant be opened " << endl;
           //exit with an error status
           return -1;
       }
   }

   //find out how many records are in the file and store that in numRecs
   while (!in.eof())
   {
       ++numRecs;
   }
   //   display the number of records that are in the file
   cout << "the number of records that are in the file = " << numRecs << endl;

   //do the following
   do
   {
       //display a menu as shown in the sample runs provided with this assignment
       cout << "r) read a record a) append a record q) quit" << endl;
       //get the user's choice and convert it to lowercase
       cout << "Enter your choice: ";
       cin >> choice;
       //if the user chose to read a record then
       if (choice == 'r')
           //ask for a record number
       {
           cout << "Record number to be read : ";
           cin >> n;
           //if that record number does not exist then
           //display an error message
           if (n <= 0)
           {
               cout << "Invalid number .. ";
           }
           else
           {
               in.read((char*)&rec + n, sizeof(rec));
               cout << "Id= "<<rec.id<<endl;
               cout << "price= " << rec.price << endl;
               cout << "Id= " << rec.quantity << endl;
           }

       }

       //otherwise if the user chose to append a record then
       if (choice == 'a')
       {
           //get an id, price, and quantity from the user and store it in a record
           cout << "Enter an ID : ";
           cin >> rec.id;
           cout << "Enter a price: ";
           cin >> rec.price;
           cout << "Enter a quantity: ";
           cin >> rec.quantity;
           //write that record to the end of the file
           //increase the numRecs variable
           //display which record number was written to the file
           out.write((char*)&rec + numRecs, sizeof(rec));
           ++numRecs;
       }
       else if (choice == 'q')
       {
           break;
       }
       /*otherwise if the user chose to quit then
       do nothing
       otherwise
       display an error message for invalid option chosen*/
       else
       {
           cout << "Invalid choice" << endl;
       }
   } while (choice != 'q'); //while the user has not chosen to quit

   in.close();
   out.close();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote