Calculation Function?

Below is my code for a calendar program that I'm trying to write. The only conditional statements I haven't written yet are the conditional statements to find the difference between the two dates. The first set of conditions is to prevent input errors. Of course the program won't terminate like I want it to, but meh, I'll deal with that later. What I'm wondering is if I can put the difference between the dates in a separate function than the user input? For example, I'll have the conditional statements that state if a user inputs the wrong numerical value for a month e.g 13, then the program terminates. Then have another function below that to state how the computation will be handled.

The reason I want to do this is so that my code is more organized.

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
  #include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
void graphical_welcome();
void printInstructions();
void userInput(int m1, int d1, int y1, int m2, int d2, int y2);
int main()
{
    // Using an array, I numbered each month to have certain amount of corresponding
    // days. January = 31, February = 28, etc.
    int days_in_months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int d1, d2;
    int m1, m2;
    int y1, y2;
    int year_diff, day_diff;
    int month_total;
    int reg_years = 365;
    // The block above defines all variables for the program
    
    // Display the Welcome Message and program instructions
    graphical_welcome();
    printInstructions();
    
    // Function to prompt the user for input
    userInput(m1, d1, y1, m2, d2, y2);
    
    cin.ignore();
    system("pause");
    return 0;
}
void graphical_welcome()
{
     cout << "|----------------------------------------------|" << endl;
     cout << "|                                              |" << endl;
     cout << "|                                              |" << endl;
     cout << "|           Welcome to the Calendar            |" << endl;
     cout << "|              Dates Calculator                |" << endl;
     cout << "|                                              |" << endl;
     cout << "|                                              |" << endl;
     cout << "|                                              |" << endl;
     cout << "|----------------------------------------------|" << endl;
     cout << "\n";
}

void printInstructions()
{
     cout << "When prompted, please enter the numeric value " << endl;
     cout << "of the month, day, and year you wish to " << endl;
     cout << "find the difference of." << endl;
     cout << "\n";
}

void userInput(int m1, int d1, int y1, int m2, int d2, int y2)
{
     cout << "The First Calendar Date Request" << endl;
     cout << "-------------------------------" << endl;
     cout << "\n";
     
     // Month combined with a conditional statement to prevent wrong input
     cout << "Please enter the month: ";
     cin >> m1;
     
      if(m1 > 12 || m1 <= 0)
    {
        cout<<"Incorrect day entered"<<endl;
        cin.ignore();
    }
     
     // Day combined with a conditional statement
     cout << "Please enter the day: ";
     cin >> d1;
     
     if(d1 > 31 || d1 <= 0)
    {
        cout<<"Incorrect Month entered"<<endl;
        cin.ignore();
    }
     
     //Year combined with a conditional statement
     cout << "Please enter the year: ";
     cin >> y1;
     
     if(y1 > 9999 || y1 < 0)
    {
        cout<<"Incorrect Year Entered"<<endl;
        cin.ignore();
    }
    
    cout << "\n";    
    cout << "The Second Calendar Date Request" << endl;
    cout << "--------------------------------" << endl;
    cout << "\n";
    
    // Month combined with a conditional statement to prevent wrong input
     cout << "Please enter the month: ";
     cin >> m2;
     
      if(m2 > 12 || m2 <= 0)
    {
        cout<<"Incorrect day entered"<<endl;
        cin.ignore();
    }
     
     // Day combined with a conditional statement
     cout << "Please enter the day: ";
     cin >> d2;
     
     if(d2 > 31 || d2 <= 0)
    {
        cout<<"Incorrect Month entered"<<endl;
        cin.ignore();
    }
     
     //Year combined with a conditional statement
     cout << "Please enter the year: ";
     cin >> y2;
     
     if(y2 > 9999 || y2 < 0)
    {
        cout<<"Incorrect Year Entered"<<endl;
        cin.ignore();
    }
}
void userInput(int m1, int d1, int y1, int m2, int d2, int y2);
Doesn't actually take any arguments so it could just be
void userInput();
and then you could just declare the variables inside userInput.

The function you could use for computation could be
void Calculation(int m1, int d1, int y1, int m2, int d2, int y2);
and would take these as args and use them itself.
The function call would have to be inside userInput.

I think this is what you were asking
hope it helps :)
Oh awesome! Thanks Jidder. I'll definitely do that. And it's exactly what I'm asking. I really want to have a function for each operation. It just seems easier to debug.

I thought the cin.ignore(); would terminate the program preventing inputs, but all it does is say "Incorrect month entered" then it'll go on to ask for input of the day.

Thanks loads though Jidder. This will put me on the path that I'm looking for.
Okay, here's the updated version. Although since I can't figure out the argument portion of it, I'm stuck with this. I've got everything that I want laid out, but I'm having trouble with the arguments ironically LOL.. Still hoping for some help on this. It's probably something small, but I can't find it :(

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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

// Function Prototypes
void graphical_welcome();
void printInstructions();
void userInput();
void dateDiff(int m1, int m2, int d1, int d2, int y1, int y2);


int main()
{
    
    // Display the Welcome Message and program instructions
    graphical_welcome();
    printInstructions();
    
    // Function to prompt the user for input
    userInput();
    
    //Output the results
    dateDiff();
    
    cin.ignore();
    system("pause");
    return 0;
}
void graphical_welcome()
{
     cout << "|----------------------------------------------|" << endl;
     cout << "|                                              |" << endl;
     cout << "|                                              |" << endl;
     cout << "|           Welcome to the Calendar            |" << endl;
     cout << "|              Dates Calculator                |" << endl;
     cout << "|                                              |" << endl;
     cout << "|                                              |" << endl;
     cout << "|                                              |" << endl;
     cout << "|----------------------------------------------|" << endl;
     cout << "\n";
}

void printInstructions()
{
     cout << "When prompted, please enter the numeric value " << endl;
     cout << "of the month, day, and year you wish to " << endl;
     cout << "find the difference of." << endl;
     cout << "\n";
}

void userInput()
{
     //Declare the variables for input
    int d1, d2;
    int m1, m2;
    int y1, y2;
     
     
     cout << "The First Calendar Date Request" << endl;
     cout << "-------------------------------" << endl;
     cout << "\n";
     
     // Month combined with a conditional statement to prevent wrong input
     cout << "Please enter the month: ";
     cin >> m1;
     
      if(m1 > 12 || m1 <= 0)
    {
        cout<<"Incorrect day entered"<<endl;
        cin.ignore();
    }
     
     // Day combined with a conditional statement
     cout << "Please enter the day: ";
     cin >> d1;
     
     if(d1 > 31 || d1 <= 0)
    {
        cout<<"Incorrect Month entered"<<endl;
        cin.ignore();
    }
     
     //Year combined with a conditional statement
     cout << "Please enter the year: ";
     cin >> y1;
     
     if(y1 > 9999 || y1 < 0)
    {
        cout<<"Incorrect Year Entered"<<endl;
        cin.ignore();
    }
    
    cout << "\n";    
    cout << "The Second Calendar Date Request" << endl;
    cout << "--------------------------------" << endl;
    cout << "\n";
    
    // Month combined with a conditional statement to prevent wrong input
     cout << "Please enter the month: ";
     cin >> m2;
     
      if(m2 > 12 || m2 <= 0)
    {
        cout<<"Incorrect day entered"<<endl;
        cin.ignore();
    }
     
     // Day combined with a conditional statement
     cout << "Please enter the day: ";
     cin >> d2;
     
     if(d2 > 31 || d2 <= 0)
    {
        cout<<"Incorrect Month entered"<<endl;
        cin.ignore();
    }
     
     //Year combined with a conditional statement
     cout << "Please enter the year: ";
     cin >> y2;
     
     if(y2 > 9999 || y2 < 0)
    {
        cout<<"Incorrect Year Entered"<<endl;
        cin.ignore();
    }
}

void dateDiff(int m1, int m2, int d1, int d2, int y1, int y2)
{
     
     // Using an array, I numbered each month to have certain amount of corresponding
    // days. January = 31, February = 28, etc.
    int days_in_months[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int year_diff, day_diff;
    int months_total;
    int reg_year = 365;
     
     if(y1 == y2)

    {

        year_diff = 0;

    }

    else

    {

        if(y1 % 4 == 0 && y1 % 100 != 0 || y1 % 400 == 0)

        {

            if(y2 % 4 == 0 && y2 % 100 != 0 || y2 % 400 == 0)

           {

                if(y1 > y2)

                {

                    year_diff = (y1 - y2) * (reg_year) + 2;

                }

                else

                {

                    year_diff = (y2 - y1) * (reg_year) + 2;

               }

                if(m2 > m1)

                {

                    if(days_in_months[m1 - 1] > days_in_months[1])
                    {

                        --year_diff;

                    }

                }

            }

            else

            {

                if(y1 > y2)

                {

                    year_diff = (y1 - y2) * (reg_year) + 1;

                }

                else

                {

                    year_diff = (y2 - y1) * (reg_year) + 1;

                }

                if(m1 > m2)

                {

                    if(days_in_months[m2 - 1] > days_in_months[1])

                    {

                        --year_diff;

                    }

                }

            }

        }

        else

        {

            if(y1 > y2)

            {

                year_diff = (y1 - y2) * (reg_year);

            }

            else

            {

                year_diff = (y2 - y1) * (reg_year);

            }

        }

    }

    if(m1 == m2)

    {

        months_total = 0;

    }

    else

    {

        if(m1 > m2)

        {

            for(int i = (m1 - 1); i > (m2 - 1); i--)

            {

                static int months_total_temp = 0;

                months_total_temp += days_in_months[i];

                months_total = months_total_temp;

            }

        }

        else

        {

            for(int i = (m1 - 1); i < (m2 - 1); i++)

            {

                static int months_total_temp = 0;

                months_total_temp += days_in_months[i];

                months_total = months_total_temp;

            }

        }

    }


    int days_total;

 

    if (d1 == d2)

    {

        day_diff = 0;

        days_total = (year_diff + months_total) - day_diff;

    }

    else

    {

        if(d1 > d2)

        {

            day_diff = d1 - d2;

            days_total = (year_diff + months_total) - day_diff;

        }

        else

        {

            day_diff = d2 - d1;

            days_total = (year_diff + months_total) + day_diff;

        }

    }


    if(y1 == y2)

    {

    }

    else

    {

        if(y1 > y2)

        {

            for(int i = (y2 + 1); i < y1; i++)

            {

                if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0)

                {

                    cout<<endl;

                    cout<<i<<endl;

                    ++days_total;

                }

            }

        }

        else

        {

            for(int i = (y1 + 1); i < y2; i++)

            {

                if(i % 4 == 0 && i % 100 != 0 || i % 400 == 0)

                {

                    cout<<endl;

                    cout<<i<<endl;

                    ++days_total;

                }

            }

            }

    }

    cout<<endl;
    cout<<"\nThe total days in between your dates are: "<<days_total<<endl;
    cout<<endl;
     
}
dateDiff(int m1, int m2, int d1, int d2, int y1, int y2);
will have to be called within userInput after all the variables have been assigned values.
Topic archived. No new replies allowed.