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

C++ Binary Files - I have already done most of this assignment, I just need to s

ID: 644384 • Letter: C

Question

C++ Binary Files - I have already done most of this assignment, I just need to switch the file to binary now. Use the binary file provided in the dropbox link:

https://www.dropbox.com/s/eomefbv9r1r5ljj/drink_machine.bin?dl=0

My code so far:

//drinkmachine.h
#ifndef _DRINKMACHINE_H_
#define _DRINKMACHINE_H_
#include<string>
// All of your header file declarations for the DrinkMachine go here.
//
// The external structures and any functions that will be called by
// the drinkMachineDrive will need to go here.
//
// Structure for a Drink Item
struct DrinkItem
{
   int id;
   std::string name;
   double price;
   int numberofDrinks;
   int drinksPurchased;
};
// Structure for the Drink Machine
struct DrinkMachine
{
   int version;
   int NumberOfItems;
   DrinkItem* drinkItem;
   int currentLocation;
};
// Function prototypes
DrinkMachine* create();
void destroy(DrinkMachine *drinkMachine);
DrinkItem* firstDrink(DrinkMachine* address1);
DrinkItem* nextDrink(DrinkMachine *pDrinkMachine);
#endif

// drinkmachine.cpp
#include "drinkmachine.h"
#include<iostream>
#include<fstream>
using namespace std;
const int INVALID_INDEX = -1;

// prototypes for any "internal" functions go there. These are
// functions that drinkmachine.cpp needs but are NOT called
// by drinkMachineDriver.cpp
// Create the Drink Machine
DrinkMachine* create()
{
   ifstream input;
   int index = 0; //how do i connect this to the structure Drink Machine
   int count = 0;
   input.open("drink_machine.txt");
   while (!input)
   {
       return NULL;
   }
   input >> index; //reads in the length of the array from the first line of the input file
   DrinkMachine *local = new DrinkMachine;
   local->version = 1;
   local->currentLocation = INVALID_INDEX;
   local->NumberOfItems = index;
   local->drinkItem = new DrinkItem[index];
   while (count<index) //streams information into the arrays
   {
       input >> local->drinkItem[count].name; //array holds translation characters from the file
       input >> local->drinkItem[count].price;
       input >> local->drinkItem[count].numberofDrinks;
       local->drinkItem[count].id=count+1;
       local->drinkItem[count].drinksPurchased=0;
       count++;
   }
   input.close();
   return local;
}
// Destroy the Drink Machine
void destroy(DrinkMachine *pDrinkMachine)
{
   delete[] pDrinkMachine->drinkItem; // You have created new using [] so use same while destruting.
   delete pDrinkMachine;   // u have just used new so just use delete.
}
DrinkItem* firstDrink(DrinkMachine* address1)
{
   if(address1->drinkItem == NULL){
       address1->currentLocation = INVALID_INDEX;
       return NULL;
   }
   address1->currentLocation = 0;
   return (address1->drinkItem);
}
DrinkItem* nextDrink(DrinkMachine *pDrinkMachine)
{
   if(pDrinkMachine->currentLocation == INVALID_INDEX){
       return NULL;
   }
   else{
       pDrinkMachine->currentLocation++;
       if(pDrinkMachine->currentLocation < pDrinkMachine->NumberOfItems)
           return (pDrinkMachine->drinkItem+pDrinkMachine->currentLocation);
       else{
           pDrinkMachine->currentLocation = INVALID_INDEX;
           return NULL;
       }
   }  
}
int items(DrinkMachine *pDrinkMachine)
{
   return pDrinkMachine->NumberOfItems;
}
bool available(DrinkMachine *pDrinkMachine, int id)
{
   for(int i=0; i<pDrinkMachine->NumberOfItems; i++){
       if(pDrinkMachine->drinkItem[i].id == id && pDrinkMachine->drinkItem[i].numberofDrinks>=1)
           return true;
   }
   return false;
}
double cost(DrinkMachine *pDrinkMachine, int id)
{
   for(int i=0; i<pDrinkMachine->NumberOfItems; i++){
       if(pDrinkMachine->drinkItem[i].id == id)
           return pDrinkMachine->drinkItem[i].price;
   }
   return -1;
}
/*
enum class Purchase{ PURCHASED, INVALID, NOT_AVAILABLE, INSUFFICIENT_FUNDS };
Purchase purchase(DrinkMachine *pDrinkMachine,int id, double amount, double &change)
{
   if(available(pDrinkMachine,id)){
       if(pDrinkMachine->drinkItem[id-1].numberofDrinks>=1){
           if(amount<cost(pDrinkMachine,id)){
               return Purchase::INSUFFICIENT_FUNDS;
           }
           else{
               pDrinkMachine->drinkItem[id-1].numberofDrinks--;
               pDrinkMachine->drinkItem[id-1].drinksPurchased++;
               change = cost(pDrinkMachine,id);
               return Purchase::PURCHASED;
           }
       }
       else
           return Purchase::NOT_AVAILABLE;
   }
   else
       return Purchase::INVALID;
}*/


//driver.cpp
#include<iostream>
#include<iomanip>
#include "drinkmachine.h"
using namespace std;
int main(){
    DrinkMachine* myDrinkMachine = create();
    DrinkItem* pDrink;
    cout << setw(4) <<"Id" << setw(13)<<"Name" << setw(9) <<"Price" << setw(5)<<"Qty" << setw(5) <<"Sold"<< endl;
    for(pDrink = firstDrink(myDrinkMachine); pDrink!=NULL; pDrink=nextDrink(myDrinkMachine)){
       cout << setw(4) <<pDrink->id << " " <<setw(13)<<
        pDrink->name << " " <<setw(6) <<
         pDrink->price << " " <<setw(4) <<
          pDrink->numberofDrinks << " " <<setw(3) <<
           pDrink->drinksPurchased << " " << endl;
    }
    destroy(myDrinkMachine);
    return 0;
}

Explanation / Answer

int main()
{
DrinkMachine* myDrinkMachine = create();
DrinkItem* pDrink;
   ofstream file;

file.open("Output.txt",ios::in,ios::out); //ios::out | ios::trunc

cout << setw(4) <<"Id" << setw(13)<<"Name" << setw(9) <<"Price" << setw(5)<<"Qty" << setw(5) <<"Sold"<< endl;
for(pDrink = firstDrink(myDrinkMachine); pDrink!=NULL; pDrink=nextDrink(myDrinkMachine))
   {
file << setw(4) <id << " " < file<< pDrink->name << " " < file<< pDrink->price << " " < file<< pDrink->numberofDrinks << " " < file<< pDrink->drinksPurchased << " " << endl;
}
   file.close();
destroy(myDrinkMachine);
return 0;
}