still having probelms with my program

Hello guys. I'm still having problems getting my program to list the items on the inventory/ add to an item to the file that I loaded..Here's 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
#include<iostream>
#include<string>
#include<iomanip>
#include<fstream>

using namespace std;

struct Inventory
{              
	  long Inventory_N;
	  float Price;
	  char Inventory_Name[50];
};
void menu (ifstream&, ofstream& , Inventory L[], int);
void loadInventory(ifstream&, Inventory L[], int);
void addInventory(ofstream& , Inventory L[], int);
int searchInventory (Inventory L[], int , int );
void printHead(ofstream& fout);
void listInventory (ofstream&, Inventory L[], int);
void saveInventory(ofstream&, Inventory L[], int);

int main()
{
    long amount;
    cout<<"Welcome to the Inventory Management System (IMS)!"<<endl<<endl;
    cout<<"How many Inventory items is on the file that contains the info on the inventory?";
    cin>>amount;
    
    string filename;
    Inventory *Info;
    int Num = amount;
    Info = new Inventory[Num]; /// create a new array of the struct into memory ///
   	ifstream input; /// for reading input ///
    ofstream output(filename.c_str()); /// for output file ///
    	
    /// set format ///
    output << fixed << showpoint << left << setprecision(2);
    /// start of main menu ///
    menu(input, output, Info, Num);
    /// close all files ///
    input.close();
    output.close();
    delete Info;
    
    system("PAUSE");	
    return 0;
}
void menu (ifstream& fin, ofstream& fout, Inventory L[], int size)
{
     int pick = int();
     int number = int();
     int place = int();
     while (pick != 6){           
         cout<<endl;
    	 cout<<"To access a feature, enter one of the numbers listed below: "<<endl<<endl;
    	 cout<<"1 - Load the Inventory."<<endl;
    	 cout<<"2 - Add an Inventory Item."<<endl;
    	 cout<<"3 - Search the Inventory for an item."<<endl;
    	 cout<<"4 - List the Inventory items on the screen "<<endl;
    	 cout<<"5 - Save the Inventory"<<endl;
    	 cout<<"6 - Exit the IMS"<<endl;
    	 cout<<"Pick a feacture to access: ";
    	 cin>>pick;
       	 switch(pick){                           
                      case 1://load
                			system("cls");
                            loadInventory(fin, L, size);
                            
                            cout << "Press Enter to continue." << endl;
    			            cin.get(); cin.get(); /// pause until enter key ///
    			
    			            menu (fin, fout, L, size); /// back to main menu //		
                			break;
                      case 2://add
                            system("cls");
	                        addInventory(fout, L, size);
                			
                			cout << "Press Enter to continue." << endl;
    			            cin.get(); cin.get(); /// pause until enter key ///
    			
    			            menu (fin, fout, L, size); /// back to main menu //		
                			break;
    		          case 3://search
                            system("cls");
                            cout << "Enter the inventory number of the item to search: ";
                            cin>> number;
                			place = searchInventory (L, size, number);
                			
   		             	    if (place == -1)
                			cout<<"Searched item not on the list."<<endl;
                			
                			cout << "Press Enter to continue." << endl;
    			            cin.get(); cin.get(); /// pause until enter key ///
    			
    			            menu (fin, fout, L, size); /// back to main menu //		
                			break;
               		  case 4://list
                			system("cls");
                			printHead(fout);
                            listInventory (fout, L, size);
                            
                            cout << "Press Enter to continue." << endl;
    			            cin.get(); cin.get(); /// pause until enter key ///
    			
    			            menu (fin, fout, L, size); /// back to main menu //		
                			break;
     		          case 5://save
                		    system("cls");
           		            saveInventory(fout, L, size);
           		            
           		            cout << "Press Enter to continue." << endl;
    			            cin.get(); cin.get(); /// pause until enter key ///
    			
    			            menu (fin, fout, L, size); /// back to main menu //		
                			break;
                      case 6://exit
                            cout<<"Thank you...Good Bye!"<<endl;
                            return;
                            break;
     		         default:
                			system("cls");
                			cout<<"Invalid entry!"<<endl;
                			break;
            }
	 } 
}
void loadInventory(ifstream& fin, Inventory L[], int size)
{
    string filename;
    cout<<"Enter the filename that has the inventory stored: "<<endl;
    cin>> filename;
    
    fin.open(filename.c_str());
    if (fin.fail()) {
                    cerr << "The file "<<filename<<" is not found.";
    }
    else{
         cout<<filename<<" opened successfully "<<endl;               
    }
    /// loop until end of array ///
    int i;
    for(i = 0; i < size; i ++){
            fin >> L[i].Inventory_N;
        	fin >> L[i].Price;

        	fin.get(L[i].Inventory_Name, 50); /// get whole name ///
        	fin.ignore (10, '\n'); /// clear all values ///
    }
    /// number of records found ///	
    cout << "The program found " << i << " records." << endl;
}
void addInventory(ofstream& fout, Inventory L[], int size)
{
     string filename;
     fout.open(filename.c_str(), ios::app);
     cout << "Enter inventory number for new data: ";
     long Inventory_N = long();
     cin>> Inventory_N;
     cout<< endl << "Enter the price for this item: $";
     float Price = float();
     cin>>Price; 
     cout<< endl << "Enter the inventory name for this item: ";
     char Inventory_Name[50];
     cin>> Inventory_Name;
     
     fout<<Inventory_N<<endl;
     fout<<Price<<endl;
     fout<<Inventory_Name<<endl;
     fout.close( );   
         
}
int searchInventory (Inventory L[], int size, int locatie)
{
    
    for (int i = 0; i < size; i++)
            		if (locatie == L[i].Inventory_N){
                       cout << endl << "The price of the Inventory '" 
         				    << L[i].Inventory_N << "' is $" << L[i].Price;
                       cout << endl << "The inventory name of the Inventory '"
                            << L[i].Inventory_N << "' is " << L[i].Inventory_Name;
                    }
    
}
void printHead(ofstream& fout)
{
     for (int i = 0; i < 97; i++){ 
         fout << "-"; 
     }
     for (int i = 0; i < 97; i++){ 
         cout << "-"; 
     }
	 fout	<< endl;
	 fout	<< setw(15)	<< "Inventory_number"<<'\t';
	 fout	<< setw(15)	<< "Price"<<'\t';
	 fout	<< setw(15)	<< "Inventory_name";
	 fout	<< endl;
	 for (int j = 0; j < 97; j++){ 
         fout << "-"; 
     }
	 cout	<< endl;
	 cout	<< endl;
	 cout	<< setw(15)	<< "Inventory_number"<<'\t';
	 cout	<< "Price"<<'\t';
	 cout	<< setw(15)	<< "Inventory_name";
	 cout	<< endl;
	 for (int j = 0; j < 97; j++){ 
         cout << "-"; 
     }
	 cout	<< endl;
}
void listInventory (ofstream& fout, Inventory L[], int size)
{
     for (int i = 0; i <size; i++){
         fout	<< setw(15)			<< L[i].Inventory_N<<'\t';
   		 fout	<< "$";
         fout	<< setw(15)			<< L[i].Price<<'\t';
         fout	<< setw(15)			<< L[i].Inventory_Name;
         fout	<< endl;
         cout	<< setw(15)			<< L[i].Inventory_N<<'\t';
   		 cout	<< "$";
         cout	<< setw(15)			<< L[i].Price<<'\t';
         cout	<< setw(15)			<< L[i].Inventory_Name;
         cout	<< endl;
     }
}
void saveInventory(ofstream& fout, Inventory L[], int size)
{
     string filename = string();
     cout<<"What name would you like the file to be saved as? ";
     cin>> filename;
     fout.open(filename.c_str());
     
     for(unsigned i = 0; i < size; i ++){
            fout << L[i].Inventory_N;
        	fout << L[i].Price;
        	fout << L[i].Inventory_Name;
    }
    cout<< endl << "Inventory is saved. "<<endl;
    fout.close();    
}

Thanks
This is incorrect.

The addInventory function saves the new inventory items to a completely different file to the one the original data was loaded from. (Actually, in your case, it doesn't
save any thing because you create a initialized filename and tries saving to it)
1
2
3
4
5
void addInventory(ofstream& fout, Inventory L[], int size)
{
     string filename;
     fout.open(filename.c_str(), ios::app);
     cout << "Enter inventory number for new data: ";



Why do you prompt the user for a filename for saving the inventory? You are going to end up
with files all over the place. Surely you want to resave to the file from which the inventory was originally loaded.

Why ask the user how many records are in the inventory? Just open the file
and count the records as they are loaded.
(AS we are This would probably mean having some MAX_RECORDS limit on number of records that can be stored ).

The menu function is flawed. It is recursive - (that is to say that it calls itself - for example see line 72). If you keep using this program without closing it - then you will get a stack overflow and crash. The menu code should be a loop.
Topic archived. No new replies allowed.