Dynamically allocated array

I'm creating an address book program that allow user to search by first name, last name,phone number and address. The user is prompted to enter a file name and the file is read into an array. I'm having trouble modifying by existing SearchFirstName function to loop through array. I have read over this topic multiple times I'm just not understanding it. Any help would be greatly appreciated. I know there are two ways to fix my code dynamically allocated array of strings using GetRecord (which will have to be reallocated multiple times), or just store it all in one string and then find out how many records there are and then create an array based on its size and manually load the whole thing in a for loop. I'm not sure how I would do this.


File

Susan, Smith, 123 456 789
101 Main Street
Bob, Smith, 567 345 9076
456 Market Street

Header File
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
    #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;

     struct addressBookType {
       entryType record[MAX_RECORDS];
       int numEntries;
    };

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
    string 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;

          inData.open(filename.c_str());

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

    } while (!inData);


      if(inData.is_open())
      {

           for(int i=0; i<MAX_RECORDS;i++)
           {
             inData>> bookArray[i];
           }
       }
    }

    // Searches passed file stream for a first name read from the user

     void SearchFirstName(ifstream& inData)
    {
      string searchName;
      entryType userRecord;
      string normalSearchName, normalFirstName;
      char choice;
      bool found = false;

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

      normalSearchName = NormalizeString(searchName);     // Convert name to all uppercase

      // Loop through all records in the file
      while (GetRecord(inData, userRecord)){

        normalFirstName = NormalizeString(userRecord.name.firstName);   // Convert retrieved string to all uppercase

        if (normalFirstName == normalSearchName) { // Requested name matches
            PrintRecord(userRecord);
            cout << "Is this the correct entry? (Y/N)";
            cin >> choice;
            choice = toupper(choice);
            cout << endl;

            if (choice == 'Y') {
                found = true;
                break;
            }
        }
    }

    // Matching name was found before the end of the file
    if (inData && !found){
        cout << "Record found: " << endl;
        PrintRecord(userRecord);
        cout << endl;
    }
    else if (!found)   // End of file. Name not found.
    {
        cout << searchName << " not found!" << endl << endl;
    }

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

My attempt
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
    void SearchFirstName(ifstream& inData)
    {
       string searchName;
       entryType userRecord;
    

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

    string newSearchName = NormalizeString(searchName);
    string upFirst = NormalizeString(userRecord.name.firstName);

    for (int i=0;i<MAX_RECORDS;i++)
    {
        while(newSearchName == upFirst)
        {
            if (bookArray[i]== upFirst)
            {
                cout<<"Name Found";
                cout <<bookArray[i]; //test case
            }
        }


    }
    }
Beside bookArray you need a variable for the amount of read records:

1
2
3
4
5
6
7
8
9
10
11
12
    string bookArray[MAX_RECORDS];
    int bookCount = 0;
...
      if(inData.is_open())
      {

           for(int i=0; i<MAX_RECORDS;i++)
           {
             inData>> bookArray[bookCount];
             ++bookCount; // Note
           }
       }
So if you search for a record you need to use bookCount (or whatever you name id) not MAX_RECORDS. The loop to find the record is similar to the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      for(int i = 0; i < bookCount; ++i){

        normalFirstName = NormalizeString(bookArray[i].name.firstName);   // Convert retrieved string to all uppercase

        if (normalFirstName == normalSearchName) { // Requested name matches
            PrintRecord(bookArray[i]);
            cout << "Is this the correct entry? (Y/N)";
            cin >> choice;
            choice = toupper(choice);
            cout << endl;

            if (choice == 'Y') {
                found = true;
                break;
            }
        }
    }
Topic archived. No new replies allowed.