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 167 168
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;
struct data
{
int Inventory_Num;
char description[150];
};
void menu (ifstream&, ofstream&, data Inventory[], int size, string& file);
void load (ifstream&, data Inventory[], int size, string& file);
void add (ofstream&, data Inventory[], int size, string& file);
int search (data Inventory[], int size, int spot);
void print (ofstream& Out);
void list (ofstream&, data Inventory[], int size);
int main()
{
// Acquires number of inventory to set up array
int amount;
cout << "How many inventory items are on the file?: ";
cin >> amount;
// Sets up array based on inputted number of inventory
data *other;
other = new data[amount];
string file;
ifstream toscreen;
ofstream tofile(file.c_str());
menu(toscreen, tofile, other, amount, file);
toscreen.close();
tofile.close();
delete other;
system("PAUSE");
return 0;
}
void menu(ifstream& In, ofstream& Out, data Inventory[], int size, string& file)
{
int choice = 0, number = 0, spot = 0;
while (choice != 0){
cout << endl;
cout << "To use the menu, please enter one of the numbers: " << endl << endl;
cout << "1) Load" << endl;
cout << "2) Add" << endl;
cout << "3) Search" << endl;
cout << "4) List" << endl;
cout << "5) Save" << endl << endl;
cout << "0) Exit" << endl << endl;
cout << "Choose a menu option: ";
cin >> choice;
switch(choice)
{
// Loads a file's inventory
case 1:
system("cls");
load(In, Inventory, size, file);
cout << "Press Enter to return to the menu." << endl;
cin.get();cin.get();
break;
// Adds to the inventory
case 2:
system("cls");
add(Out, Inventory, size, file);
cout << "Press Enter to return to the menu." << endl;
cin.get();cin.get();
break;
// Conducts an inventory search
case 3:
system("cls");
spot = search (Inventory, size, spot);
if (spot < 0)
{
cout << "Sorry, that item is not on the list.";
}
cout << "Press Enter to return to the menu." << endl;
cin.get();cin.get();
break;
// Lists out the inventory
case 4:
system ("cls");
print(Out);
list(Out, Inventory, size);
cout << "Press Enter to return to the menu." << endl;
cin.get();cin.get();
break;
}
}
}
// Loads file
void load (ifstream& In, data Inventory[], int size, string& file)
{
In.open(file.c_str());
if (In.fail())
{
cout << "File not found." << endl;
}
else
{
cout << "" << file << " has loaded successfully!" << endl;
}
int i;
for (i = 0; i < size && !In.eof(); i++)
{
In >> Inventory[i].Inventory_Num;
In.get(Inventory[i].description, 100);
In.ignore (10, '\n');
}
}
void add (ofstream& Out, data Inventory[], int size, string& file)
{
int Inventory_Num = 0;
char description[150];
Out.open(file.c_str(), ios::app);
cout << "Enter the inventory number:" << endl;;
cin >> Inventory_Num;
Out << endl << Inventory_Num;
cout << "Enter the inventory description:" << endl;
cin >> description;
Out << " " << description << endl;
}
int search (data Inventory[], int size, int spot)
{
int number = 0;
cout << "Please enter the inventory number of the item you're looking for." << endl;
cin >> number;
for (int i = 0; i < size; i++)
{
if (Inventory[i].Inventory_Num == number)
{
cout << endl << "Inventory number: " << Inventory[i].Inventory_Num << "" << endl;;
cout << "" << Inventory[i].description << "" << endl;
}
}
}
void print (ofstream& Out)
{
cout << endl << "Item Number" << '\t';
cout << setw(25) << "Item Description" << endl;
cout << "---------------------------------------------" << endl;
cout << endl;
}
void list(ofstream& Out, data Inventory[], int size)
{
for (int i = 0; i < size; i++)
{
cout << setw(6) << Inventory[i].Inventory_Num << '\t';
cout << setw(20) << Inventory[i].description;
cout << endl;
}
}
|