//setting the values of tool names, quantity, cost, and record number
hardware.setNameOfTools (nameOfTools);
hardware.setQtyOfTools (qtyOfTools);
hardware.setCostOfTools (costOfTools);
//seek position in file of user-specified record
outHardware.seekp ( (hardware.getRecNum() -1) * sizeof (hardware));
//write user-specified information in file
outHardware.write (reinterpret_cast < const char * > (&hardware),
sizeof (hardwareData) );
//enables the user to enter another item.
cout << "Enter Record Number. (Enter 0 to end input.)" << endl << endl;
}//end while
#include <iostream>
#include <string>
#include <limits>
#include <fstream>
#include <cstdlib>
#include <iomanip>
usingnamespace std;
class hardwareData
{
public:
string getNameOfTools()
{
return nameOfTools;
}//end funtion getNameOfTools
void setNameOfTools (string nameOfToolsValue)
{
nameOfTools = nameOfToolsValue;
}//end function setNameOfTools
int getQtyOfTools()
{
return qtyOfTools;
}//end funtion getQtyOfTools
void setQtyOfTools (int qtyOfToolsValue)
{
qtyOfTools = qtyOfToolsValue;
}//end of function setQtyOfTools
double getCostOfTools()
{
return costOfTools;
}//end function getCostOfTools
void setCostOfTools (double costOfToolsValue)
{
costOfTools = costOfToolsValue;
}//end function setCostOfTools
int getRecNum()
{
return recNum;
}//end function getRecNum
void setRecNum (int recNumValue)
{
recNum = recNumValue;
}//end function setRecNum
private:
string nameOfTools;
int qtyOfTools;
double costOfTools;
int recNum;
};//end class hardwareData
int main ()
{
string nameOfTools;
int qtyOfTools;
double costOfTools;
int recNum;
int recsize = sizeof(string) + sizeof(int) + sizeof(double) + sizeof(int);
fstream outHardware("hardware.dat", ios::in | ios::out);
//exit program if ofstream could not open file
if (!outHardware)
{
cerr << "File could not be opened." << endl;
exit (1);
}//end if
hardwareData hardware;
cout << "Enter Record Number. (Enter 0 to end input.)" << endl;
cin >> recNum;
cout << endl;
while (recNum > 0 && recNum <=100)
{
hardware.setRecNum(recNum);
cout << "Tool Name" << setw(15) << "Quantity" << setw(10)
<< "Cost/Unit" << endl;
cin >> nameOfTools >> setw(14) >> qtyOfTools
>> setw(5) >> costOfTools;
cout << endl << endl;
//setting the values of tool names, quantity, cost, and record number
hardware.setNameOfTools (nameOfTools);
hardware.setQtyOfTools (qtyOfTools);
hardware.setCostOfTools (costOfTools);
//seek position in file of user-specified record
outHardware.seekp ( (hardware.getRecNum() -1) * sizeof (hardware));
//write user-specified information in file
outHardware.write (reinterpret_cast<char*> (&nameOfTools),
sizeof (string) );
outHardware.write (reinterpret_cast<char*> (&qtyOfTools),
sizeof (int) );
outHardware.write (reinterpret_cast<char*> (&costOfTools),
sizeof (double) );
//enables the user to enter another item.
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter Record Number. (Enter 0 to end input.)" << endl;
cin >> recNum;
}//end while
outHardware.close();
return 0;
}//end funtion main
Here's the problems I found:
1. You were declaring a constructor that accepted 4 values but never defined what the constructor did with those and then just set the values inside the main body of the code anyways.
2. You never defined the size of a record, nor applied where a record would be so it never knew where to write and where not to. The first time I ran it I got a hardware.dat that was 180MB o.O;
3. After the first run, you didn't ask for a new record number so it was stuck in the loop of writing in the same place and never letting you exit the program.
4. Didn't close the file at the end. Technically, you don't have to but I HIGHLY suggest it.