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 tool identification number should be the record
number. Use the following information to start your file:
Record# Tool name Quantity Cost
3 Electric sander 7 57.98
17 Hammer 76 11.99
24 Jig saw 21 11.00
39 Lawn mower 3 79.50
56 Power saw 18 99.99
68 Screwdriver 106 6.99
77 Sledge hammer 11 21.50
83 Wrench 34 7.50
problem i am having is when i go to enter the items listed above, the first entry stores correctly, but each consecutive entry gets fubared. Some please take a look at my code for me and tell me whats going on. Brain is fried and only have a day left to get this done.
thx in advance
#include <iostream>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
using namespace std;
typedef struct _tooldata
{
int toolNum;
char toolName[128];
int quantity;
double cost;
} toolData;
void textFile(fstream &readFromFile);
int enterChoice();
void updateRecord(fstream &updateFile);
void newRecord(fstream &insertInFile);
void deleteRecord(fstream &deleteFromFile);
void outputLine(ostream &output, toolData tool);
void gotoxy(int x, int y)
{
COORD ord;
ord.X = x;
ord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), ord);
}
void clrscr()
{
system("CLS");
}
static char FILENAME[] = "tools.dat";
void CreateDataFile()
{
fstream testFileExists(FILENAME, ios::in | ios::out);
if (!testFileExists)
{
ofstream outtools(FILENAME);
toolData *data = new toolData();
strcpy(data->toolName, "");
data->quantity = 0;
data->cost = 0.0;
data->toolNum = 0;
for(int i = 0; i < 100; i++)
outtools.write((char*)data, sizeof(*data));
outtools.close();
}
else
testFileExists.close();
}
int main()
{
CreateDataFile();
fstream inOuttools(FILENAME, ios::in | ios::out);
inOuttools.seekg(0,ios::end);
long location = inOuttools.tellg();
cout << "Size of hardware.dat = " << location;
int choice;
while ( ( choice = enterChoice() ) != 5 ) {
switch (choice)
{
case 1: textFile(inOuttools); break;
case 2: updateRecord(inOuttools); break;
case 3: newRecord(inOuttools); break;
case 4: deleteRecord(inOuttools); break;
default: cerr << "Incorrect choice" << endl; break;
}
inOuttools.clear(); // resets end-of-file indicator
}
clrscr();
return 0;
}
// Prompt for and input menu choice
int enterChoice(void)
{
cout << endl << "Enter your choice" << endl
<< "1. stored a formatted text file of accounts" << endl
<< " called hardware.dat for printing" << endl
<< "2. update inventory" << endl
<< "3. add a new item" << endl
<< "4. delete an item" << endl
<< "5. end program" << endl << "? ";
int menuChoice;
cin >> menuChoice;
return menuChoice;
}
// Create a random access data file called <tools.dat>
void makeDirectAccessFile(fstream &inOuttools)
{
toolData blanktool = {0, "", 0, 0.0};
for (int i = 1; i <= 100; i++)
inOuttools.write((char *)&blanktool, sizeof(toolData));
cout << endl << endl
<< "A direct access file called <hardware.dat> has been " << endl
<< "created with four fields initialized as follows:"
<< endl << endl
<< " toolNum = 0" << endl
<< " toolName[15] = \"\"" << endl
<< " long quantity = \"\"" << endl
<< " cost = 0.0" << endl;
cout << endl << "Press any key to return to the menu." << endl << endl;
getch();
}
void textFile(fstream &readFromFile)
{
ofstream outPrintFile("print.txt", ios::out);
if (!outPrintFile) {
cerr << "File could not be opened." << endl;
exit(1);
}
clrscr();
cout << setiosflags(ios::left) << setw(6) << "Tool"
<< setw(16) << "Tool Name" << setw(11) << "Quantity"
<< setiosflags(ios::right) << setw(10) << "Cost" << endl;
outPrintFile << setiosflags(ios::left) << setw(6) << "Tool"
<< setw(16) << "Tool Name" << setw(11) << "Quantity"
<< setiosflags(ios::right) << setw(10) << "Cost" << endl;
readFromFile.seekg(0);
toolData tool;
readFromFile.read((char *)&tool, sizeof(toolData));
while (!readFromFile.eof()) {
if (tool.toolNum != 0) {
outputLine(outPrintFile, tool);
outputLine(cout, tool);
}
readFromFile.read((char *)&tool, sizeof(toolData));
}
}
void updateRecord(fstream &updateFile)
{
int account;
clrscr();
do {
cout << "Enter inventory number to update (1 - 100): ";
cin >> account;
} while (account < 1 || account > 100);
updateFile.seekg((account - 1) * sizeof(toolData));
toolData tool;
updateFile.read((char *)&tool, sizeof(toolData));
if (tool.toolNum != 0) {
outputLine(cout, tool);
cout << endl << "Enter charge (+) or payment (-): ";
float transaction;
cin >> transaction;
tool.cost += transaction;
outputLine(cout, tool);
updateFile.seekp((account - 1) * sizeof(toolData));
updateFile.write((char *)&tool, sizeof(toolData));
}
else
cerr << "Acount #" << account << " has no information." << endl;
}
void newRecord(fstream &insertInFile)
{
char text[80];
clrscr();
cout << "Enter new item number (1 - 100): ";
int account;
cin >> account;
insertInFile.seekg((account - 1) * sizeof(toolData));
toolData tool;
insertInFile.read((char *)&tool, sizeof(toolData));
if (tool.toolNum == 0)
{
cout << "Enter the tool name and press <ENTER>: ";
cin.getline(text,80);
cin.getline(text,80);
if ( strlen(text) > 14)
{
strncpy (tool.toolName, text, 14);
tool.toolName[14] = '\0';
cout << "Name is too long. It has been shortened to "
<< tool.toolName << "." << endl;
}
else
{
strcpy(tool.toolName, text);
tool.toolName[14] = '\0';
}
cout << "Enter the quantity and press <ENTER>: ";
cin >> tool.quantity;
cout << "Enter the cost as a decimal value and press <ENTER>: ";
cin >> tool.cost;
tool.toolNum = account;
insertInFile.seekp((account - 1) * sizeof(toolData));
insertInFile.write((char *)&tool, sizeof(toolData));
}
else
cerr << "Account #" << account
<< " already contains information." << endl;
}
void deleteRecord(fstream &deleteFromFile)
{
clrscr();
cout << "Enter account number to delete (1 - 100): ";
int account;
cin >> account;
deleteFromFile.seekg((account - 1) * sizeof(toolData));
toolData tool;
deleteFromFile.read((char *)&tool, sizeof(toolData));
if (tool.toolNum != 0) {
toolData blanktool = {0, "", 0, 0};
deleteFromFile.seekp((account - 1) * sizeof(toolData));
deleteFromFile.write((char *)&blanktool, sizeof(toolData));
cout << setiosflags(ios::left) << setw(6) << "tool"
<< setw(16) << "Last Name" << setw(11) << "First Name"
<< setiosflags(ios::right) << setw(10) << "cost" << endl;
outputLine(cout, tool);
cout << "Account #" << account << " deleted." << endl;
}
else
cout << "Account #" << account << " is empty." << endl;
}
void outputLine(ostream &output, toolData tool)
{
output << setiosflags(ios::left) << setw(6) << tool.toolNum
<< setw(16) << tool.toolName << setw(11) << tool.quantity
<< setiosflags(ios::fixed | ios::showpoint | ios::right)
<< setw(10) << setprecision(2) << tool.cost << endl;
}