Help,
I still cannot understand why the following function will not write the change to my program correctly. For my other functions (add a record and delete record, it wrote all the records perfectly to the file, but for some reason when I go to update a record it does not do this. Someone said it could be my seekg but I don't understand why it worked before but not on this function:
// update the new quantity and unit price in the record
void Tools::updateRecord( fstream &updateFile )
{
// obtain number of account to update
int partNumber = getPart( "\nEnter tool part identification number to update: " );
// move file-position pointer to correct record in file
updateFile.seekg( ( partNumber - 1 ) * sizeof ( Tools ) );
// read first record from file
Tools tool;
updateFile.read( reinterpret_cast < char * >( &tool ),
sizeof( Tools ) );
// update record
if ( tool.getPartNumber() != 0 )
{
printHeader();
outputLine( updateFile, tool ); // display the record
// request user to specify new quantity
cout << "\nEnter new current quantity: ";
int currentQuantity; // change the quantity
cin >> currentQuantity;
tool.setInStock( currentQuantity ); // update record
// request user enter a new unit price
cout << "Enter new unit price for the tool: ";
double currentPrice; // change the unit price
cin >> currentPrice;
tool.setUnitPrice( currentPrice ); // update record
cout << "\nPart # " << partNumber << " has been updated.\n";
// print header in the output prompt screen
printHeader();
outputLine( updateFile, tool ); // display the record
// move file-position pointer to correct record in file
updateFile.seekp( ( partNumber - 1 ) * sizeof( Tools ) );
// write updated record over old record in file
updateFile.write( reinterpret_cast < const char * >( &tool ),
sizeof( Tools ) );
} // end if
else // display error if account does not exist
cerr << "\nPart identification # " << partNumber << " has no information." << endl;
} // end function updateRecord
I also am including the outputLine function that is called:
void Tools::outputLine(ostream &output, const Tools &record )
{
output << left << setw(10 ) << record.getPartNumber()
<< setw( 16 ) << record.getToolName()
<< right << setw(9) << record.getInStock()
<< right << setw(15) << setprecision(2) << fixed << showpoint << record.getUnitPrice() << endl;
cout << left << setw(10) << record.getPartNumber()
<< setw( 16 ) << record.getToolName()
<< right << setw(12) << record.getInStock()
<< right<< setw(15) << setprecision(2) << fixed << showpoint << record.getUnitPrice() << endl;
}