Bag  0.91
BagDriver.cpp
Go to the documentation of this file.
1 
11 //-----------------------
12 // C++ includes
13 //-----------------------
14 #include <iostream>
15 #include <fstream>
16 #include <string>
17 #include <vector>
18 
19 //-----------------------
20 // Application includes
21 //-----------------------
22 #include "ArrayBag.h"
23 #include "io_functions.h"
24 
25 //-----------------------
26 // Using statements
27 //-----------------------
28 using namespace std;
29 
30 //----------------------
31 // Functions Prototypes
32 //----------------------
33 int getBagNum(); //prompts user for bag number
34 void processOption(char& c, ArrayBag<string> bags[]); //processes menu input
35 void displayBag(ArrayBag<string>& bag); //display the bag's entire contents
36 void restoreBag(ArrayBag<string>& bag); //prompts for filename with items
37 void saveBag(ArrayBag<string>& bag); //prompts for filename to store items in
38 
42 int main()
43 {
44  int bagNum; //The bag number to use
45  ArrayBag<string> bag[3];//bags to test, bag 0 used for intersection/union
46  string restoreBagFlg; // holds response to restore bag from a file
47  string menuOption; // holds menu option input from user
48 
49 //Opening Message
50  cout<< "Bag Driver - Tests the Bag class using two bags" << endl;
51 
52 //Restore the bag from a file
53  cout<< "----------------------------------" << endl;
54  cout<< "Would you like to restore a bag from a file? (Y/N) ";
55 
56 //Read user input - use a string to accept all inputs
57  getline(cin, restoreBagFlg);
58  if(restoreBagFlg[0] == 'Y' || restoreBagFlg[0] == 'y'){
59  bagNum = getBagNum();
60  restoreBag(bag[bagNum]); // opens/reads file into aBag
61  }
62 
63 //Menu driven part of main program
64 //Initial menu display and loop to process menu option
65  printMenu();
66  do {
67  cout << "\nEnter menu selection (0 for menu): ";
68  getline(cin, menuOption); //Use a string in case user enters chars
69  processOption(menuOption[0], bag);
70  }while(menuOption[0] != '9');
71 
72  return 0; //Exit program
73 }//main()
74 
75 
76 //---------------------------
77 // Local function definitions
78 //---------------------------
79 
85 int getBagNum()
86 {
87  int bagNum=0;
88  while(bagNum != 1 && bagNum != 2)
89  {
90  cout << "Which bag (1 or 2)? ";
91  bagNum = getInt();
92  }
93  return bagNum;
94 } //getBagNum()
95 
102 {
103  string item;
104  vector<string> bagVector = bag.toVector();
105 
106  if(bagVector.size() > 0)
107  {
108  cout << "The bag contains: " << endl;
109  for(unsigned int i=0; i<bagVector.size(); i++){
110  cout << bagVector[i] << ", ";
111  }
112  cout<<endl;
113  }
114  else
115  {
116  cout << "The bag is empty." << endl;
117  }
118 }
119 
120 
126 void processOption(char &menuChar, ArrayBag<string> bag[])
127 {
128  int bagNum;
129  string item;
130  string saveBagFlg;
131 
132  switch (menuChar)
133  {
134  case '0': // Display menu options
135  printMenu();
136  break;
137 
138  case '1': // Output size of the bag
139  cout << "\nBag Size is: " << bag[getBagNum()].getCurrentSize() << endl;
140  break;
141 
142  case '2': // Add items to the bag
143  cout << "Add Item:" << endl;
144  item = getItem();
145  bagNum = getBagNum();
146  if(bag[bagNum].add(item))
147  {
148  cout << item << " was successfully added to bag " << bagNum << endl;
149  }
150  else
151  {
152  cout << item << " was not added to the bag." << endl;
153  }
154  break;
155 
156 //NOTE that options 3 - 8 are left for the student to complete
157 
158  case '8': // Display entire contents of bag
159  displayBag(bag[getBagNum()]);
160  break;
161 
162  case '9': // Exit the program
163  case 'Q': // handles multiple quit inputs
164  case 'q':
165  cout<< "Would you like to save the bag to a file? (Y/N) ";
166  getline(cin, saveBagFlg);
167  if(saveBagFlg[0] == 'Y' || saveBagFlg[0] == 'y')
168  saveBag(bag[getBagNum()]); // save bag to file
169  cout<< "Goodbye!"<< endl; // Exit message
170  menuChar = '9';
171  break;
172  default: // Invalid menu option entered
173  cout << "\nError! Invalid option. Please try again.\n\n";
174  printMenu();
175  }
176 }//processOption()
177 
189 {
190  bool success = true;
191  ifstream fin;
192  string filename;
193  string item;
194 
195  //Data validation to get filename and open it
196  do{
197  cout<<"Enter the filename that contains the bag: ";
198  getline(cin, filename);
199  fin.clear();
200  fin.open(filename.c_str());
201  if(fin == 0){
202  cout<<"ERROR - could not open file " << filename<<endl;
203  }
204  } while (!fin);
205 
206  //read file until eof reached
207  while(success && getline(fin,item)){
208  success = bag.add(item);
209  }
210  if (!success) cout << "Not all items added to the bag!" << endl;
211  cout<< bag.getCurrentSize() << " items read into the bag\n\n";
212  fin.close();
213 }//restoreBag()
214 
220 {
221  //NOTE this is a function stub. You need to implement it.
222  cout << "Bag will be saved when implemented" << endl;
223 }//saveBag()
224