Having trouble with Random Access Files

I will start with posting the instructions that out professor gave us so there is no confusion on what I am trying to accomplish.

You are the owner of a hardware store and need to keep an inventory that can tell you what different tools you have, how many of each you have on hand and the cost of each one. Write a program that initializes the random-access file hardware.dat to 100 empty records, lets you input the data concerning each tool, enables you to list all your tools, lets you delete a record for a tool that you no longer have and lets you update any information in the file. The too identification number should be the record number. Your program should include a menu that allows the user to select various options for a specific record number. Save the file as
InventorySystem.cpp. Submit the code along with the hardware.dat file. Use the following information to start your file.

This is what he is asking us. I wrote the program but I cannot figure out what is wrong with it. I will post my program below and then the error I am getting.

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

// class for the hardware inventory
class Inventory
{
private:
string toolName;
int toolQty;
double toolCost;
int recordNum;
public:
string getToolName()
{
return toolName;
}
int getToolQty()
{
return toolQty;
}
double getToolCost()
{
return toolCost;
}
int getRecordNum()
{
return recordNum;
}
//-------------------------------------
void setToolName(string TN)
{
toolName = TN;
}
void setToolQty(int TQ)
{
toolQty = TQ;
}
void setToolCost(double TC)
{
toolCost = TC;
}
void setRecordNum(int RN)
{
recordNum = RN;
}
};

int main()
{
string toolNames;
int toolQtys;
double toolCosts;
int recordNum;
int size = 100;

fstream outHardware("hardware.dat", ios::in | ios::out);

if (!outHardware)
{
cout << "File not available\n";
exit(1);
}

Inventory hardware;

cout << "Enter record number or hit 0 to end\n";
cin >> recordNum;
cout << endl;

while (recordNum > 0 && recordNum <= 100)
{
hardware.setRecordNum(recordNum);
cout << "Tool Name" << setw(15) << "Quantity" << setw(10) << "Cost" << endl;

cin >> toolNames >> setw(14) >> toolQtys >> setw(5) >> toolCosts;
cout << endl << endl;

hardware.setToolName(toolNames);
hardware.setToolQty(toolQtys);
hardware.setToolCost(toolCosts);

outHardware.seekp((hardware.getRecordNum() - 1) * sizeof(hardware));

outHardware.write(reinterpret_cast<char*> (&toolNames), sizeof(string));
outHardware.write(reinterpret_cast<char*> (&toolQtys), sizeof(int));
outHardware.write(reinterpret_cast<char*> (&toolCosts), sizeof(double));

cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter record number or 0 to end\n";
cin >> recordNum;
}

outHardware.close();

return 0;
}

Error:

1>------ Build started: Project: Assignement 13, Configuration: Debug Win32 ------
1>LINK : fatal error LNK1168: cannot open C:\Users\Brayden\Desktop\Spring Semster 2017\C++\Week 12\Assignement 13\Debug\Assignement 13.exe for writing
1>Done building project "Assignement 13.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any help is appreciated. Thanks!
Last edited on
If the program Assignement 13.exe is running you can't build it.

Make sure all windows are closed and try again.
If that's not it, delete the file and try again.

You might also take out the space, but I don't know if that would cause this.
Better yet, try saving it to a new name
I will try that. I had two other parts of the assignment that I ran in the solution that worked fine but they are currently excluded from the project. That is how I have done all my assignments up to now.
Question. should I make a new solution entirely or just create a new source file in the solution?
Okay, so I made a new solution and source file just to cover both. It compiled fine just like it did up top. But when I went to run the program it is telling "File not available". This leads me to believe that there is something wrong with the hardware.dat in my program. I do not have the file already on my computer but from what we learned this week, I was under the impression that when you called to it as I did in the program, it would create the file so it could be used. Am I incorrect? or is there something else that I am missing?
Topic archived. No new replies allowed.