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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
class Entry {
string name, quantity, notes;
};
Entry entryList[100];
int rec_num = 0;
int num_entries;
string toUpper (string S) {
for (int i= 0; i<S.length(); i++)
S[i] = toupper(S[i]);
return S;
}
void ReadFile () {
string S, eol;
fstream F("Inventory.txt");
while (!F.eof() && !F.fail()){
F >> entryList[rec_num].name
>> entryList[rec_num].quantity;
getline(F, eol); //reads the end-of-line marker
getline(F, S);
entryList[rec_num].notes = S;
rec_num++;
}
cout << "Inventory read." << endl;
num_entries = rec_num;
F.close();
return;
}
void StoreFile () {
fstream F("Inventory.txt");
rec_num = 0;
while (rec_num < num_entries){
F << entryList[rec_num].name << " "
<< entryList [rec_num].quantity << " "
<< entryList [rec_num].notes << " " << endl;
rec_num++;
}
cout << "Inventory stored." << endl;
return;
}
void add_item(string name, string quantity, string notes){
entryList [num_entries].name = name;
entryList [num_entries].quantity = quantity;
entryList [num_entries].notes = notes;
num_entries++;
return;
}
void retrieve_item(string name){
for (int i = 0; i < num_entries; i++){
if (toUpper(entryList [i].name) == toUpper(name)) {
cout << "Quantity: " << entryList [i].quantity << endl
<< "Notes: " << entryList [i].notes << endl;
return;
}
}
cout << "Item not found" << endl;
return;
}
void listAllItems() {
int i = 0;
while (i < num_entries) {
cout << "-- " << entryList [i].name << " "
<< entryList [i].quantity << endl
<< "-- " << entryList [i].notes << endl << endl;
i++;
}
}
int main(){
string name, quantity, notes;
char command;
ReadFile ();
cout << "Use \"e\" for enter, \"f\" for find, \"l\" for list, \"q\" to quit."
<< endl << "Command: ";
cin >> command;
while (command != 'q'){
switch (command){
case 'e': cin >> name; cout << "Enter Quantity: ";
cin >> quantity; cout << "Enter Notes: ";
cin.ignore(); getline(cin, notes);
add_item(name, quantity, notes); break;
case 'f': cin >> name; retrieve_item(name); break;
case 'l': listAllItems(); break;
}
cout << "\nCommand: "; cin >> command;
}
StoreFile();
cout << "All set !";
return 0;
}
|