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

Hello, I am having issues making my delete function work. My program does compil

ID: 3564509 • Letter: H

Question

Hello, I am having issues making my delete function work. My program does compile but my delete function doesn't work any help would be appreciated. I haven't finished my last two functions because I am focusing on the delete but any advice on how to Sell a title and print the value of all sold titles would be nice as well.

#include
#include
#include
#include
#include
#include

using namespace std;

//declare variables
void Add();
void Edit();
void Display();
void Delete();
void Sell();
void Value();
void printRecords();

struct Inventory
{
    char title[35], artist[25], type[12];
    int year;
    int price;
    int choice;
    bool dead_flag;


};


int main()
{
    int choice;
    do
    {
        cout << "MENU" << endl;
        cout << "1. Add Record(s): " << endl;
        cout << "2. Display Records: " << endl;
        cout << "3. Edit Records: " << endl;
        cout << "4. Delete Records: " << endl;
        cout << "5. Sell a Title: " << endl;
        cout << "6. Sold Value: " << endl;

        cout << "Please enter your selection. > ";
        cin >> choice;

        switch (choice)// change into an if else statement then turn into a function
        {
        case 1:
            Add();//Add record
            break;
        case 2:
            Display();//Display record
            break;
        case 3:
            Edit();//Edit record
            break;
        case 4:
            Delete();//Delete record
            break;
        case 5:
            Sell(); //Sell record
            break;
        case 6:
            //   Value(); //value of sold records
            break;


        default:
            cout << "Invalid Selection" << endl;
        }
    }
    while(choice >= 7);

    system("PAUSE");
    return 0;
}

//Add function
void Add()
{
    system("CLS"); //clears screen

    fstream fout;
    Inventory inv;
    char ch;

    fout.open("output.bin", ios::out | ios::binary | ios::app);

    do
    {
        cout << "Enter title: ";
        cin >> inv.title;
        cout << "Enter artist: ";
        cin >> inv.artist;
        cout << "Enter type: ";
        cin >> inv.type;
        cout << "Enter price: ";
        cin >> inv.price;
        cout << "Enter year: ";
        cin >> inv.year;
        cout << endl;
        //write record to file
        fout.write(reinterpret_cast(&inv), sizeof(inv));

        cout << "Do you want to add another record? " << endl;
        cin >> ch;
    }
    while(tolower(ch) == 'y');

//close the file
    fout.close();
}

//"Display" function
void Display()
{
    system("CLS"); //clears screen

    Inventory inv;
    fstream fout;

    fout.open("output.bin", ios::in | ios::binary);

    fout.read(reinterpret_cast (&inv), sizeof(inv));

    while (!fout.eof())
    {

        cout << " Title : "   << inv.title;
        cout << " Artist : " << inv.artist;
        cout << " Type : "    << inv.type;
        cout << " Year : "    << inv.year;
        cout << " Price : "   << inv.price;
        cout << endl;
        fout.read(reinterpret_cast (&inv), sizeof(inv));

    }
//close the file
    fout.close();
}

void Edit()
{
    int x;
    Inventory inv;
    fstream fout;

    fout.open("output.bin", ios::in|ios::binary);
    fout.seekg(0, ios::end);
    int count = fout.tellg()/sizeof(inv);
    cout << " There are "<< count <<" record in the file";
    cout << " Enter Record Number to edit: ";
    cin >> x;

    fout.seekg((x-1)*sizeof(inv));
    fout.read(reinterpret_cast(&inv), sizeof(inv));

    cout << "Record " << x << " has following data: " << endl;
    Display();
    fout.close();
    fout.open("output.bin", ios::out|ios::in|ios::binary);
    fout.seekp((x-1)*sizeof(inv));
    cout << " Enter data to Modify: " << endl;

    fout.write(reinterpret_cast (&inv), sizeof(inv));
}


void Delete()
{
    Inventory inv;
    fstream fout;
    int x;//to hold the record number the user wishes to delete

    fout.open("output.bin", ios::in|ios::out|ios::binary);
    fout.seekg(0, ios::end);
    int count = fout.tellg()/sizeof(inv);
    cout << " There are "<< count <<" record in the file";
    cout << " Enter Record Number to delete: ";
    cin >> x;

    fout.seekg((x-1)*sizeof(inv));
    fout.read(reinterpret_cast(&inv),sizeof(inv)); //read record from file

    inv.dead_flag = true; //Here

    fout.seekp(fout.tellg() - std::fstream::pos_type(sizeof(Inventory)));; //Move back one Inventory's worth in bytes

    fout.write(reinterpret_cast(&inv), sizeof(inv)); //Now write it back to the file

    fout.close();
}


void Sell()
{
    Inventory inv;
    fstream fout;
    int x;//to hold the record number the user wishes to sell

    fout.open("output.bin", ios::in|ios::out|ios::binary);
    fout.seekg(0, ios::end);
    int count = fout.tellg()/sizeof(inv);
    cout << " There are "<< count <<" record in the file";
    cout << " Enter Record Number to sell: ";
    cin >> x;
    x--;

}

void Value()
{
    Inventory inv;
    fstream fout;
    int x; //to display the value of sold records
    fout.open("output.bin", ios::in|ios::out|ios::binary);
    fout.seekg(0, ios::end);
    int count = fout.tellg()/sizeof(inv);
    cout << " There are "<< count <<" record in the file";
    cout << "The total value of records are: ";


}

Explanation / Answer

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
#ifndef __EMP_H_
#define __EMP_H__

class employee
{
private:
int id;
char sex;
double wage;
int years;
public:
//Constructor
employee();
//Overloaded Constructor
employee( int uid, char usex, double uwage, int uyears );
//A get and set for Employee ID
void setid( int uid );
int getid( );
//A get and set for Employee Sex
void setsex( char usex );
char getsex( );
//A get and set for Employee Wage
void setwage( double uwage );
double getwage( );
//A get and set for Employee Years w/ Company
void setyears( int uyears );
int getyears( );

};
#endif

CLASS DEFINITION employee.cpp

#include <iostream>
#include "stdafx.h"
#include "employee.h"
#include <string>
using namespace std;

//Constructor
employee::employee() {
id = 0;
sex = 'x';
wage = 0.0;
years = 0;
};

//Overloaded Constructor
employee::employee( int uid, char usex, double uwage, int uyears ) {
id = uid;
sex = usex;
wage = uwage;
years = uyears;
};
//A get and set for Employee ID
void employee::setid( int uid ) {
id = uid;
};
int employee::getid( ) {return id;};

//A get and set for Employee Sex
void employee::setsex( char usex ) {
sex = usex;
};
char employee::getsex( ) {return sex;};

//A get and set for Employee Wage
void employee::setwage( double uwage ) {
wage = uwage;
};
double employee::getwage( ) {return wage;};

//A get and set for Employee Years w/ Company
void employee::setyears( int uyears ) {
years = uyears;
};
int employee::getyears( ) {return years;};
MAIN CODE

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>
#include <conio.h>
#include "employee.h"
#include <vector>
#include <cctype>
#include <fstream>
#include <algorithm>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
const int EMPLOYEES = 3;
string filename = "c:\employee.dat";
int uid = 0;
char usex = 'q';
double uwage = 0.10;
int uyears = 100;
employee e;
vector<employeeetable;

ofstream outFile(filename.c_str());
if(outFile.fail())
{
cout << " Failed to open the data file." << endl;
exit(1);
}

outFile << setiosflags(ios::fixed)
<< setiosflags(ios::showpoint)
<< setprecision(2);

for (int i = 0; i < EMPLOYEES; i++) {
cout << " Record " << i + 1 << " of " << EMPLOYEES << endl;
cout << " Enter the Employees ID: ";
cin >uid;
do {cout << " Enter the Employees Sex: ";
cin >usex;
usex = toupper(usex);
if (usex != 'M' && usex != 'F') {
cout << " You did not enter in a valid Sex Please enter in either
M/F. ";
}
}
while(usex != 'M' && usex != 'F');
cout << " Enter the Employees Wage: ";
cin >uwage;
cout << " Enter the Employees Years with the Company: ";
cin >uyears;
cout << endl << endl << endl;
e = employee(uid, usex, uwage, uyears);
etable.push_back(e);
}

sort(etable.begin(), etable.end());

for(int i = 0; i < EMPLOYEES; i++)
{
outFile << etable[i].getid() << " "
<< etable[i].getsex() << " "
<< etable[i].getwage() << " "
<< etable[i].getyears() << endl;
}
_getch();


return 0;
}

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