C++ Assignment Help! Due tmrw

Hey was working on this assignment due tmrw. Need to create a search function which i have no idea on how to do this. Also was trying to make it so it could open two txt files and display both on different commands.

Here is my code.
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
281
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

// define a data structure
struct InventoryRecord
{
  string name;   // the name of this inventory item
  int qty;
  int year;
  int sku;    // the number in stock
  double cost;   // the cost
};

// const for the max size of the record array
const int MAX_SIZE = 100;

// function prototypes
void addData(InventoryRecord list[], int& size);
void dispData(const InventoryRecord list[], int size);
void saveFile(const InventoryRecord list[], int size);
void openFile(InventoryRecord list[], int& size);
void openStore(InventoryRecord list[], int& size);
char getMenuResponse();


int main(int argc, char *argv[])
{
  InventoryRecord recList[MAX_SIZE];
  int numOfRecs = 0;
  bool run = true;
  do
  {
    cout << "Inventory Program - " << numOfRecs << " items in stock" << endl;
  	switch ( getMenuResponse() ) 
  	{
  		case 'A': addData(recList, numOfRecs); break;
  		case 'D': dispData(recList, numOfRecs); break;
  		case 'O': openFile(recList, numOfRecs); break;
  		case 'S': saveFile(recList, numOfRecs); break;
  		case 'L': openStore(recList, numOfRecs); break;
  		case 'Q': run = false; break;
  		default : cout << "That is NOT a valid choice" << endl;
  	}
  } while (run);
  cout << endl << "Program Terminated" << endl;
  
  // system("PAUSE"); // Program exits immediatly upon "Quit" if commented out
  return EXIT_SUCCESS;
}

// Task:     Allow data entry of one inventory item
// Accepts:  References to the inventory array and its size
// Returns:  Nothing
// Modifies: The array and size 'actual parameter'
// NOTE:     Could be modified to allow entry of more than one item
void addData(InventoryRecord list[], int& size)
{
  InventoryRecord tmp; // declare a temp item that we will load before putting in the array
  char response;
  char str[256]; // needed for cin.getline; we are going to use a char array
  if (size < MAX_SIZE) {
    system("cls");
    cout << "Enter Inventory Records" << endl << endl;
    cout << "Name:     ";
    // Get up to 256 characters from the keyboard including white space.
    // Stop reading if encounter the \n first. If there's any chance of 
    // more than 256 characters you will have to clean up cin with
    // cin.ignore before the next input.
    cin.getline(str, 256, '\n'); // for char arrays; different from the other getline
    tmp.name = str;
    cout << "QTY:      ";
    cin >> tmp.qty;
    cout << "Year:     ";
    cin >> tmp.year;
    cout << "SKU:      ";
    cin >> tmp.sku;
    cout << "Cost:     ";
    cin >> tmp.cost;
    cout << endl;
    // see if this record should be added to the array
    cout << "Add the record to inventory? (y/n) ";
    cin >> response;
    if (toupper(response) == 'Y') 
      list[size++] = tmp;
  } else {
    cout << "Inventory at max; cannot enter more items." << endl;
    system("pause");
  }
  system("cls");
}

void dispData(const InventoryRecord list[], int size)
{
  system("cls");
  double cost = 0;
  
  if(size < 1) {
    cout << "Nothing to display" << endl;
  } else {
    cout << "Listing of Items In Inventory" << endl << endl;
    cout << fixed << setprecision(2);   
    cout << "Item Name            QTY        Year           SKU             Cost" << endl;
    cout << "*********************************************************************" << endl;
        
    cout << left;     
    for (int i = 0; i < size; i++) {
      cout << setw(21) << list[i].name << right
           << setw(0)  << list[i].qty
           << setw(13)  << list[i].year
           << setw(15)  << list[i].sku
           << setw(18) << list[i].cost << left << endl;
          
    }
    
    cout << "********************************************************************" << endl;
    cout << right << setw(100) << size;

  }
  
  system("PAUSE");
  system("cls");
}

// Save records to disc
void saveFile(const InventoryRecord list[], int size) {
  ofstream outfi("Inventory.txt");
  
  // make sure the file stream is open before doing IO
  if (!outfi.fail()) { 
    system("cls");  
    cout << "Saving inventory to the disc ";
    
    for(int i = 0; i < size; i++) {
      outfi << list[i].name << ';' 
            << list[i].qty << ';'
            << list[i].year << ';'
            << list[i].sku << ';'
            << list[i].cost;
      // Start a new line after all but the last record
      // Simplifies reading the file as EOF is at end of last line
      if (i < size-1) outfi << endl;
    }
    cout << endl << size << " records writen to the disc." << endl;
    outfi.close();
    system("PAUSE");
    system("cls");
  } 
  else {
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
// Open file and load array
void openFile(InventoryRecord list[], int& size)
{
  ifstream infi("inventory.txt");
  string str;
  stringstream strstrm;
  
  // make sure the file stream is open before doing IO
  if (!infi.fail()) { 
  
    system("cls");
    cout << "Reading inventory from the disc ";
    
    size = 0; // overwrite any existing records
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, str, ';'); 
      list[size].name = str;
      
      // get, convert and store the quantity
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> list[size].qty;
      
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear();
      strstrm << str;
      strstrm >> list[size].year;
      
      getline(infi, str, ';');
      strstrm.str(""); strstrm.clear();
      strstrm << str;
      strstrm >> list[size].sku;
      
      // get, convert and store the cost
      getline(infi, str); 
      strstrm.str(""); strstrm.clear(); // empty and clear the stringstream
      strstrm << str; 
      strstrm >> list[size++].cost;
    }
    cout << endl << size << " records read from the disc." << endl;
  
    system("PAUSE");
    system("cls");
  }
  else { // something went wrong with opening the file
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }

}
void openStore(InventoryRecord list[],int& size)
{
  ifstream infi("store.txt");
  string strng;
  stringstream strbuystrm;
  
  // make sure the file stream is open before doing IO
  if (!infi.fail()) { 
  
    system("cls");
    cout << "Reading inventory from the disc ";
    
    size = 0; // overwrite any existing records
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, strng, ';'); 
      list[size].name = strng;
      
      // get, convert and store the quantity
      getline(infi, strng, ';');
      strbuystrm.str(""); strbuystrm.clear(); // empty and clear the stringstream
      strbuystrm << strng; 
      strbuystrm >> list[size].qty;
      
      getline(infi, strng, ';');
      strbuystrm.str(""); strbuystrm.clear();
      strbuystrm << strng;
      strbuystrm >> list[size].year;
      
      getline(infi, strng, ';');
      strbuystrm.str(""); strbuystrm.clear();
      strbuystrm << strng;
      strbuystrm >> list[size].sku;
      
      getline(infi, strng); 
      strbuystrm.str(""); strbuystrm.clear();
      strbuystrm << strng; 
      strbuystrm >> list[size++].cost;
    }
    cout << endl << size << " records read from the disc." << endl;
  
    system("PAUSE");
    system("cls");
  }
  else { // something went wrong with opening the file
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }
  
}
char getMenuResponse()
// Task:     Put the menu on screen and get a response
// Accepts:  Nothing
// Returns:  The users response
// Modifies: Nothing
// NOTE:     Characters are far more intuitive at the command
//           line than numbers; avoid using numbers.
{
	char response;
	cout << endl << "Make your selection" << endl
		 << "(A)dd Record, (D)isplay Records, (O)pen File, (S)ave File, (Q)uit" << endl
		 << "> ";
	cin >> response;
	cin.ignore(256, '\n');	
	// clean-up up to 256 chars including the delimiter specified (\n, the endl) 
	// OR stop when the \n is encountered after removing it.
	return toupper(response);
	// note the use of toupper, why?
}
Last edited on
I don't see a question.
My question is if i want to make a main database in this case store.txt that can be looked at from a second display, how do i do this without getting errors from the first open.
Example
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
void openStore(InventoryRecord list[],int& size)
{
  ifstream infi("store.txt");
  string strng;
  stringstream strbuystrm;
  
  // make sure the file stream is open before doing IO
  if (!infi.fail()) { 
  
    system("cls");
    cout << "Reading inventory from the disc ";
    
    size = 0; // overwrite any existing records
    while(!infi.eof() && size < MAX_SIZE) {
      // get and store the name
      getline(infi, strng, ';'); 
      list[size].name = strng;
      
      // get, convert and store the quantity
      getline(infi, strng, ';');
      strbuystrm.str(""); strbuystrm.clear(); // empty and clear the stringstream
      strbuystrm << strng; 
      strbuystrm >> list[size].qty;
      
      getline(infi, strng, ';');
      strbuystrm.str(""); strbuystrm.clear();
      strbuystrm << strng;
      strbuystrm >> list[size].year;
      
      getline(infi, strng, ';');
      strbuystrm.str(""); strbuystrm.clear();
      strbuystrm << strng;
      strbuystrm >> list[size].sku;
      
      getline(infi, strng); 
      strbuystrm.str(""); strbuystrm.clear();
      strbuystrm << strng; 
      strbuystrm >> list[size++].cost;
    }
    cout << endl << size << " records read from the disc." << endl;
  
    system("PAUSE");
    system("cls");
  }
I am getting errors when i implement this with also the openFile command. And second how do i create a search function that will search through Names, SKU's or years from the files and display those
Last edited on
Having you tried reading the errors to see what the problem is?
expected constructor, destructor, or type conversion before '<<' token

basically non stop but immediately after i take out the function it works properly.

rly need help plz.
> expected constructor, destructor, or type conversion before '<<' token

On which line are you getting the error?
line 256-259.

How do i go about implementing a search function to search for 6 digit codes (SKU) which are implemented?
1
2
3
4
5
  else { // something went wrong with opening the file
    cout << "ERROR: problem with file" << endl;
    system("PAUSE");
    system("cls");
  }


There doesn't seem to be anything wrong with lines 256-259.

Does it compile cleanly if you comment out the else (lines 256-259) ?



> How do i go about implementing a search function to search for 6 digit codes

Iterate through the list from beginning to end, and check the sku value in each InventoryRecord.
Could u explain a little more indepth about how i should Iterate it?

a little bit programming retarded atm.
Something like this:
1
2
3
4
5
6
7
8
void display_search_results( const InventoryRecord list[], int size, int sku_value )
{
    for( int i = 0 ; i < size() ; ++i )
       if( list[i].sku == sku_value )
       {
           // TODO: display the InventoryRecord
       }
}
Ok. I am still having problems implementing it. This is all i need done on this if anyone could help that would be amazing.

I need to create a sort by either ID, SKU, Name, Year. plz halp !

heres it updated.

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
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdio.h>
#include <string>
#include <limits>
#include <sstream>

using namespace std;

// Data Structure naming and beginning and creating structure to it.
struct Stamplist
{
  string country;   // The country where the stamp comes from
  int ID;           // The ID the Stamp will use
  int qty;          // How many Stamps there are
  int year;         // What Year the Stamp is from
  int sku;          // The SKU number of the Stamp
  double cost;      // the cost or value of the stamp
};


const int MAX_SIZE = 100; //Max array size allowing 100 total stamp creations.


void addStamp(Stamplist list[], int& size); // Function Add Stamp
void dispStamp(const Stamplist list[], int size); // Function Display Stamp
void saveCollection(const Stamplist list[], int size); // Function Save Stamp Collection
void openCollection(Stamplist list[], int& size); // Function Open Stamp Collection
void deleteCollection(const Stamplist list[], int size); // Function Delete Stamp Collection
void deleteSearch(const Stamplist list[],int size); // Function Delete Search Info
void randomStamp(const Stamplist list[],int size); // Random Search
void searchResults(const Stamplist list[], int size);
void savesearch(const Stamplist list[], int size); // Save Search info
char browser();


1
2
3
4
5
6
7
8
9
10
11
12
13
switch ( browser() ) 
  	{
  		case 'A': addStamp(recList, numOfRecs); break; // A to Create Stamp
  		case 'D': dispStamp(recList, numOfRecs); break;// D to Display Stamps from your collection
  		case 'O': openCollection(recList, numOfRecs); break;// O to Open Previous Collection file
  		case 'X': deleteCollection(recList, numOfRecs); break;// X to Delete Collection File
  		case 'S': saveCollection(recList, numOfRecs); break;// S to Save Collection File
  		case 'R': randomStamp(recList, numOfRecs); break;// R to Randomly Select a Stamp from Search
  		case 'E': deleteSearch(recList, numOfRecs); break;// E to Delete Search info
  		case 'M': searchResults(recList, numOfRecs); break;
        case 'P': savesearch(recList, numOfRecs); break; // P to Save Search info
  		case 'Q': run = false; break;// q to quit
  		default : cout << "That is NOT a valid choice" << endl;


The problem is here
1
2
3
4
5
6
7
8
9
10
11
}
}
void searchResults(const Stamplist list[], int size, int sku_value )
{
    for( int i = 0 ; i < size() ; ++i )
       if( list[i].sku == sku_value )
       {
           cout<<Stamplist;
       }
}
}
Last edited on
Mea culpa!

1
2
3
4
5
6
7
8
9
10
11
12
13
void display_search_results( const InventoryRecord list[], int size, int sku_value )
{
    // for( int i = 0 ; i < size() ; ++i )
    for( int i = 0 ; i < size ; ++i )
       if( list[i].sku == sku_value )
       {
           // TODO: display the InventoryRecord
           std::cout << "name: " << list[i]. name << '\n'
                     << "quantity: " << list[i].qty << '\n'
                     // etc.
                     << "\n\n" ;
       }
}


Topic archived. No new replies allowed.