Tool.h file
#ifndef TOOL_H
#define TOOL_H
#include <string>
using std::string;
const int LENGTH = 30;
class Tool
{
Tool ( int = -1; string = " "; int = 0; double = 0.0 );
public:
int getPartNumber();
int setPartNumber( void );
char getToolName();
int setToolName( void );
int getInStock();
int setInStock( void );
int getUnitPrice();
int setUnitPrice( void );
private:
int partNumber;
char toolName[ LENGTH ];
int inStock;
double unitPrice;
};
#endif
Tool.cpp file
#include <string>
using std::string;
#include <cstring>
using std::cstring;
using namespace std;
#include "stdafx.h"
#include "Tool.h"
void Tool::setPartNumber( int partNumber )
{
//sets the partNumber
partNumber = setPartNumber();
}
int Tool::getPartNumber() const
{
return partNumber;//returns the partNumber
}
void Tool::setToolName( string toolNameString )
{
//copy at most 30 characters from string to toolName
const char *toolNameValue = toolNameString.data();
int length = toolNameString.size();
length = ( length < 30 ? length: 29 );
strncpy( toolName, toolNameValue, length );
//append null-terminating character to end of toolName
toolName[ length ] = '\0';
}// end function setToolName
void Tool::getToolName( char toolName )
{
return toolName;//returns toolName
}
void Tool::setInStock( int inStock )
{
inStock = setInStock;//sets the inStock items
}
int Tool::getInStock() const
{
return inStock;//returns inStock Items
}
void Tool::setUnitPrice( double unitPrice )
{
unitPrice = setUnitPrice;//sets the setUnitPrice to unitPrice
}
void Tool::getUnitPrice( int unitPrice )
{
return unitPrice;//returns unitPrice
}
ex17_12.cpp file
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <iomanip>
using std::fixed;
using std::left;
using std::setprecision;
using std::setw;
using std::showpoint;
#include <fstream>
using std::fstream;
#include <cctype>
#include <cstdlib>
#include "stdafx.h"
#include "Tool.h"
/*// function prototypes
void initializeFile( fstream & );
void inputData( fstream & );
void listTools( fstream & );
void updateRecord( fstream & );
void insertRecord( fstream & );
void deleteRecord( fstream & );
int instructions( void );*/
// function to clear file
void initializeFile( fstream & )
{
Tool blankItem; // empty Tool object
// fill file with blank records
for ( int i = 0; i < 100; i++ )
fRef.write( reinterpret_cast< char * >( &blankItem ), sizeof( Tool ) );
} // end function initializeFile
// function that receives input
void inputData( fstream & )
{
Tool temp; // temporary Tool object
// temporary variables used to hold user input
int number;
char name[ LENGTH ];
double price;
int stock;
// ask user for and set partNumber
cout << "Enter the part number (0 - 99, -1 to end input): ";
cin >> number;
// set Tool members until -1 is entered
while ( number != -1 )
{
cout << "Enter the tool name: "; // ask user for tool name
cin.ignore(); // ignore the newline on the input stream
cin.get( name, LENGTH ); // store tool name in variable name
temp.setToolName( name ); // set tool member name
temp.setPartNumber( number ); // set part number
// ask user for quantity and price
cout << "Enter quantity and price: ";
cin >> stock >> price; // store input in temporary variables
temp.setInStock( stock ); // set inStock
temp.setUnitPrice( price ); // set unitPrice
// place file position pointer at next write location
fRef.seekp( ( temp.getPartNumber() ) * sizeof( Tool ) );
// write data to file
fRef.write( reinterpret_cast< char * >( &temp ), sizeof( Tool ) );
// ask user for next part number
cout << "Enter the part number (0 - 99, -1 to end input): ";
cin >> number;
} // end while
} // end inputData
// function that decides what choice user selected
int instructions( void )
{
int choice;
// ask user to enter a choice
cout << "\nEnter a choice:\n1 List all tools."
<< "\n2 Update record.\n3 Insert record."
<< "\n4 Delete record.\n5 End program.\n";
// ask user for choice until a valid choice is entered
do
{
cout << "? ";
cin >> choice;
}
while ( choice < 1 || choice > 5 );
return choice; // return user choice
} // end function instructions
// function that lists tools in file
void listTools( fstream &)
{
Tool temp;
// display column headings
cout << setw( 7 ) << "Record#" << " " << left
<< setw( 30 ) << "Tool name" << left
<< setw( 13 ) << "Quantity" << left << setw( 10 ) << "Cost" << endl;
// continue until 100 tools are displayed or end of file reached
for ( int count = 0; count < 100 && !fRef.eof(); count++ )
{
// set file position pointer and begin reading
fRef.seekg( count * sizeof( Tool ) );
fRef.read( reinterpret_cast< char * >( &temp ), sizeof( Tool ) );
// if part number is valid, display Tool information
if ( temp.getPartNumber() >= 0 && temp.getPartNumber() < 100 )
{
cout << fixed << showpoint;
cout << left << setw( 7 ) << temp.getPartNumber() << " "
<< left << setw( 30 ) << temp.getToolName() << left
<< setw( 13 ) << temp.getInStock() << setprecision( 2 )
<< left << setw( 10 ) << temp.getUnitPrice() << '\n';
} // end if
} // end for
} // end function listTools
// function to update a tool's information
void updateRecord( fstream & )
{
Tool temp;
int part;
char name[ LENGTH ];
int stock;
double price;
// ask user what part to update
cout << "Enter the part number for update: ";
cin >> part;
// set file position pointer to correct tool
fRef.seekg( part * sizeof( Tool ) );
// read tool information
fRef.read( reinterpret_cast< char * >( &temp ), sizeof( Tool ) );
// display tool information if partNumber is not -1
if ( temp.getPartNumber() != -1 )
{
cout << setw( 7 ) << "Record#" << " " << left
<< setw( 30 ) << "Tool name" << left
<< setw( 13 ) << "Quantity" << setw( 10 ) << "Cost" << endl;
cout << fixed << showpoint;
cout << setw( 7 ) << temp.getPartNumber() << " "
<< left << setw( 30 ) << temp.getToolName()
<< left << setw( 13 ) << temp.getInStock()
<< setprecision( 2 ) << setw( 10 ) << temp.getUnitPrice() << '\n'
<< "Enter the tool name: "; // ask user for new name
cin.ignore(); // ignore the newline on the input stream
cin.get( name, LENGTH ); // set new name
temp.setToolName( name );
cout << "Enter quantity and price: "; // ask for price and quantity
cin >> stock >> price;
temp.setInStock( stock ); // set new quantity
temp.setUnitPrice( price ); // get new price
// set file position pointer and write information to file
fRef.seekp( ( temp.getPartNumber() ) * sizeof( Tool ) );
fRef.write( reinterpret_cast< char * > ( &temp ), sizeof( Tool ) );
else
cerr << "Cannot update. The record is empty.\n";
} // end if
} // end function updateRecord
// function to insert a new record
void insertRecord( fstream & )
{
Tool temp;
int part;
char name[ LENGTH ];
int stock;
double price;
// ask user for part number
cout << "Enter the part number for insertion: ";
cin >> part;
// set file position pointer and read data from file
fRef.seekg( ( part ) * sizeof( Tool ) );
fRef.read( reinterpret_cast< char * > ( &temp ), sizeof( Tool ) );
// as long as record is empty get information from user
if ( temp.getPartNumber() == -1 )
{
temp.setPartNumber( part ); // set partNumber
cout << "Enter the tool name: "; // ask user for tool name
cin.ignore(); // ignore the newline on the input stream
cin.get( name, LENGTH );
temp.setToolName( name ); // set toolName
// ask user for new quantity and price
cout << "Enter quantity and price: ";
cin >> stock >> price;
temp.setInStock( stock ); // set quantity
temp.setUnitPrice( price ); // set price
// set file position pointer and write information to file
fRef.seekp( ( temp.getPartNumber() ) * sizeof( Tool ) );
fRef.write( reinterpret_cast< char * >( &temp ), sizeof( Tool ) );
else
cerr << "Cannot insert. The record contains information.\n";
} // end if
} // end function insertRecord
// function to delete a record
void deleteRecord( fstream & )
{
Tool blankItem;
Tool temp;
int part;
// get tool user wants to delete
cout << "Enter the part number for deletion: ";
cin >> part;
// set file position pointer and read information from file
fRef.seekg( part * sizeof( Tool ) );
fRef.read( reinterpret_cast< char * >( &temp ), sizeof( Tool ) );
// if record contains data, set record to an empty Tool object
if ( temp.getPartNumber() != -1 )
{
fRef.seekp( part * sizeof( Tool ) );
fRef.write( reinterpret_cast< char * >( &blankItem ), sizeof( Tool ) );
cout << "Record deleted.\n";
else
cerr << "Cannot delete. The record is empty.\n";
} // end if
} // end function deleteRecord
Lab5_1.cpp file also Driver File
// Lab5_1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::ios;
#include <iomanip>
using std::fixed;
using std::left;
using std::setprecision;
using std::setw;
using std::showpoint;
#include <fstream>
using std::fstream;
#include <cctype>
#include <cstdlib>
using std::exit;
int main()
{
int choice;
char response;
// file stream used for input and output
fstream file( "hardware.dat", ios::in | ios::out );
void ( *f[] )( fstream & ) = { listTools, updateRecord, insertRecord, deleteRecord };
// terminate program if file cannot be opened
if ( !file )
{
cerr << "File could not be opened.\n";
exit( 1 );
} // end if
// ask user if new file should be made
cout << "Should the file be initialized (Y or N): ";
cin >> response;
response = ( response );
// test if user's response was valid
while ( ( response != 'Y' ) && ( response != 'N' ) )
{
cout << "Invalid response. Enter Y or N: ";
cin >> response;
response = ( response );
} // end while
// initialize file if user says to
if ( response == 'Y' )
{
initializeFile( file );
inputData( file );
} // end if
// perform user instructions until 5 is entered
while ( ( choice = instructions() ) != 5 )
{
( *f[ choice - 1 ] )( file );
file.clear(); // reset eof indicator
} // end while
file.close(); // close input/output file
return 0;
} // end main
I get these errors
Error 1 error C2059: syntax error : ')' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.h 9
Error 2 error C2447: '{' : missing function header (old-style formal list?) c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.h 10
Error 3 error C2065: 'fstream' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 33
Error 4 error C2059: syntax error : ')' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 33
Error 5 error C2143: syntax error : missing ';' before '{' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 34
Error 6 error C2447: '{' : missing function header (old-style formal list?) c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 34
Error 7 error C2059: syntax error : ')' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 43
Error 8 error C2143: syntax error : missing ';' before '{' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 44
Error 9 error C2447: '{' : missing function header (old-style formal list?) c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 44
Error 10 error C2065: 'cout' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 90
Error 11 error C2065: 'cin' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 98
Error 12 error C2059: syntax error : ')' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 107
Error 13 error C2143: syntax error : missing ';' before '{' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 108
Error 14 error C2447: '{' : missing function header (old-style formal list?) c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 108
Error 15 error C2059: syntax error : ')' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 136
Error 16 error C2143: syntax error : missing ';' before '{' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 137
Error 17 error C2447: '{' : missing function header (old-style formal list?) c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 137
Error 18 error C2059: syntax error : ')' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 190
Error 19 error C2143: syntax error : missing ';' before '{' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 191
Error 20 error C2447: '{' : missing function header (old-style formal list?) c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 191
Error 21 error C2059: syntax error : ')' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 233
Error 22 error C2143: syntax error : missing ';' before '{' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 234
Error 23 error C2447: '{' : missing function header (old-style formal list?) c:\documents and settings\09459393\desktop\lab5_1\lab5_1\ex17_12.cpp 234
Error 24 error C2065: 'listTools' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\lab5_1.cpp 34
Error 25 error C2065: 'updateRecord' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\lab5_1.cpp 34
Error 26 error C2065: 'insertRecord' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\lab5_1.cpp 34
Error 27 error C2065: 'deleteRecord' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\lab5_1.cpp 34
Error 28 error C3861: 'initializeFile': identifier not found c:\documents and settings\09459393\desktop\lab5_1\lab5_1\lab5_1.cpp 59
Error 29 error C3861: 'inputData': identifier not found c:\documents and settings\09459393\desktop\lab5_1\lab5_1\lab5_1.cpp 60
Error 30 error C3861: 'instructions': identifier not found c:\documents and settings\09459393\desktop\lab5_1\lab5_1\lab5_1.cpp 64
Error 31 fatal error C1903: unable to recover from previous error(s); stopping compilation c:\documents and settings\09459393\desktop\lab5_1\lab5_1\lab5_1.cpp 64
Error 32 error C2059: syntax error : ')' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.h 9
Error 33 error C2447: '{' : missing function header (old-style formal list?) c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.h 10
Error 34 error C2027: use of undefined type 'Tool' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 13
Error 35 error C2660: 'setPartNumber' : function does not take 0 arguments c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 16
Error 36 error C2027: use of undefined type 'Tool' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 18
Error 37 error C2270: 'getPartNumber' : modifiers not allowed on nonmember functions c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 19
Error 38 error C2065: 'partNumber' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 20
Error 39 error C2027: use of undefined type 'Tool' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 22
Warning 40 warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 26
Error 41 error C2065: 'toolName' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 28
Error 42 error C2027: use of undefined type 'Tool' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 34
Error 43 error C2562: 'getToolName' : 'void' function returning a value c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 36
Error 44 error C2027: use of undefined type 'Tool' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 38
Error 45 error C2440: '=' : cannot convert from 'void (__cdecl *)(int)' to 'int' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 40
Error 46 error C2027: use of undefined type 'Tool' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 42
Error 47 error C2270: 'getInStock' : modifiers not allowed on nonmember functions c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 43
Error 48 error C2065: 'inStock' : undeclared identifier c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 44
Error 49 error C2027: use of undefined type 'Tool' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 46
Error 50 error C2440: '=' : cannot convert from 'void (__cdecl *)(double)' to 'double' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 48
Error 51 error C2027: use of undefined type 'Tool' c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 50
Error 52 error C2562: 'getUnitPrice' : 'void' function returning a value c:\documents and settings\09459393\desktop\lab5_1\lab5_1\tool.cpp 52