Will Somebody Test This Please?

Will this program compile on Windows? The program reads in a text file containing names and addresses. You can then add to the file and print the contents of the address book.

Will someone please compile and run this program on their Windows computer so I can be sure that my instructor will be able to run it?

Thank you so much!

main.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "AddressBook.h"
#include <iostream>
#include <string>
#include <istream>
#include <fstream>
#include <ostream>
using namespace std;

const int ADDR_BOOK_SZ  = 1000;
AddressBook addrBook[ADDR_BOOK_SZ];
int numEntries = 0;
bool fileOpen = false;

void openFile(ifstream& infile, string filename);

int getSize(ifstream& infile, string filename);

void makeArray(ifstream& infile);

void setArray(ifstream& infile, string& filename);

void printArray();

void addEntry();

void writeToFile();

char printMenu();

void printErrorMessage();

int main()
{
    ifstream infile;
    string filename;
    char choice;

    cout << endl;

    do
    {
       choice = printMenu();
       switch (choice)
       {
          case 'a': setArray(infile, filename);
                  break;
          case 'b': addEntry();
                  break;
          case 'c': printArray();
                  break;
          case 'd': break;
          default:
             printErrorMessage();
       }
    } while (choice != 'd');

    writeToFile();

    return 0;
}

void openFile(ifstream& infile, string filename)
{
    infile.open(filename.c_str());
    if (infile.fail())
    {
        cout << "Error in opening the input file." << endl;
        cout << "Ending program now." << endl;
        exit(1);
    }
}

int getSize(ifstream& infile, string filename)
{
    string oneItem;
    int count = 0;

    infile >> oneItem;
    while (!infile.eof())
    {
        getline (infile, oneItem);
        count++;
    }
    infile.close();
    infile.open(filename.c_str());

    return (count / 7);
}

void makeArray(ifstream& infile)
{
    string oneItem;

    for(int j = 0; j < numEntries; j++)
    {
        getline (infile, oneItem);
        addrBook[j].SetFirstName(oneItem);
        getline (infile, oneItem);
        addrBook[j].SetLastName(oneItem);
        getline (infile, oneItem);
        addrBook[j].SetStreetNum(oneItem);
        getline (infile, oneItem);
        addrBook[j].SetStreetName(oneItem);
        getline (infile, oneItem);
        addrBook[j].SetCity(oneItem);
        getline (infile, oneItem);
        addrBook[j].SetState(oneItem);
        getline (infile, oneItem);
        addrBook[j].SetZipCode(oneItem);
    }
    infile.close();
    fileOpen = true;
}

void setArray(ifstream& infile, string& filename)
{
    //cout << "Enter a filename to open: ";
    //cin >> filename;
    filename = "sample.txt";
    openFile(infile, filename);
    numEntries = getSize(infile, filename);
    makeArray(infile);
    cout << "Loaded file and read " << numEntries << " records." << endl << endl;
}

void printArray()
{
    for(int i = 0; i < numEntries; i++)
    {
        addrBook[i].PrintToConsole();
        cout << endl;
    }
    cout << endl;
}

void addEntry()
{
    bool duplicate = false;
    int i = 0;

    addrBook[numEntries].AddEntryFromConsole();

    while(duplicate == false && i < numEntries)
    {
        duplicate = (addrBook[i] == addrBook[numEntries]);
        i++;
    }

    if (duplicate)
    {
        cout << "This is a duplicate entry and will not be added to the address book." << endl << endl;
    }
    else if (!duplicate)
    {
        numEntries++;
    }
}

void writeToFile()
{
    for(int i = 0; i < numEntries; i++)
    {
        addrBook[i].AppendToFile();
        cout << endl;
    }
    cout << endl;
}

char printMenu()
{
    char choice;

    cout << "Please make a selection:" << endl;
    cout << "a) Open an address book file" << endl;
    cout << "b) Add a new address book entry" << endl;
    cout << "c) Print the contents of current address book" << endl;
    cout << "d) Quit" << endl;
    cout << "-> ";
    cin >> choice;
    choice = tolower(choice);
    cout << endl;
    cin.ignore(); //to discard the endl for other cin functions

    return choice;
}

void printErrorMessage()
{
   cout << endl;
   cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << endl;
   cout << "!!!! Input only listed menu options     !!!" << endl;
   cout << "!!!! Please try again.                  !!!" << endl;
   cout << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
   cout << endl;
}


AddressBook.cpp
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
#include "AddressBook.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <sstream>

using namespace std;

int AddressBook::entryCnt = 0;
string AddressBook::filePath = "";
string firstName;
string lastName;
int streetNum;
string streetName;
string city;
string state;
int zipCode;

void AddressBook::SetFirstName(const string fName)
{
    firstName = fName;
}

void AddressBook::SetLastName(const string buff)
{
    lastName = buff;
}

void AddressBook::SetStreetNum(const string buff)
{
    streetNum  = atoi(buff.c_str());
}

void AddressBook::SetStreetNum(int num)
{
    streetNum = num;
}

void AddressBook::SetStreetName(const string buff)
{
    streetName = buff;
}

void AddressBook::SetCity(const string buff)
{
    city = buff;
}

void AddressBook::SetState(const string buff)
{
    state = buff;
}

void AddressBook::SetZipCode(const string buff)
{
    zipCode = atoi(buff.c_str());
}

void AddressBook::SetZipCode(int zip)
{
    zipCode = zip;
}

void AddressBook::GetFirstName(string &buff) const
        {
    buff = firstName;
        }

void AddressBook::GetLastName(string &buff) const
        {
    buff = lastName;
        }

void AddressBook::AddEntryFromConsole()
{
    string input; // for the int properties

    cout << "Enter the first name: ";
    getline(cin, firstName);
    cout << "Enter the last name: ";
    getline(cin, lastName);
    cout << "Enter the street number: ";
    getline(cin, input);
    stringstream fStream(input); // safely converts string to an int
    fStream >> streetNum;
    cout << "Enter the street name: ";
    getline(cin, streetName);
    cout << "Enter the city: ";
    getline(cin, city);
    cout << "Enter the state: ";
    getline(cin, state);
    cout << "Enter the zip code: ";
    getline(cin, input);
    stringstream sStream(input);
    sStream >> zipCode;
    cout << endl << endl;
    entryCnt++;
}

void AddressBook::PrintToConsole()
{
    cout << firstName << endl;
    cout << lastName << endl;
    cout << streetNum << endl;
    cout << streetName << endl;
    cout << city << endl;
    cout << state << endl;
    cout << zipCode << endl;
}

void AddressBook::AppendToFile()
{
    ofstream outfile;
    outfile.open("default.txt");
    outfile << firstName << endl;
    outfile << lastName << endl;
    outfile << streetNum << endl;
    outfile << streetName << endl;
    outfile << city << endl;
    outfile << state << endl;
    outfile << zipCode <<endl;
    outfile.close();
}

AddressBook& AddressBook::operator=(const AddressBook& obj)
{
    AddressBook newBook;
    newBook.firstName = obj.firstName;
    newBook.lastName = obj.lastName;
    newBook.streetNum = obj.streetNum;
    newBook.streetName = obj.streetName;
    newBook.city = obj.city;
    newBook.state = obj.state;
    newBook.zipCode = obj.zipCode;
}

bool operator== (const AddressBook& obj1, const AddressBook& obj2)
        {
    string name[4];
    int first, last;

    obj1.GetFirstName(name[0]);
    obj2.GetFirstName(name[1]);
    obj1.GetLastName(name[2]);
    obj2.GetLastName(name[3]);

    first = name[0].compare(name[1]);
    last = name[2].compare(name[3]);

    return(first == 0 && last == 0);
        }


Thanks!
Last edited on
Here's AddressBook.h:
AddressBook.h
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
#ifndef ADDRESSBOOK_H_
#define ADDRESSBOOK_H_
#include <string>
#include <cstring>

using namespace std;

const int ENTRY_SZ = 256;

class AddressBook {
private:
    string firstName;
    string lastName;
    int streetNum;
    string streetName;
    string city;
    string state;
    int zipCode;

public:
    static int entryCnt;
    static string filePath;

    void SetFirstName(const string fName);
    void SetLastName(const string buff);
    void SetStreetNum(const string buff);
    void SetStreetNum(int num);
    void SetStreetName(const string buff);
    void SetCity(const string buff);
    void SetState(const string buff);
    void SetZipCode(const string buff);
    void SetZipCode(int zip);
    void GetFirstName(string &buff) const;
    void GetLastName(string &buff) const;
    void AddEntryFromConsole();
    void PrintToConsole();
    void AppendToFile();
    AddressBook& operator=(const AddressBook& obj);

};

bool operator== (const AddressBook& obj1, const AddressBook& obj2);

#endif /* ADDRESSBOOK_H_ */ 
Topic archived. No new replies allowed.