Using Arrays and Opening files

I am creating an address book that can hold 50 entries. I'm not sure how to modify my exiting program to include an array. Any suggestions would be greatly appreciated.

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
  int main()
{
    const MaxRecord[50];

    entryType userRecord;
    string filename;
    ifstream inData;
    char searchOption;

    OpenFile(filename, inData);

    MainMenu(inData, filename);

    return 0;
}

void OpenFile(string& filename, ifstream& inData)
{
    do {
        cout << "Enter file name to open: ";
        cin >> filename;

        inData.open(filename.c_str());

        if (!inData)
            cout << "File not found!" << endl;

    } while (!inData);
}

void MainMenu(ifstream& inData, string filename)
{
    char menuOption;

    do {
        cout << endl;
        cout << "Select an option..." << endl;
        cout << "(A)dd entry, (P)rint, (S)earch, e(X)it: ";
        cin >> menuOption;
        menuOption = toupper(menuOption);

        switch (menuOption) {
            case 'A':
                AddEntry(inData, filename);
                break;
            case 'P':
                PrintAddressBook(inData);
                break;
            case 'S':
                SearchMenu(inData);
                break;
            case 'X':
                break;
            default:
                cout << "Invalid option selected!" << endl;
                break;
        }

        // Clear file fail state and return to beginning
        inData.clear();
        inData.seekg(0);

    } while (menuOption != 'X');
}

void AddEntry(ifstream& inData, string filename)
{
    entryType userRecord;
    ofstream outData;

    ReadRecord(userRecord);

    inData.close();
    outData.open(filename.c_str(), fstream::app);

    WriteRecord(outData, userRecord);
    outData.close();

    inData.open(filename.c_str());

}
It doesn't look like you've defined what MaxRecord is. Assuming it's not a type of some kind, I assume it is supposed to be the variable name for your array. So, this code will yield a compile-time error:
const MaxRecord[50]

You need to give your array a type. Is it an array of integers? Or an array of strings? Or an array of some class of objects that you've defined? We can't tell. If an integer, it should be:

int MaxRecord[50]

This creates an uninitialized array of integers. If it's supposed to be an array of string class objects:

string MaxRecord[50]

Make sense?

Also, if the elements of the array aren't const , then you don't need const.

Lastly, I don't see where you've defined what a entryType is, so your compiler will likely complain about this too.
Last edited on
entryType is declared in my header file.
I'm just confused on what I would need to change in my open file and main menu function in order to properly execute the array. Everything in else in my code is declared in other functions.
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

#include<string>
using namespace std;
typedef string Address[50];

enum Title {Mr, Mrs, Ms, Dr, NA};

struct NameType {
    Title title;
    string firstName;
    char middleInitial;
    string lastName;
};

struct AddressType {
    string street;
    string city;
    string state;
    string zip;
};

struct PhoneType {
    int areaCode;
    int prefix;
    int number;
};

struct entryType {
    NameType name;
    AddressType address;
    PhoneType phone;
};

const int MAX_RECORDS = 50;

struct addressBookType {
    entryType record[MAX_RECORDS];
    int numEntries;
};
Topic archived. No new replies allowed.