Need help with a menu program

So I'm doing a menu program that follows these rules:

Your code need to have the following features, all accessible from a menu.

* Load inventory from the datafile into memory. After loading, the number of inventory items will be displayed on the menu. Showing a zero before it's loaded is fine.
* Add an inventory item to the data file. This will automatically sort the data too (Insertion sort is the one you want!). This will update the number of items on the menu screen
* Search the inventory for an item by inventory number. This assumes the array is sorted (which it should be) and uses a binary search. If found, print the stored info, otherwise print 'not found'.
* List the inventory on the screen in neat columns. Display it in chucks of 15 items with a pause in between.
* Save the inventory to the datafile. It should also stay in memory.

I'm having trouble with the "Add" function as well as the "List" function after using the Add function. I can't add to the file without asking the user what file to load to. I want to be able to just use the file that was loaded with the "Load" function but I'm stuck with how to do that. After I add to the file, using the list function does not show the added inventory only if I had already loaded the file using the Load function. I have yet to do the Save function as well.

I also want to try loading the file without asking the user how much inventory is on the file (prior to using the Load function), but I'm stuck there as well. I'm thinking of starting over the program and doing it with vectors. Would that be easier?

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;

struct data
{
    int Inventory_Num;
    char description[150];
};
void menu (ifstream&, ofstream&, data Inventory[], int size);
void load (ifstream&, data Inventory[], int);
void add (ofstream&, data Inventory[], int);
int search (data Inventory[], int size, int spot);
void print (ofstream& Out);
void list (ofstream&, data Inventory[], int size);

int main()
{
    // Acquires number of inventory to set up array
    int amount;
    cout << "How many inventory items are on the file?: ";
    cin >> amount;
    
    // Sets up array based on inputted number of inventory
    data *other;
    other = new data[amount];
    string file;
    ifstream toscreen;
    ofstream tofile(file.c_str());
    
    menu(toscreen, tofile, other, amount);
    toscreen.close();
    tofile.close();
    delete other;
    system("PAUSE");
    return 0;
}

void menu(ifstream& In, ofstream& Out, data Inventory[], int size)
{
    int choice = 0, number = 0, spot = 0;
    while (choice != 6) {
    cout << endl;
    cout << "To use the menu, please enter one of the numbers: " << endl << endl;
    cout << "1) Load" << endl;
    cout << "2) Add" << endl;
    cout << "3) Search" << endl;
    cout << "4) List" << endl;
    cout << "5) Save" << endl << endl;
    cout << "0) Exit" << endl << endl;
    cout << "Choose a menu option: ";
    cin >> choice;
    switch(choice)
    {
        // Loads a file's inventory
        case 1:
            system("cls");
            load(In, Inventory, size);
            cout << "Press Enter to return to the menu." << endl;
            cin.get();cin.get();
            break;
            
        // Adds to the inventory    
        case 2:
            system("cls");
            add(Out, Inventory, size);
            cout << "Press Enter to return to the menu." << endl;
            cin.get();cin.get();
            break;
            
        // Conducts an inventory search
        case 3:
            system("cls");
            spot = search (Inventory, size, spot);
            if (spot < 0)
            {
                cout << "Sorry, that item is not on the list.";
            }
            cout << "Press Enter to return to the menu." << endl;
            cin.get();cin.get();
            break;
            
        // Lists out the inventory
        case 4:
            system ("cls");
            print(Out);
            list(Out, Inventory, size);
            cout << "Press Enter to return to the menu." << endl;
            cin.get();cin.get();
            break;
            }

    }

}

// Loads file
void load (ifstream& In, data Inventory[], int size)
{
    string file;
    cout << "Please enter the name of the file you would like to load." << endl;
    cin >> file;
    In.open(file.c_str());
    if (In.fail())
    {
        cout << "File not found." << endl;
    }
    else
    {
        cout << "" << file << " has loaded successfully!" << endl;
    }
    
    int i;
    for (i = 0; i < size && !In.eof(); i++)
    {
        In >> Inventory[i].Inventory_Num;
        In.get(Inventory[i].description, 100);
        In.ignore (10, '\n');
    }
}

void add (ofstream& Out, data Inventory[], int size)
{
    cout << "What file would you like to add to?" << endl;
    string file;
    cin >> file;
    int Inventory_Num = 0;
    char description[150];
    Out.open(file.c_str(), ios::app);
    cout << "Enter the inventory number:" << endl;;
    cin >> Inventory_Num;
    Out << endl << Inventory_Num;
    cout << "Enter the inventory description:" << endl;
    cin >> description;
    Out << " " << description << endl;
}

int search (data Inventory[], int size, int spot)
{
    int number = 0;
    cout << "Please enter the inventory number of the item you're looking for." << endl;
    cin >> number;
    for (int i = 0; i < size; i++)
    {
        if (Inventory[i].Inventory_Num == number)
        {
            cout << endl << "Inventory number: " << Inventory[i].Inventory_Num << "" << endl;;
            cout << "" << Inventory[i].description << "" << endl;
        }
    }
}

void print (ofstream& Out)
{
    cout << endl << "Item Number" << '\t';
    cout << setw(25) << "Item Description" << endl;
    cout << "---------------------------------------------" << endl;
    cout << endl;
}

void list(ofstream& Out, data Inventory[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << setw(6) << Inventory[i].Inventory_Num << '\t';
        cout << setw(20) << Inventory[i].description;
        cout << endl;
    }
}
Last edited on
For the 'load' and 'add' just save the content of that 'file' string inserted on line 106 by either returning it from the load function or creating 'string file' outside the load function that passes a reference that can be again passed to the add function.

I also think u'r being too liberal with passing ofstream and ifstream objects all over the place. 'menu' can definitely can create those objects inside itself for example. 'list' and 'print' (maybe others) don't even use the ofstream object u send in.

Line 33 is also a problem because u'r trying to open a file using a string that has not been initialized...

U'll also probably want the ios::app flag on for ur ofstream or else u'll be overwriting the contents in the file each time the file is reopened for writing.
Last edited on
closed account (o3hC5Di1)
Hi there,

Let's tackle one thing at a time here.

You won't be able to use the same filestream as in the load function, because that's an ifstream (meant for reading) and writing to a file requires an ofstream.

What you could do is make the filename a variable in the main() function and pass it by reference to the load() function, it's not the cleanest - but it will work.

Also, remember that when you change the file (add), you will need to reload it in order to view your changes, so you should probably put a statement at the end of the add() function so that the file will be reloaded.

Hope that helps.

All the best,
NwN
OK, I tried making 'string file' in main and pass it by reference to 'add' and 'load' but now I can't get past the first question when compiling it. I put in the number of inventory and the program ends. I think it has something to do with the i/ofstreams but I'm not sure what exactly.

Also, I'm not entirely sure what kind of statement to put in add() to reload it. I don't know if I've learned that.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;

struct data
{
    int Inventory_Num;
    char description[150];
};
void menu (ifstream&, ofstream&, data Inventory[], int size, string& file);
void load (ifstream&, data Inventory[], int size, string& file);
void add (ofstream&, data Inventory[], int size, string& file);
int search (data Inventory[], int size, int spot);
void print (ofstream& Out);
void list (ofstream&, data Inventory[], int size);

int main()
{
    // Acquires number of inventory to set up array
    int amount;
    cout << "How many inventory items are on the file?: ";
    cin >> amount;
    
    // Sets up array based on inputted number of inventory
    data *other;
    other = new data[amount];
    string file;
    ifstream toscreen;
    ofstream tofile(file.c_str());
    
    menu(toscreen, tofile, other, amount, file);
    toscreen.close();
    tofile.close();
    delete other;
    system("PAUSE");
    return 0;
}

void menu(ifstream& In, ofstream& Out, data Inventory[], int size, string& file)
{
    int choice = 0, number = 0, spot = 0;
    while (choice != 0){
    cout << endl;
    cout << "To use the menu, please enter one of the numbers: " << endl << endl;
    cout << "1) Load" << endl;
    cout << "2) Add" << endl;
    cout << "3) Search" << endl;
    cout << "4) List" << endl;
    cout << "5) Save" << endl << endl;
    cout << "0) Exit" << endl << endl;
    cout << "Choose a menu option: ";
    cin >> choice;
    switch(choice)
    {
        // Loads a file's inventory
        case 1:
            system("cls");
            load(In, Inventory, size, file);
            cout << "Press Enter to return to the menu." << endl;
            cin.get();cin.get();
            break;
            
        // Adds to the inventory    
        case 2:
            system("cls");
            add(Out, Inventory, size, file);
            cout << "Press Enter to return to the menu." << endl;
            cin.get();cin.get();
            break;
            
        // Conducts an inventory search
        case 3:
            system("cls");
            spot = search (Inventory, size, spot);
            if (spot < 0)
            {
                cout << "Sorry, that item is not on the list.";
            }
            cout << "Press Enter to return to the menu." << endl;
            cin.get();cin.get();
            break;
            
        // Lists out the inventory
        case 4:
            system ("cls");
            print(Out);
            list(Out, Inventory, size);
            cout << "Press Enter to return to the menu." << endl;
            cin.get();cin.get();
            break;
        }
            
    }

}


// Loads file
void load (ifstream& In, data Inventory[], int size, string& file)
{
    In.open(file.c_str());
    if (In.fail())
    {
        cout << "File not found." << endl;
    }
    else
    {
        cout << "" << file << " has loaded successfully!" << endl;
    }
    
    int i;
    for (i = 0; i < size && !In.eof(); i++)
    {
        In >> Inventory[i].Inventory_Num;
        In.get(Inventory[i].description, 100);
        In.ignore (10, '\n');
    }
}

void add (ofstream& Out, data Inventory[], int size, string& file)
{
    int Inventory_Num = 0;
    char description[150];
    Out.open(file.c_str(), ios::app);
    cout << "Enter the inventory number:" << endl;;
    cin >> Inventory_Num;
    Out << endl << Inventory_Num;
    cout << "Enter the inventory description:" << endl;
    cin >> description;
    Out << " " << description << endl;
}

int search (data Inventory[], int size, int spot)
{
    int number = 0;
    cout << "Please enter the inventory number of the item you're looking for." << endl;
    cin >> number;
    for (int i = 0; i < size; i++)
    {
        if (Inventory[i].Inventory_Num == number)
        {
            cout << endl << "Inventory number: " << Inventory[i].Inventory_Num << "" << endl;;
            cout << "" << Inventory[i].description << "" << endl;
        }
    }
}

void print (ofstream& Out)
{
    cout << endl << "Item Number" << '\t';
    cout << setw(25) << "Item Description" << endl;
    cout << "---------------------------------------------" << endl;
    cout << endl;
}

void list(ofstream& Out, data Inventory[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << setw(6) << Inventory[i].Inventory_Num << '\t';
        cout << setw(20) << Inventory[i].description;
        cout << endl;
    }
}
OK for that, it was because the while loop had choice == 0 while choice was initialized to 0. And the load function was missing the question (lol).

NOW:
I can only add up to one inventory. I tried adding multiple and it wouldn't work.
The 'list' function won't display added inventory.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;

struct data
{
    int Inventory_Num;
    char description[150];
};
void menu (ifstream&, ofstream&, data Inventory[], int size, string& file);
void load (ifstream&, data Inventory[], int size, string& file);
void add (ofstream&, data Inventory[], int size, string& file);
int search (data Inventory[], int size, int spot);
void print (ofstream& Out);
void list (ofstream&, data Inventory[], int size);

int main()
{
    // Acquires number of inventory to set up array
    int amount;
    cout << "How many inventory items are on the file?: ";
    cin >> amount;
    
    // Sets up array based on inputted number of inventory
    data *other;
    other = new data[amount];
    string file;
    ifstream toscreen;
    ofstream tofile(file.c_str());
    
    menu(toscreen, tofile, other, amount, file);
    toscreen.close();
    tofile.close();
    delete other;
    system("PAUSE");
    return 0;
}

void menu(ifstream& In, ofstream& Out, data Inventory[], int size, string& file)
{
    int choice = 0, number = 0, spot = 0;
    while (choice != 6){
        cout << endl;
        cout << "To use the menu, please enter one of the numbers: " << endl << endl;
        cout << "1) Load" << endl;
        cout << "2) Add" << endl;
        cout << "3) Search" << endl;
        cout << "4) List" << endl;
        cout << "5) Save" << endl << endl;
        cout << "6) Exit" << endl << endl;
        cout << "Choose a menu option: ";
        cin >> choice;
        switch(choice)
        {
                // Loads a file's inventory
            case 1:
                system("cls");
                load(In, Inventory, size, file);
                cout << "Press Enter to return to the menu." << endl;
                cin.get();cin.get();
                break;
                
                // Adds to the inventory    
            case 2:
                system("cls");
                add(Out, Inventory, size, file);
                cout << "Press Enter to return to the menu." << endl;
                cin.get();cin.get();
                break;
                
                // Conducts an inventory search
            case 3:
                system("cls");
                spot = search (Inventory, size, spot);
                if (spot < 0)
                {
                    cout << "Sorry, that item is not on the list.";
                }
                cout << "Press Enter to return to the menu." << endl;
                cin.get();cin.get();
                break;
                
                // Lists out the inventory
            case 4:
                system ("cls");
                print(Out);
                list(Out, Inventory, size);
                cout << "Press Enter to return to the menu." << endl;
                cin.get();cin.get();
                break;
        }
        
    }
    
}


// Loads file
void load (ifstream& In, data Inventory[], int size, string& file)
{
    cout << "Please enter the name of the file you would like to load." << endl;
    cin >> file;
    In.open(file.c_str());
    if (In.fail())
    {
        cout << "File not found." << endl;
    }
    else
    {
        cout << "" << file << " has loaded successfully!" << endl;
    }
    
    int i;
    for (i = 0; i < size && !In.eof(); i++)
    {
        In >> Inventory[i].Inventory_Num;
        In.get(Inventory[i].description, 100);
        In.ignore (10, '\n');
    }
}

void add (ofstream& Out, data Inventory[], int size, string& file)
{
    int Inventory_Num = 0;
    char description[150];
    Out.open(file.c_str(), ios::app);
    cout << "Enter the inventory number:" << endl;;
    cin >> Inventory_Num;
    Out << endl << Inventory_Num;
    cout << "Enter the inventory description:" << endl;
    cin >> description;
    Out << " " << description << endl;
}

int search (data Inventory[], int size, int spot)
{
    int number = 0;
    cout << "Please enter the inventory number of the item you're looking for." << endl;
    cin >> number;
    for (int i = 0; i < size; i++)
    {
        if (Inventory[i].Inventory_Num == number)
        {
            cout << endl << "Inventory number: " << Inventory[i].Inventory_Num << "" << endl;;
            cout << "" << Inventory[i].description << "" << endl;
        }
    }
}

void print (ofstream& Out)
{
    cout << endl << "Item Number" << '\t';
    cout << setw(25) << "Item Description" << endl;
    cout << "---------------------------------------------" << endl;
    cout << endl;
}

void list(ofstream& Out, data Inventory[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << setw(6) << Inventory[i].Inventory_Num << '\t';
        cout << setw(20) << Inventory[i].description;
        cout << endl;
    }
}
Fixed the problem with adding multiple items.
But now I still can't display added inventory or search added inventory.
1
2
3
4
5
6
7
8
9
10
11
12
13
void add (ofstream& Out, data Inventory[], int size, string& file)
{
    int Inventory_Num = 0;
    char description[150];
    Out.open(file.c_str(), ios::app);
    cout << "Enter the inventory number:" << endl;;
    cin >> Inventory_Num;
    Out << endl << Inventory_Num;
    cout << "Enter the inventory description:" << endl;
    cin >> description;
    Out << " " << description;
    Out.close( );
}
Last edited on
I've added the 'save' option but it appears that the 'add' function saves the added items anyways. Still having trouble displaying or searching for anything thats added
1
2
3
4
5
6
7
8
 // Saves the inventory
            case 5:
                system("cls");
                save ();
                cout << "Press Enter to return to the menu." << endl;
                cin.get(); cin.get();
                break;


1
2
3
4
void save()
{
    cout << endl << "Your inventory has been saved. " << endl;
}
closed account (o3hC5Di1)
Hi there,

You will need to close the ifstream where the file is open and reopen it afterwards.

Pass the ifstream to the add() function, then close it and call your load() function again to re-open it.

Does that make sense to you?

All the best,
NwN
Topic archived. No new replies allowed.