how add values into an array in C++

how to add values into an array dynamically .. is there any way where u can add values into array without using vector. The code which i have blocked is used for adding values into reminderObject and while running the program the input is asked twice that is frm enter day to enter min idk why its asking twice .

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
#include <iostream>

using namespace std;

void remindersList();


class dateClass
{
    public:

    int day;
    int month;
    int year;
    
    void input();
    void displayLongFormat();
    void displayShortFormat();
    
};


void dateClass::input()
{
    
    cout<<"\n\nEnter Year : "<<endl;
    cin>>year;
    
    cout<<"\nEnter day (1-31) : "<<endl;

    do 
    {
        cin>>day;
    } 
    while (day>31 || day<1);
    
    cout<<"\nSelect the month : "<<endl;
    
    cout<<"\n1.Jan"<<endl;
    cout<<"2.Feb"<<endl;
    cout<<"3.Mar"<<endl;
    cout<<"4.Apr"<<endl;
    cout<<"5.May"<<endl;
    cout<<"6.June"<<endl;
    cout<<"7.July"<<endl;
    cout<<"8.Aug"<<endl;
    cout<<"9.Sept"<<endl;
    cout<<"10.Oct"<<endl;
    cout<<"11.Nov"<<endl;
    cout<<"12.Dec\n"<<endl;
    
    cout<<"Enter month"<<endl;
    do
    {
        cin>>month;
    }
    while (month>12 || month<1);

}


void dateClass::displayLongFormat()
{
    switch (month)
    {
        case 1:
            cout<<day<<" "<<"Jan"<<" "<<year<<endl;
            break;
        case 2:
            cout<<day<<" "<<"Feb"<<" "<<year<<endl;
            break;
        case 3:
            cout<<day<<" "<<"Mar"<<" "<<year<<endl;           
            break;
        case 4:
            cout<<day<<" "<<"Apr"<<" "<<year<<endl;    
            break;
        case 5:
            cout<<day<<" "<<"May"<<" "<<year<<endl;        
            break;
        case 6:
            cout<<day<<" "<<"June"<<" "<<year<<endl;
            break;
        case 7:
            cout<<day<<" "<<"July"<<" "<<year<<endl;
            break;
        case 8:
            cout<<day<<" "<<"Aug"<<" "<<year<<endl;
            break;
        case 9:
            cout<<day<<" "<<"Sept"<<" "<<year<<endl;
            break;
        case 10:
            cout<<day<<" "<<"Oct"<<" "<<year<<endl;
            break;
        case 11:
            cout<<day<<" "<<"Nov"<<" "<<year<<endl;
            break;
        case 12:
            cout<<day<<" "<<"Dec"<<" "<<year<<endl;
            break;
            
        default:displayLongFormat();
            break;
    }

}


void dateClass::displayShortFormat()
{
    cout<<day<<"/"<<month<<"/"<<year<<endl;
}


class timeClass
{
    public:
    int hour;
    int min;
     
    void input();
    void output();
    
};


void timeClass::input()
{
    cout<<"\nEnter hour (0-23) : "<<endl;

    do
    {
        cin>>hour;
    }
    while(hour>23 || hour<0);
        
    cout<<"\nEnter min (0-59) : "<<endl;
    
    do
    {
        cin>>min;
    }
    while (min>59 || min<0);
    
}

void timeClass::output()
{
    cout<<hour<<":"<<min<<endl;
}


class reminder
{
public:
    
    string message;
    
    void displayItems();
    void outputLongFormat();
    void outputShortFormat();
    
    dateClass date;
    timeClass time;
    
    
}reminderObject[10];

void reminder::displayItems()
{
    
    cout<<"\n"<<reminderObject[0].message<<endl;
    date.displayLongFormat(); 
    cout<<reminderObject[0].time.hour<<":"<<reminderObject[0].time.min<<endl;
    
    int select;
    cout<<"\nPress 1 to view all the reminders"<<endl;
    cin>>select;
    
    switch (select)
    {
        case 1:
            remindersList();
            break;
            
        default:displayItems();
            break;
    }
    
}

void remindersList()
{
    reminder newRem;                                 // IMPORTANT  NOTE 

    for (int i=0; i<=3; i++)
    {
        cout<<"\n"<<reminderObject[i].message<<"  "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
    }
    
    int option;
    
    
    cout<<"\n\nSelect Options\n"<<endl;
    cout<<"1.Add Reminder"<<endl;
    cout<<"2.Delete Reminder"<<endl;
    cin>>option;
    
    switch (option)
    {
        case 1:
            cout<<"Enter Message :"<<endl;
            cin>>newRem.message;
            
            newRem.date.input();                    // IMPORTANT   NOTE
            newRem.time.input();                    // IMPORTANT   NOTE   
            newRem.time.output();                  // IMPORTANT   NOTE

            break;
            
            
        case 2:
            break;
            
        default:remindersList();
            break;
    }
    
}


int main (int argc, const char * argv[])
{
    
    reminderObject[0].message="Go and get milk";    
    reminderObject[0].date.day=28;
    reminderObject[0].date.month=5;
    reminderObject[0].date.year=2011;
    reminderObject[0].time.hour=1;
    reminderObject[0].time.min=40;
    
    reminderObject[1].message="Drink Tea";
    reminderObject[1].date.day=28;
    reminderObject[1].date.month=5;
    reminderObject[1].date.year=2011;
    reminderObject[1].time.hour=2;
    reminderObject[1].time.min=00;
    
    reminderObject[2].message="Sleep";
    reminderObject[2].date.day=28;
    reminderObject[2].date.month=5;
    reminderObject[2].date.year=2011;
    reminderObject[2].time.hour=3;
    reminderObject[2].time.min=00;
    
    reminderObject[3].message="Watch IPL";
    reminderObject[3].date.day=28;
    reminderObject[3].date.month=5;
    reminderObject[3].date.year=2011;
    reminderObject[3].time.hour=6;
    reminderObject[3].time.min=00;

    
    reminderObject[0].displayItems();
}
Last edited on
Yes, but you need a counter after line 168 like
1
2
3
4
int reminderCount = 0;
...
reminderObject[reminderCount] = newRem;
++reminderCount;


And yes remove is also possible

There're several problems in your code like line 103 which will crash your program in case of an invalid month. Look here http://www.cplusplus.com/forum/beginner/43617/#msg240465 to learn how to avoid recursion while displaying
when i output the program in case of invalid month it doesnt crash u just have to give valid month number then it asks for next option

the code which u have written should be written in reminderList() in line 193 .?
i am not able to add new reminders into existing list of reminder list .
Last edited on
when i output the program in case of invalid month it doesnt crash u just have to give valid month number then it asks for next option
displayLongFormat() does crash on line 103 (endless recusion) if month is not 1 - 12. So why not replacing default:displayLongFormat(); with default:cout<<"- invalid date -"<<endl;

the code which u have written should be written in reminderList() in line 193 .?

this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class reminder
{
public:
    
    string message;
    
    void displayItems();
    void outputLongFormat();
    void outputShortFormat();
    
    dateClass date;
    timeClass time;
    
    
}reminderObject[10];
int reminderCount = 0;
and this
1
2
3
4
5
6
7
8
9
10
11
12
13
    switch (option)
    {
        case 1:
            cout<<"Enter Message :"<<endl;
            cin>>newRem.message;
            
            newRem.date.input();                    // IMPORTANT   NOTE
            newRem.time.input();                    // IMPORTANT   NOTE   
            newRem.time.output();                  // IMPORTANT   NOTE

            reminderObject[reminderCount] = newRem;
            ++reminderCount;
            break;
For your example set reminderCount = 3; before line 265

This is the way to add an item to an array.

Your input problem is certainly based on wrong recursion. Get rid of them
Last edited on
After the code 218 the for loop will be
1
2
3
4
for (int i=0; i<=9; i++)
            {
                cout<<"\n"<<reminderObject[i].message<<"  "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
            }

since there will be total 10 reminders list ( 3 by default 7 items added ) but when i do this the last reminder which is watch IPL is getting erased and is replaced by new reminder which is added. what should i do to add multiple reminders at a time

how to delete any one item from an existing array
can i do like this

idk how to select the corresponding item .

1
2
3
4
5
6
7
8
9
10
for (int i=0; i<reminderCount; i++)
            {
               reminderObject[i].message=" ";    
               reminderObject[i].date.day=0;
                reminderObject[i].date.month=0;
                reminderObject[i].date.year=0;
               reminderObject[i].time.hour=0;
             reminderObject[i].time.min=0;
                reminderCount=0;
            }


. and i wanted to know whether can items in reminder can be sorted out in chronological according to dates
Last edited on
You need a better grasp to the inherent logic. Trust me global variables are your enemies while the debugger is your friend

After the code 218 the for loop will be ...
Nope. This way you show all objects whether they exist or not. It must be for (int i=0; i<reminderCount; i++)

There are several ways to remove an item
My suggestion would be (with error check (just to show how it's done))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
        case 2:
            if(reminderCount > 0) // Check if remove is possible
            {
              cout<<"Enter item number"<<endl;
              int n;
              cin>>n;
              if((n >= 0) && (n < reminderCount)) // Check if the user entered a valid item
              {
                --reminderCount;
                reminderObject[n] = reminderObject[reminderCount];
              }
              else
                cout<<"Invalid item number"<<endl;
            }
            else
              cout<<"No item to remove"<<endl;
            break;
Last edited on
i am finding in difficulty in inherent logic. is there a way to overcome that like is there a book r something like that or i just have to keep doing different program

and while adding an item into existing array this item replaces the last item which is already there in the reminder list
i am finding in difficulty in inherent logic. is there a way to overcome that
Yes. Debugging. Stepping through your code line by line and watching the content of the variables will definitely show you what's going on. It might be tedious at first but that will make you a better programmer because you know what's going in your code.

If I solve all the problems for you then the problems are solved but you still don't know what's going on.

is there a book r something
Usually most of the things you find in those books you can also find in the internet.

and while adding an item into existing array this item replaces the last item which is already there in the reminder list
You mean this?
1
2
            reminderObject[reminderCount] = newRem;
            ++reminderCount;
No. reminderCount always points to the item after the last. If you have 1 Item then reminderCount = 1 but the item is at reminderObject[0]
yep in this
1
2
 reminderObject[reminderCount] = newRem;
            ++reminderCount;
.

so if i put reminderCount = 5 then the item will be at reminderObject[4]. and so on so if i want to add the reminder after the existing list then i'll have put it as reminderCount = 4 then in output all the first 3 reminders are shown and then the fourth reminder will get displayed which is at reminderObject[3]
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
#include <iostream>

using namespace std;

void remindersList();
void options();
void newremindersList();

class dateClass
{
public:
    
    int day;
    int month;
    int year;
    
    void input();
    void displayLongFormat();
    void displayShortFormat();
    
};


void dateClass::input()
{

    cout<<"\nEnter day (1-31) : "<<endl;
    
    do 
    {
        cin>>day;
        cout<<day;
    } 
    while (day>31 || day<1);
    
    cout<<"\nSelect the month : "<<endl;
    
    cout<<"\n1.Jan"<<endl;
    cout<<"2.Feb"<<endl;
    cout<<"3.Mar"<<endl;
    cout<<"4.Apr"<<endl;
    cout<<"5.May"<<endl;
    cout<<"6.June"<<endl;
    cout<<"7.July"<<endl;
    cout<<"8.Aug"<<endl;
    cout<<"9.Sept"<<endl;
    cout<<"10.Oct"<<endl;
    cout<<"11.Nov"<<endl;
    cout<<"12.Dec\n"<<endl;
    
    cout<<"Enter month"<<endl;
    do
    {
        cin>>month;
        cout<<month;
    }
    while (month>12 || month<1);
    
    cout<<"\n\nEnter Year : "<<endl;
    cin>>year;
    cout<<year;
    
}


void dateClass::displayLongFormat()
{
    switch (month)
    {
        case 1:
            cout<<day<<" "<<"Jan"<<" "<<year<<endl;
            break;
        case 2:
            cout<<day<<" "<<"Feb"<<" "<<year<<endl;
            break;
        case 3:
            cout<<day<<" "<<"Mar"<<" "<<year<<endl;           
            break;
        case 4:
            cout<<day<<" "<<"Apr"<<" "<<year<<endl;    
            break;
        case 5:
            cout<<day<<" "<<"May"<<" "<<year<<endl;        
            break;
        case 6:
            cout<<day<<" "<<"June"<<" "<<year<<endl;
            break;
        case 7:
            cout<<day<<" "<<"July"<<" "<<year<<endl;
            break;
        case 8:
            cout<<day<<" "<<"Aug"<<" "<<year<<endl;
            break;
        case 9:
            cout<<day<<" "<<"Sept"<<" "<<year<<endl;
            break;
        case 10:
            cout<<day<<" "<<"Oct"<<" "<<year<<endl;
            break;
        case 11:
            cout<<day<<" "<<"Nov"<<" "<<year<<endl;
            break;
        case 12:
            cout<<day<<" "<<"Dec"<<" "<<year<<endl;
            break;
            
        default:displayLongFormat();
            break;
    }
    
}


void dateClass::displayShortFormat()
{
    cout<<day<<"/"<<month<<"/"<<year<<endl;
}


class timeClass
{
public:
    int hour;
    int min;
    
    void input();
    void output();
    
};


void timeClass::input()
{
    cout<<"\nEnter hour (0-23) : "<<endl;
    
    do
    {
        cin>>hour;
    }
    while(hour>23 || hour<0);
    
    cout<<"\nEnter min (0-59) : "<<endl;
    
    do
    {
        cin>>min;
    }
    while (min>59 || min<0);
    
}

void timeClass::output()
{
    cout<<hour<<":"<<min<<endl;
}


class reminder
{
public:
    
    string message;
    
    void displayItems();
    void outputLongFormat();
    void outputShortFormat();
    
    dateClass date;
    timeClass time;
    
    
}reminderObject[5];
int reminderCount = 4;
int reminderNumber;
reminder newRem;                                 // IMPORTANT  NOTE 


void reminder::displayItems()
{
    
    cout<<"\n"<<reminderObject[0].message<<endl;
    date.displayLongFormat(); 
    cout<<reminderObject[0].time.hour<<":"<<reminderObject[0].time.min<<endl;
    
    int select;
    cout<<"\nPress 1 to view all the reminders"<<endl;
    cin>>select;
    
    switch (select)
    {
        case 1:
            remindersList();
            break;
            
        default:displayItems();
            break;
    }
    
}

void remindersList()
{
    
    for (int i=0; i<=3; i++)
    {
        cout<<"\n"<<reminderObject[i].message<<"  "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
    }
    int option;
    
    cout<<"\n\nSelect Options\n"<<endl;
    cout<<"1.Add Reminder"<<endl;
    cout<<"2.Delete Reminder"<<endl;
    cin>>option;
    
    switch (option)
    {
        case 1:
            cout<<"Enter Message :"<<endl;
            cin>>newRem.message;
            cout<<newRem.message;
            
            newRem.date.input();                   // IMPORTANT   NOTE
            newRem.time.input();                   // IMPORTANT   NOTE   
            newRem.time.output();                  // IMPORTANT   NOTE
            
            reminderObject[reminderCount] = newRem;
            reminderCount++;
            
            newremindersList();
            break;
            
        case 2:
            
            if(reminderCount > 0) 
            {
                cout<<"Enter item number "<<endl;
                int n;
                cin>>n;
                if((n >= 0) && (n < reminderCount))       // Check if the user entered a valid item
                {
                    reminderObject[n] = reminderObject[reminderCount];
                    reminderCount--;
                }
                else
                {
                    cout<<"Invalid item number"<<endl;
                    newremindersList();
                }
                
            }
            break;
            
        default:remindersList();
            break;
    }
    
}


void newremindersList()
{
    
    for (int i=0; i<=reminderNumber; i++)
    {
        cout<<"\n"<<reminderObject[i].message<<"  "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
    }

}



    

int main (int argc, const char * argv[])
{

    reminderNumber=4; 
    
    reminderObject[0].message="Go and get milk";    
    reminderObject[0].date.day=28;
    reminderObject[0].date.month=5;
    reminderObject[0].date.year=2011;
    reminderObject[0].time.hour=1;
    reminderObject[0].time.min=40;
    
    reminderObject[1].message="Drink Tea";
    reminderObject[1].date.day=28;
    reminderObject[1].date.month=5;
    reminderObject[1].date.year=2011;
    reminderObject[1].time.hour=2;
    reminderObject[1].time.min=00;
    
    reminderObject[2].message="Sleep";
    reminderObject[2].date.day=28;
    reminderObject[2].date.month=5;
    reminderObject[2].date.year=2011;
    reminderObject[2].time.hour=3;
    reminderObject[2].time.min=00;
    
    reminderObject[3].message="Watch IPL";
    reminderObject[3].date.day=28;
    reminderObject[3].date.month=5;
    reminderObject[3].date.year=2011;
    reminderObject[3].time.hour=6;
    reminderObject[3].time.min=00;
    
    
    reminderObject[0].displayItems();

}



i got the output but when i wanted to delete item 1 then i'll have to give 0 in enter item number , in the output in 1st item it shows as 0/0/0 , rest of items are shown ... is there a way where it shows only remaining items than showing null characters in 1st item . I want to delete an item after adding items . I want to sort these items based on the dates .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
case 2:
            if(reminderCount > 0) // Check if remove is possible
            {
              cout<<"Enter item number"<<endl;
              int n;
              cin>>n;
              if((n >= 0) && (n < reminderCount)) // Check if the user entered a valid item
              {
                --reminderCount;
                reminderObject[n] = reminderObject[reminderCount];
              }
              else
                cout<<"Invalid item number"<<endl;
            }
            else
              cout<<"No item to remove"<<endl;
            break;




is this how the code works

when the reminderCount is greater than 0 . input the item number , when the number is greater than 0 and when reminderCount is less than 0 decrement reminderCount ( if it is 4 it becomes 3 ) the reminderObject of the array reminderCount is assigned to reminderObject of that item number array . correct me if i am wrong
Hm, some comments may make it clearer:
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
#include <iostream>

using namespace std;

void remindersList();
void options();
void newremindersList();

class dateClass
{
public:
    
    int day;
    int month;
    int year;
    // Add a constructor that intialze the values
    void input();
    void displayLongFormat();
    void displayShortFormat();
    
};


void dateClass::input()
{
    cout<<"\nEnter day (1-31) : "<<endl;
    
    do 
    {
        cin>>day;
        cout<<day;
    } 
    while (day>31 || day<1); // Wrong logic
    
    cout<<"\nSelect the month : "<<endl;

...    
    
    cout<<"Enter month"<<endl;
    do
    {
        cin>>month;
        cout<<month;
    }
    while (month>12 || month<1); // Wrong logic
    
    cout<<"\n\nEnter Year : "<<endl;
    cin>>year;
    cout<<year;
    
}


void dateClass::displayLongFormat()
{
    switch (month)
    {
...
        default:displayLongFormat(); // This will crash, but it looks like you need to observe it yourself
            break;
    }
    
}


void dateClass::displayShortFormat()
{
    cout<<day<<"/"<<month<<"/"<<year<<endl;
}


class timeClass
{
public:
    int hour;
    int min;
    
    void input();
    void output();
    
};


void timeClass::input()
{
    cout<<"\nEnter hour (0-23) : "<<endl;
    
    do
    {
        cin>>hour;
    }
    while(hour>23 || hour<0); // Wrong logic
    
    cout<<"\nEnter min (0-59) : "<<endl;
    
    do
    {
        cin>>min;
    }
    while (min>59 || min<0); // Wrong logic
    
}

void timeClass::output()
{
    cout<<hour<<":"<<min<<endl;
}


class reminder
{
public:
    
    string message;
    
    void displayItems();
    void outputLongFormat();
    void outputShortFormat();
    
    dateClass date;
    timeClass time;
    
    
};
const int remindeMax = 5 // This is the maximum amount of items (with this you can check whether you'r in range)
reminder reminderObject[remindeMax]; // This are the items with the amount above
int reminderCount = 0; // The number of items used (currently none)
int reminderNumber;
reminder newRem;                                 // IMPORTANT  NOTE 


void reminder::displayItems()
{
    
    cout<<"\n"<<reminderObject[0].message<<endl; // You want the information of this object not always the first
    date.displayLongFormat();  // ok
    cout<<reminderObject[0].time.hour<<":"<<reminderObject[0].time.min<<endl; // Why not time.output();?

   // The following should not be here
    int select;
    cout<<"\nPress 1 to view all the reminders"<<endl;
    cin>>select;
    
    switch (select)
    {
        case 1:
            remindersList();
            break;
            
        default:displayItems();
            break;
    }
    
}

void remindersList()
{
    // 3 is not valid anymore
    for (int i=0; i<=3; i++)
    {
        cout<<"\n"<<reminderObject[i].message<<"  "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
    }
    // Use (misnamed) newremindersList()
    newremindersList(); // Simplier isn't it
    int option;
    
    cout<<"\n\nSelect Options\n"<<endl;
    cout<<"1.Add Reminder"<<endl;
    cout<<"2.Delete Reminder"<<endl;
    cin>>option;
    
    switch (option)
    {
        case 1:
            if(reminderCount < remindeMax) // You cannot add an item if your array is full. If we don't check it may crash!
            {
              // Note that reminderCount is the item after the last valid item
              // With the if clause above we made sure that at least 1 item is available
              // So you don't need a temporary item like 'newRem' anymore
              cout<<"Enter Message :"<<endl;
              cin>>reminderObject[reminderCount].message;
              cout<<reminderObject[reminderCount].message;

              reminderObject[reminderCount].date.input();                   // IMPORTANT   NOTE
              reminderObject[reminderCount].time.input();                   // IMPORTANT   NOTE   
              reminderObject[reminderCount].time.output();                  // IMPORTANT   NOTE
              reminderCount++; // Now the so prepared item gets valid!
            
              newremindersList(); // This shows all valid items
            }
            else
              cout<<"No further reminder available!"<<endl;
            break;
            
        case 2:
            
            if(reminderCount > 0) // This makes sure that there is actually an item to remove!
            {
                cout<<"Enter item number "<<endl;
                int n;
                cin>>n; // Here the user enters the item number we displayed with newremindersList()
                // At newremindersList() we do i + 1 so there must be an n - 1 to access the correct item
                // Keep in mind the index of an array always starts with 0 (_not_ 1)
                if((n > 0) && (n <= reminderCount))       // This checks if the user entered a valid value
                {
                    // The following line makes the last item invalid
                    reminderCount--; // It is important that this expression comes first
                    // Now after decrementing the reminderCount is the index to the previously last valid item
                    reminderObject[n - 1] = reminderObject[reminderCount]; // Note that n - 1 (the user entered the index + 1)
                    // The line above copies the last item which was made invalid to the position where the item to be deleted is (hence overwriting it)
                    // It is a trick to keep deletion simple!
            
                    newremindersList(); // This shows all valid items
                }
                else
                {
                    cout<<"Invalid item number"<<endl;
                    newremindersList();
                }
                
            }
            break;
            
        default:remindersList();
            break;
    }
    
}


void newremindersList() // Better rename it to showremindersList()
{
    
    for (int i=0; i<=reminderNumber; i++)
    {
        cout<<"\n"<<reminderObject[i].message<<"  "<<reminderObject[i].date.day<<"/"<<reminderObject[i].date.month<<"/"<<reminderObject[i].date.year<<","<<reminderObject[i].time.hour<<":"<<reminderObject[i].time.min;
    }

    for (int i=0; i<=reminderCount; i++) // Using reminderCount is important here! Otherwise you wont see the really used item
    {
        cout<<"\n" << i + 1 << ":";
        reminderObject[i].displayItems(); // This simplifies it a lot, don't you think so
    }
}



    

int main (int argc, const char * argv[])
{

    reminderNumber=4; // Sorry not needed

...    
    
    reminderCount = 4; // Now you added 4 items at once!
    remindersList(); // reminderObject[0].displayItems();

}
Last edited on
Topic archived. No new replies allowed.