struct and arrays

You are required to write a program for a magazine subscription company to handle their
subscription list. The company keeps a large “database” that stores all of their subscribers’
details. Each subscriber has a record containing the following 8 pieces of information:
Subscriber Number
Last Name
First Name
Street Address
City
State
Zip Code
Expiration Date
The subscriber number is a unique integer assigned to each subscriber by the program. The
subscriber number starts at the value of 101, then incremented by one for each subsequent
subscriber. The expiration date is in the form YYMM (e.g., 1305) and defines the last issue of the
magazine that this customer will receive.
The program should start with 8 subscribers as indicated in subscriptions.txt. That is, you should
hard-code these 8 subscriptions in your program. When the program runs, the user will be
prompted to enter the current date (in the same YYMM format) for comparison with the expiry
date of existing subscriptions. The program will allow users
• to enter new subscribers,
• to search and display subscriber information by subscriber number,
• to display all subscriptions in a particular state,
• to display mailing labels (as shown below) of all the unexpired subscriptions,
• to display mailing labels of all the expired subscriptions for the company to send out an
enticement to re-subscribe, and
• to display mailing labels of all subscriptions due to expire in two months so the company can
send out reminders to re-subscribe.

8 Subscriptions:

101
Gray
John
233 Parkview Road, Flushing
Michigan
47489
1809

102
Tang
Charlotte
303 E. Kearsley Street, Flint
Michigan
48502
1712


103
Brown
Coral
8890 Holly Road, Grand Blanc
Michigan
48439
1709

104
Arai
Hugh
77 Riverside Walk, San Antonio
Texas
25786
1805

105
Blackett
Kevin
125 Picking Circle, San Jose
California
60089
1704

106
Achtemichuk
Mark
3987 143 St, Flint
Michigan
48503
1810

107
Browning
William
1 Down Street, Washington
D.C.
24675
1712

108
Brown
Maureen
9066 Osoyoo Crescent, New York City
New York
10021
1802

Important Requirements
GLOBAL VARIABLES are NOT allowed. You should write and call appropriate
functions to perform the operations. Examples: separate functions to tabulate the default
8 subscriptions, to check expiry, to display a record with formatting, to search a
subscription by subscription number, etc.
You must provide the function protoytpes in the beginning of your program and all the
function definitions placed after the main function.
Below is my program. Can someone check my program if it satisfy all the requirements from my questions above. I don't know how to use struct instead of class. How to remove vector library?
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include<iostream>

#include<string>

#include<vector>

using namespace std;

class Subscriber{

public :

int subscriberNumber;

string lastName;

string firstName;

string streetAddress;

string city;

string state;

int zipcode;

int expirationdate;

};

class SubscriberHandling{

private :

int sub_id;

int curDate;

vector<Subscriber> subscribers;

public:

SubscriberHandling(int date);

void hardCode();

void addSubscriber();

void expiredSubscriptions();

void unexpiredSubscriptions();

void subscriptionsInState(string state);

void subscriptionExpiringInTwoMonths();

void searchSubscriber(int num);

};

//driver function

int main()

{

SubscriberHandling sh(2001);//set current date as 2001

sh.expiredSubscriptions();

SubscriberHandling sh1(1712);

sh1.unexpiredSubscriptions();

sh.searchSubscriber(105);

cin.get();//to hold the console screen

return 0;

}

//parameterised constructor

SubscriberHandling::SubscriberHandling(int date){

curDate=date;//set current date

sub_id=101;//initial value of subscriber

hardCode();

}

//hardcode method

void SubscriberHandling:: hardCode()

{

Subscriber one;

one.subscriberNumber=sub_id;

sub_id++;

one.lastName="Gray";

one.firstName="John";

one.streetAddress="233 Parkview Road, Flushing";

one.city="Michigan";

one.state="";

one.zipcode=47489;

one.expirationdate=1809;

Subscriber two;

two.subscriberNumber=sub_id;

sub_id++;

two.lastName="Tang";

two.firstName="Charlotte";

two.streetAddress="303 E. Kearsley Street, Flint";

two.city,"Michigan";

two.state,"";

two.zipcode=48502;

two.expirationdate=1712;

Subscriber three;

three.subscriberNumber=sub_id;

sub_id++;

three.lastName="Brown";

three.firstName="Coral";

three.streetAddress="8890 Holly Road, Grand Blanc ";

three.city="Michigan";

three.state="";

three.zipcode=48439;

three.expirationdate=1709;

Subscriber four;

four.subscriberNumber=sub_id;

sub_id++;

four.lastName="Arai";

four.firstName="Hugh";

four.streetAddress="77 Riverside Walk, San Antonio";

four.city="Texas";

four.state="";

four.zipcode=25786;

four.expirationdate=1805;

Subscriber five;

five.subscriberNumber=sub_id;

sub_id++;

five.lastName="Blackett";

five.firstName="Kevin";

five.streetAddress="125 Picking Circle, San Jose";

five.city="California";

five.state="";

five.zipcode=60089;

five.expirationdate=1704;

Subscriber six;

six.subscriberNumber=sub_id;

sub_id++;

six.lastName="Achtemichuk";

six.firstName="Mark";

six.streetAddress="3987 143 St, Flint";

six.city="Michigan";

six.state="";

six.zipcode=48503;

six.expirationdate=1810;

Subscriber seven;

seven.subscriberNumber=sub_id;

sub_id++;

seven.lastName="Browning";

seven.firstName="William";

seven.streetAddress="1 Down Street, Washington";

seven.city="D.C.";

seven.state="";

seven.zipcode=24675;

seven.expirationdate=1712;



Subscriber eight;

eight.subscriberNumber=sub_id;

sub_id++;

eight.lastName="Brown";

eight.firstName="Maureen";

eight.streetAddress="9066 Osoyoo Crescent, New York City";

eight.city="New York";

eight.state="";

eight.zipcode=10021;

eight.expirationdate=180;



//add the subscribers into the list

subscribers.push_back(one);

subscribers.push_back(two);

subscribers.push_back(three);

subscribers.push_back(four);

subscribers.push_back(five);

subscribers.push_back(six);

subscribers.push_back(seven);

subscribers.push_back(eight);

}

//add subscriber function

void SubscriberHandling::addSubscriber()

{

//create a new subscriber

Subscriber sub;

sub.subscriberNumber=sub_id;//set the subscriber id

sub_id++;//increment the sub id

cout<<"Enter the details of the subscriber.\n"<<endl;

cout<<"Enter The first name."<<endl;

cin>>sub.firstName;//input first name of subscriber

cout<<"Enter The last name."<<endl;

cin>>sub.lastName;//input last name of subscriber

cout<<"Enter The STREET ADDRESS."<<endl;

cin>>sub.streetAddress;//input first name of subscriber

cout<<"Enter The CITY."<<endl;

cin>>sub.city;//input first name of subscriber

cout<<"Enter The STATE."<<endl;

cin>>sub.state;//input first name of subscriber

cout<<"Enter The ZIP-CODE."<<endl;

cin>>sub.zipcode;//input first name of subscriber

cout<<"Enter The EXPIRATION DATE."<<endl;

cin>>sub.expirationdate;//input first name of subscriber

subscribers.push_back(sub);//add the subscriber to the list

}

This is the second part of my program. Sorry it is too long
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//expired subscriptions

void SubscriberHandling::expiredSubscriptions()

{



vector<Subscriber> :: iterator ite;//get an iterator of subscriber type

cout<<"\nThe expired subscriptions are :"<<endl;

//from begin to end of list

for(ite=subscribers.begin();ite!=subscribers.end();ite++)

{

if((*ite).expirationdate<=curDate)

{//found the subscriber

//print the info



cout<<"\nfirst name: "<<(*ite).firstName<<endl;

cout<<"last name: "<<(*ite).lastName<<endl;

cout<<"street Address: "<<(*ite).streetAddress<<endl;

cout<<"city: "<<(*ite).city<<endl;

cout<<"state: "<<(*ite).state<<endl;

cout<<"zip-code: "<<(*ite).zipcode<<endl;

cout<<"expiration date: "<<(*ite).expirationdate<<endl<<endl;



}

}

}

//subscriptionsexpiring in 2 months

void SubscriberHandling::subscriptionExpiringInTwoMonths()

{

vector<Subscriber> :: iterator ite;//get an iterator of subscriber type

cout<<"\nThe subscriptions expiring in 2 months are :"<<endl;

//from begin to end of list

for(ite=subscribers.begin();ite!=subscribers.end();ite++)

{

if((*ite).expirationdate-curDate<=2)

{//found the subscriber

//print the info



cout<<"\nfirst name: "<<(*ite).firstName<<endl;

cout<<"last name: "<<(*ite).lastName<<endl;

cout<<"street Address: "<<(*ite).streetAddress<<endl;

cout<<"city: "<<(*ite).city<<endl;

cout<<"state: "<<(*ite).state<<endl;

cout<<"zip-code: "<<(*ite).zipcode<<endl;

cout<<"expiration date: "<<(*ite).expirationdate<<endl<<endl;

}

}

}

//search for a subscriber using id

void SubscriberHandling::searchSubscriber(int num)

{

vector<Subscriber> :: iterator ite;//get an iterator of subscriber type

//from begin to end of list

for(ite=subscribers.begin();ite!=subscribers.end();ite++)

{

if((*ite).subscriberNumber==num)

{//found the subscriber

//print the info

cout<<"\nThe Subscriber with id "<<num<<" has information as follows :\n"<<endl;

cout<<"first name: "<<(*ite).firstName<<endl;

cout<<"last name: "<<(*ite).lastName<<endl;

cout<<"street Address: "<<(*ite).streetAddress<<endl;

cout<<"city: "<<(*ite).city<<endl;

cout<<"state: "<<(*ite).state<<endl;

cout<<"zip-code: "<<(*ite).zipcode<<endl;

cout<<"expiration date: "<<(*ite).expirationdate<<endl;

return;//return from the function

}

}

cout<<"No such subscriber with this ID exists"<<endl;//else there is not subscriber with id

}

//subscriptions in a state

void SubscriberHandling::subscriptionsInState(string state)

{

vector<Subscriber> :: iterator ite;//get an iterator of subscriber type

cout<<"\nThe subsribers in state "<<state<<" are :\n"<<endl;

//from begin to end of list

for(ite=subscribers.begin();ite!=subscribers.end();ite++)

{

if((*ite).state==state)

{//found the subscriber

//print the info

//cout<<"The Subscriber with id "<<num<<" has information as follows :"<<endl;

cout<<"\nfirst name: "<<(*ite).firstName<<endl;

cout<<"last name: "<<(*ite).lastName<<endl;

cout<<"street Address: "<<(*ite).streetAddress<<endl;

cout<<"city: "<<(*ite).city<<endl;

cout<<"state: "<<(*ite).state<<endl;

cout<<"zip-code: "<<(*ite).zipcode<<endl;

cout<<"expiration date: "<<(*ite).expirationdate<<endl<<endl;

}

}

}

//display subscriptions unexpired

void SubscriberHandling::unexpiredSubscriptions()

{

vector<Subscriber> :: iterator ite;//get an iterator of subscriber type

cout<<"\nThe Unexpired subscriptions are :"<<endl;

//from begin to end of list

for(ite=subscribers.begin();ite!=subscribers.end();ite++)

{

if((*ite).expirationdate>curDate)

{//found the subscriber

//print the info



cout<<"\nfirst name: "<<(*ite).firstName<<endl;

cout<<"last name: "<<(*ite).lastName<<endl;

cout<<"street Address: "<<(*ite).streetAddress<<endl;

cout<<"city: "<<(*ite).city<<endl;

cout<<"state: "<<(*ite).state<<endl;

cout<<"zip-code: "<<(*ite).zipcode<<endl;

cout<<"expiration date: "<<(*ite).expirationdate<<endl<<endl;

}

}

}

I don't know how to use struct instead of class. How to remove vector library? This is the second part of my program. Sorry it is too long

Why would you want to remove vector and class? I don't see it disallowed by the instructions you were given.

Technically a struct and class in C++ are the same except the default access for struct is public, and for class it is private. People do tend to use them differently, but the language itself makes them almost interchangeable.

If you don't use a vector, the idea of having a database to which the user can add new items could become more difficult.

As for the length of the code, there is a lot of duplication, for example, almost identical cout statements in multiple different functions.

I'd suggest the first thing to do is to enhance the Subscriber class by adding constructor(s) and member function(s). That should make the code shorter and simpler.
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
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

using namespace std;

class Subscriber{
private:
    int subscriberNumber;
    string lastName;
    string firstName;
    string streetAddress;
    string city;
    string state;
    int zipcode;
    int expirationdate;

public:    
    Subscriber() : subscriberNumber(0),  zipcode(0),  expirationdate(0) {  }  
    
    Subscriber(int number, string last, string first, string address, 
               string cty, string st, int zip, int expdate) : 
        subscriberNumber(number), lastName(last), firstName(first), 
        streetAddress(address), city(cty), state(st), zipcode(zip), 
        expirationdate(expdate) {  }  
        
    void print() const;  
    
};

class SubscriberHandling {
private :
    int sub_id;
    int curDate;
    vector<Subscriber> subscribers;
public:
    SubscriberHandling(int date);
    void hardCode();
    void printAll();
    void addSubscriber();
    void expiredSubscriptions();
    void unexpiredSubscriptions();
    void subscriptionsInState(string state);
    void subscriptionExpiringInTwoMonths();
    void searchSubscriber(int num);
};

//driver function
int main()
{
    SubscriberHandling sh(2001);//set current date as 2001
    sh.printAll();
    sh.expiredSubscriptions();
    SubscriberHandling sh1(1712);
    sh1.unexpiredSubscriptions();
    sh.searchSubscriber(105);
    cin.get();//to hold the console screen
    return 0;
}

void Subscriber::print() const 
{
    cout << left << setw(19) << "Id:"         << subscriberNumber
         << setw(20) << "\nFirst name: "      << firstName
         << setw(20) << "\nLast name: "       << lastName
         << setw(20) << "\nStreet Address: "  << streetAddress
         << setw(20) << "\nCity: "            << city
         << setw(20) << "\nState: "           << state
         << setw(20) << "\nZip-code: "        << zipcode
         << setw(20) << "\nExpiration date: " << expirationdate
         << "\n\n" << right;
}


//parameterised constructor
SubscriberHandling::SubscriberHandling(int date)
{
    curDate=date;//set current date
    sub_id=101;//initial value of subscriber
    hardCode();
}

// hardcode method
void SubscriberHandling:: hardCode()
{
    // add the subscribers into the list
    subscribers.push_back( Subscriber(101, "Gray", "John", "233 Parkview Road", "Flushing", "Michigan", 47489, 1809) );
    subscribers.push_back( Subscriber(102, "Tang", "Charlotte", "303 E. Kearsley Street", "Flint", "Michigan", 48502, 1712) );
    subscribers.push_back( Subscriber(103, "Brown", "Coral", "8890 Holly Road", "Grand Blanc", "Michigan", 48439, 1709) );
    subscribers.push_back( Subscriber(104, "Arai", "Hugh", "77 Riverside Walk", "San Antonio", "Texas", 25786, 1805) );
    subscribers.push_back( Subscriber(105, "Blackett", "Kevin", "125 Picking Circle", "San Jose", "California", 60089, 1704) );
    subscribers.push_back( Subscriber(106, "Achtemichuk", "Mark", "3987 143 St", "Flint", "Michigan", 48503, 1810) );
    subscribers.push_back( Subscriber(107, "Browning", "William", "1 Down Street", "Washington", "D.C.", 24675, 1712) );
    subscribers.push_back( Subscriber(108, "Brown", "Maureen", "9066 Osoyoo Crescent", "New York City", "New York", 10021, 1802) );    
}

//add subscriber function
void SubscriberHandling::addSubscriber()
{
}

//expired subscriptions
void SubscriberHandling::expiredSubscriptions()
{
}

//subscriptionsexpiring in 2 months
void SubscriberHandling::subscriptionExpiringInTwoMonths()
{
}

//search for a subscriber using id
void SubscriberHandling::searchSubscriber(int num)
{
}

//subscriptions in a state
void SubscriberHandling::subscriptionsInState(string state)
{
}

//display subscriptions unexpired
void SubscriberHandling::unexpiredSubscriptions()
{
}

// Print All Subsribers 
void SubscriberHandling::printAll()
{
    for (const auto & s : subscribers)
        s.print();       
}


I made the members of Subscriber private. If you need to make them public, that's possibly ok too, but you might think about it first, there may be more suitable ways, such as using the print() function or the constructors - or add other functions if needed.

I don't know how to use struct instead of class.


They're identical, apart from default member visibility and inheritance. If you know how to use class, you know how to use struct.
Thanks Chervil
But I don't think my program satisfy all requirements from my problem
like when i run the program it only display the info of 8 subscription
then the program exit

It does not allow user to enter new subscribers
It doesn't display mailing labels of all the expired subscriptions for the company to send out an
enticement to re-subscribe
It doesn't display mailing labels of all subscriptions due to expire in two months so the company can send out reminders to re-subscribe.

Since the id of subscriptions should be unique, you should keep track of ids used and then to increment it as you add a new subscription. That is, you do not need user to enter the id. The same for expiry, which you may assume annual subscription, so if the current date entered is 1712, you may use a formula to make the expiry to be 1812.

If your program assigns the subscription id instead of user input, you can safely use the getline() in the string library to read in the string values (refer to lecture slides if you are unsure) by asking the user to enter them in separate lines, then finally, you just need to use the familiar cin >> to capture the zip code. In this way, there's no need to use the cstring library.

As for the index of the Subscription array, you should keep track of the index already used, then you will know the next index to insert a new subscription. Note, you should use a function to hard-code the first 8 subscriptions in the array, then start with the index of 8 to enter a new subscription.
Registered users can post here. Sign in or register to post.