1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
//def of struct
#ifndef TOOLINFO_H
#define TOOLINFO_H
struct tool_info {
int recordnum;
char tool[15];
int qty;
double cost;
};
#endif
//tools.cpp
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
#include <cstdlib>
int choice;
char sym[5];
void displays_menu(void){
cout <<" MENU" << endl;
cout <<"_________________________________________________________________________"<< endl;
cout <<"[1] List All" << endl;
cout <<"[2] Delete a Record" << endl;
cout <<"[3] Update a Record" << endl;
cout <<"[4] Add a Record" << endl;
cout <<"[5] QUIT" << endl;
cout <<"_________________________________________________________________________\n" << endl;
}
int get_choice(){
int choice;
cout<<"\nYour choice:" << endl;
cin >> choice;
cout <<"\n";
return choice;
}
tool_info tools;
void outputLine(ostream &output, const tool_info &tools)
{
output << setiosflags(ios::left) << setw(12) << tools.recordnum
<< setw(18) << tools.tool << setw(8) << tools.qty << setw(6) << setprecision(2)
<< resetiosflags(ios::left)
<< tools.cost << '\n';
}
void list_all(){
ifstream inHardwareFile("hardware.dat", ios::in);
if(!inHardwareFile){
cerr << "File could not be opened!" << endl;
exit(1);
}
cout << setiosflags(ios::left) << setw(12) << "Record #:"
<< setw(18) << "Tool name:" << setw(8) << "Qty:" << setw(6) << "Cost:\n"
<< setiosflags(ios::fixed | ios::showpoint);
while(inHardwareFile >> tools.recordnum >> tools.tool >> tools.qty >> tools.cost){
outputLine(cout, tools);
}
}
void delete_line(){
ofstream outHardwareFile("hardware.dat", ios::binary);
if(!outHardwareFile){
cerr << "File could not be opened!" << endl;
exit(1);
}
//int search_num;
tool_info blankRecord = { 0, "", 0,0.0 };
cout << "Give the record # of the one you want to delete:" << endl;
cin >> tools.recordnum;
outHardwareFile.seekp((tools.recordnum -1)* sizeof(tool_info));
outHardwareFile.write(reinterpret_cast<const char *>(&blankRecord),sizeof(tool_info));
}
void update(){
ofstream outHardwareFile("hardware.dat", ios::binary);
if(!outHardwareFile){
cerr << "File could not be opened!" << endl;
exit(1);
}
tool_info blankRecord = { 0, "", 0,0.0 };
cout << "Give the record # of the one you want to update:" << endl;
cin >> tools.recordnum;
outHardwareFile.seekp((tools.recordnum -1)* sizeof(tool_info));
outHardwareFile.write(reinterpret_cast<const char *>(&blankRecord),sizeof(tool_info));
cout << "Enter record number:";
cin >> tools.recordnum;
cout << "Enter tool name:";
cin >> tools.tool;
cout << "Enter quantity:";
cin >> tools.qty;
cout << "Enter cost:";
cin >> tools.cost;
cout << "Successfully updated a record!\n" << endl;
}
void add(){
ofstream outHardwareFile("hardware.dat", ios::app);
if(!outHardwareFile){
cerr << "File could not be opened!" << endl;
exit(1);
}
cout << "Enter record number:";
cin >> tools.recordnum;
cout << "Enter tool name:";
cin >> tools.tool;
cout << "Enter quantity:";
cin >> tools.qty;
cout << "Enter cost:";
cin >> tools.cost;
cout << "Successfully added a record!\n" << endl;
outHardwareFile << tools.recordnum << "\t" << tools.tool << "\t" << tools.qty << "\t" << tools.cost << "\n";
}
int main(){
do{
displays_menu();
choice = get_choice();
switch(choice){
case 1: list_all();
break;
case 2: delete_line();
break;
case 3: update();
break;
case 4: add();
break;
case 5: break;
default: cout << "\nThere's no such choice in the menu!" << endl;
}
if(choice!=5){
cout <<"\nPress any key to continue..." << endl;
cin >> sym;
}
} while (choice !=5);
return 0;
}
| |