C++ Submenu help

I fixed it and now I have a new question.

My second issue is that all the functions except the add function need to be a submenu. I can't find an example online that truly helps me understand what I need to do. If anyone would be willing to show me how to put one of my functions in a submenu I will do the rest.
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
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>


using namespace std;

//declare variables
void Add();
void Edit();
void Display();
void Delete();

struct Inventory
{
    char title[35], artist[25], type[12];
    int year;
    int price;
    int choice;
};

int main()
{
    int choice;
    do
    {
        cout << "MENU" << endl;
        cout << "1. Add Record(s): " << endl;
        cout << "2. Display Records: " << endl;
        cout << "3. Edit Records: " << endl;
        cout << "4. Delete Records: " << endl;
        cout << "Please enter your selection." << endl;
        cin >> choice;

        switch (choice)// change into an if else statement then turn into a function
        {
        case 1:
            Add();
            break;	//Add record
        case 2:
            Display();
            break;	//Display record
        case 3:
            Edit(); 
            break; //Edit Record
        case 4:
            Delete();
            break;   //Delete record

        default:
            cout << "Invalid Selection" << endl;
        }
    }
    while
    (choice <= 4);
    system("PAUSE");
    return 0;
}





//Add function
void Add()
{
    system("CLS"); //clears screen
    fstream fout;
    const int size = 3;
    char ch;
    int i = 0;
    fout.open("Records.txt", ios::out);
    Inventory inv;
    do
    {
        cout << "Enter title: " << endl;
        cin.ignore();
        cin >> inv.title;
        cout << "Enter artist: " << endl;
        cin >> inv.artist;
        cout << "Enter type: " << endl;
        cin >> inv.type;
        cout << "Enter price: " << endl;
        cin >> inv.price;
        cout << "Enter year: " << endl;
        cin.ignore();
        cin >> inv.year;

//write record to file
        fout.write(reinterpret_cast<char*>(&inv), sizeof(inv));
        cout << "Do you want to add another record? " << endl;
        cin >> ch;
    }
    while
    (ch == 'Y' && 1 < 4);

//close the file
    fout.close();
}



//"Display" function
void Display()
{
    fstream fout;
    fout.open("Records.txt", ios::in);
    Inventory inv;
    fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
    while (!fout.eof())
    {

        cout << "\nTitle\t: ";
        cout << inv.title;
        cout << "\nArtist\t: ";
        cout << inv.artist;
        cout << "\nType\t: ";
        cout << inv.type;
        cout << "\nYear\t: ";
        cout << inv.year;
        cout << "\nPrice\t: ";
        cout << inv.price;
        fout.read(reinterpret_cast <char*> (&inv), sizeof(inv));
    }
//close the file
    fout.close();
}


void Edit()
{


}


void Delete()
{

}
Last edited on
EDIT: I didn't read the code careful enough so I suspect my answer is not what you want. See jlb's answer instead.

The Inventory object inside the Add function is a local variable. It is created on line 74 and is destroyed on line 100 when the function ends. It has nothing to do with the Inventory object inside the Display function.

If you want both functions to use the same Inventory object you could use global variable, or you could declare create it in main and pass it as a function argument to the functions that needs it.
Last edited on
Let's start with your Add() function. First you need to be sure the file exists because the fstream with the open mode you specified will not create a file if it doesn't already exist. You should always check that the file opens properly before you try to read or write from the file. My suggestion is to stop using the fstream and use ofstream when do output and ifstream when doing input. The ofstream by default will create a file if it doesn't exist, but you do need to use an open mode that doesn't erase the file contents when it opens, use ios::app to keep the file contents.

And don't forget that the extraction operator stops processing strings when it encounters a white space character, you should consider using getline() instead. And if you do stick with the extraction operator be sure you limit the number of characters that will be extracted using the setw() function to avoid possible buffer overruns.

#include <iomanip>
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Add()
{
    system("CLS"); //clears screen
    //fstream fout; // Not needed, see below,.
    const int size = 3;
    char ch;
    int i = 0;
    ofstream fout("Records.txt", ios::app); // Use the constructor to open the file whenever possible.
    if(!fout) // Check to insure the file opened.
    {
        cerr << "Failed to open the output file.\n";
        return; // No sense going on, since the file didn't open.
    }
    Inventory inv;
    do
    {
        cout << "Enter title: " << endl;
        cin.ignore();
        cin >> setw(35) > inv.title;
...
    // fout.close(); // Not needed, let the destructor do it's job.
}


Topic archived. No new replies allowed.