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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
//#include <cstdlib>
#include <limits>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int /*void*/ menuSelection(/*int &selection, vector<string> &Names*/ int inventory_size );
bool /*void*/ performSelection(int /*&*/selection, /*string &file,*/ /*string &filename,*/ /*int &numberInput, string &nameInput,*/ vector<string> &Names, vector<int> &Numbers);
void listitems(vector<string> &Names, vector<int> &Numbers);
void additem(/*int &numberInput, string &nameInput,*/ vector<string> &Names, vector<int> &Numbers);
bool /*void*/ loadfile(/*string &filename,*/ vector<int> &Numbers, vector<string> &Names);
void /*int*/ searchitems(vector<int> &Numbers, vector<string> &Names);
void saveinventory(/*string &file,*/ vector<int> &Numbers, vector<string> &Names);
int main(int argc, char *argv[])
{
int selection;
//unused int n;
// string file;
// string filename;
// int numberInput;
// string nameInput;
vector<string> Names; // set to 15???
vector<int> Numbers;
//unused int i;
/* This is not efficient. See the following notes...
do
{
menuSelection(selection , Names );
performSelection(selection, file, filename, numberInput, nameInput, Names, Numbers);
} while (selection !=0);
*/
do {
selection = menuSelection( Names.size() );
}
while (performSelection( selection, Names, Numbers));
// system("PAUSE");
cout << "Press ENTER to quit." << flush;
cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
// return EXIT_SUCCESS;
return 0;
}
int readInteger()
{
int result;
do {
cout << "> " << flush;
// Read a line (terminated by ENTER) from the user
string s;
getline( cin, s );
// Get rid of any trailing whitespace
s.erase( s.find_last_not_of( " \f\n\r\t\v" ) + 1 );
// Turn it into an integer
istringstream ss( s );
ss >> result;
// Check to see that all is well
if (ss.eof()) break;
// Otherwise, instruct the user again
cout << "Please enter an INTEGER number";
} while (true);
return result;
}
int menuSelection( int inventory_size )
{
cout << endl;
cout << "1. Load Inventory" << endl;
cout << "2. Add Item" << endl;
cout << "3. Search for Item" << endl;
cout << "4. List Items" << endl;
cout << "5. Save Inventory" << endl;
cout << "6. End Program" << endl;
cout << "Number of Items in Inventory: " << inventory_size << endl;
cout << endl;
cout << "Enter the number of the action you would like to do: " << endl;
// cin >> selection;
// cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
return readInteger();
}
bool /*void*/ performSelection(int /*&*/selection, /*string &file,*/ /*string &filename,*/ /*int &numberInput, string &nameInput,*/ vector<string> &Names, vector<int> &Numbers)
{
switch (selection)
{
case 1:
return loadfile(/*filename,*/ Numbers, Names);
case 2:
additem(/*numberInput, nameInput,*/ Names, Numbers);
break;
case 3:
searchitems(Numbers, Names);
break;
case 4:
listitems(Names, Numbers);
break;
case 5:
saveinventory(/*file,*/ Numbers, Names);
break;
case 6:
// system ("PAUSE");
// exit (1);
return false;
}
return true;
}
bool /*void*/ loadfile(/*string &filename,*/ vector<int> &Numbers, vector<string> &Names)
{
string filename;
//not needed int i=0 ;
//moved & renamed int temp;
//moved & renamed string sub;
ifstream inputFile;
string line;
cout << "Enter the filename where the inventory is stored: " << endl;
// cin >> filename;
getline( cin, filename );
inputFile.open(filename.c_str());
if (inputFile.fail())
{
cerr << "File opening failed.\n";
// system ("PAUSE");
// exit (1);
return false;
}
else
{
cout << "The file opened" << endl;
}
while (inputFile)
{
int item_number;
inputFile >> item_number;
inputFile.ignore( numeric_limits <streamsize> ::max(), '\n' );
if (!inputFile) break;
string item_name;
getline(inputFile, item_name);
if (!inputFile) break;
Numbers.push_back(item_number);
Names.push_back(item_name);
//not needed i++;
}
return true;
}
void additem(/*int &numberInput, string &nameInput,*/ vector<string> &Names, vector<int> &Numbers)
{
int numberInput;
string nameInput;
//unused int i;
//moved int n;
cout << "Enter the number of the item: ";
// cin >> numberInput;
// cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
numberInput = readInteger();
Numbers/*.insert(Numbers.end(), 1, numberInput);*/.push_back(numberInput); // use the simplest method
cout << "Enter the name of the item: ";
// cin >> nameInput;
getline( cin, nameInput );
Names/*.insert(Names.end(), 1, nameInput);*/.push_back(nameInput);
//insertion sort
int j, val;
for(unsigned int i = 1; i < Numbers.size(); i++)
{
val = Numbers[i];
j = i - 1;
while(j >= 0 && Numbers[j] > val)
{
Numbers[j + 1] = Numbers[j];
std::swap(Names[j], Names[j+1]); //maintains association between vectors
j = j - 1;
}
Numbers[j + 1] = val;
}
cout << "Your sorted inventory reads: ";
for(unsigned n = 0 ; n < Numbers.size() ; n++ )
{
cout << Numbers[n]<< " " << Names[n] << endl;
}
}
void listitems(vector<string> &Names, vector<int> &Numbers)
{
//moved int k;
cout << "The items in the inventory are: " << endl;
for (unsigned k = 0 ; k < Numbers.size() ; k++ )
{
cout << Numbers[k] << " " << Names[k] << endl;
}
}
void /*int*/ searchitems(vector<int> &Numbers, vector<string> &Names)
{
int searchitem;
int middle;
cout << "Enter the item number you'd like to search for: ";
// cin >> searchitem;
// cin.ignore( numeric_limits <streamsize> ::max(), '\n' );
searchitem = readInteger();
//binary search
int lo = 0;
int hi = (Numbers.size() -1);
while(lo <= hi)
{
middle = (lo + hi) / 2;
if ( Numbers[middle] == searchitem)
{
cout << "The item you searched for is: Item #: "
<< Numbers[middle] << " Item Name: " << Names[middle]<< endl;
break;
}
if (Numbers[middle] < searchitem)
{
lo = middle +1;
}
if (Numbers[middle] > searchitem)
{
hi = middle -1;
}
if((Numbers[middle] != searchitem) && (lo>hi))
{
cout << "Item not found" << endl;
cout << endl;
break;
}
}
}
void saveinventory(/*string &file,*/ vector<int> &Numbers, vector<string> &Names)
{
string file; // I would have named it 'filename'
//moved int i;
cout << "Enter what you would like the File Name to be: ";
// cin >> file;
getline( cin, file );
ofstream outputFile;
outputFile.open(file.c_str());
for (unsigned i = 0 ; i < Numbers.size() ; i++)
{
outputFile << Numbers[i] << endl;
outputFile << Names[i] << endl;
}
outputFile.close();
}
|