Struct Array and Opening File


My code so far for a phone directory program. This program is to read from a file given by user into an array. Then allows user to search by name , phone or address, add an entry, delete an entry or print the entire directory. My problem is that my open file function is not reading the file into the array or its a possible that my searchFirstName function is not properly looping through the array. If any one has any suggestions on how I could fix my code it would be greatly appreciated.

File

Mickey, Mouse, 123 555 1212
1 Disney Lane, Orlando, FL, 12345
Donald, Duck, 123 555 1213
1313 Webfoot Walk, Duckburg, CA, 00000

Header
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
#include<string>
using namespace std;

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

struct NameType {
    Title title;
    string firstName;
    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;

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
entryType bookArray[MAX_RECORDS];
int main()
{
    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;
    for(int i=0; i<MAX_RECORDS;i++)
        {
            inData >> bookArray[i].name.firstName;
            inData>>bookArray[i].name.lastName;
            inData>>bookArray[i].address.street;

        }
        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, (D)elete 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 'D':
                DeleteEntry(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());

}

void PrintAddressBook(ifstream& inData)
{
    entryType userRecord;
    char choice;

    cout << "Do you want to print a brief list? (Y/N)";
    cin >> choice;
    choice = toupper(choice);

    if (choice == 'N') {
        // Loop through all records in the file
        while (GetRecord(inData, userRecord)){
            PrintRecord(userRecord);
            cout << endl;
        }
    }
    else {
        while (GetRecord(inData, userRecord)){
            PrintBriefRecord(userRecord);
            cout << endl;
        }
    }
}

void SearchMenu(ifstream& inData)
{
    char searchOption;

    do {
        cout << endl;
        cout << "Enter how you want to search... " << endl;
        cout << "(F)irst name, (L)ast name, (A)ddress, (P)hone, e(X)it: ";
        cin >> searchOption;
        searchOption = toupper(searchOption);

        switch (searchOption) {
            case 'F':
                SearchFirstName(inData);
                break;
            case 'L':
                SearchLastName(inData);
                break;
            case 'A':
                SearchAddress(inData);
                break;
            case 'P':
                SearchPhone(inData);
                break;
            case 'X':
                break;
            default:
                cout << "Invalid option selected!" << endl;
                break;
        }

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

// Searches passed file stream for a first name read from the user
void SearchFirstName(ifstream& inData)
{
    entryType userRecord;
    string searchName;

    char choice;

    cout << "Enter first name to search for: ";
    cin >> searchName;

     string newString;
       newString = NormalizeString(searchName);   // Convert retrieved string to all uppercase

       for (int i=0;i<MAX_RECORDS;i++)
{
       string userString = NormalizeString(bookArray[i].name.firstName);


        if (newString == userString) { // Requested name matches
            cout<<bookArray[i].name.firstName;
            cout<<bookArray;
            }
        }

    // Matching name was found before the end of the file

    // Clear file fail state and return to beginning
    inData.clear();
    inData.seekg(0);
}
Well, you read the data before you open the file. so move line 29 before line 22.

Generally: There should be only one load and one save function. Searching and editing should be done using the array.

Currently you read the maximum (50) entries while the file just contains less[2]. So you need another variable that holds the number of entries. Stop the reading when the stream tells eof().
Last edited on
Topic archived. No new replies allowed.