Calculating Week of Day Help!

I wanted to ask what does it mean when my book asks to "create a top-level function named dayofWeek( int dayOfWeek ( int month, int day, int year);)" for this program. The book says to "encapsulate the necessary logic to return the day of week of the specified date as an int(sunday = 0 , monday = 1)" and the most confusing part of all " You should add validation code to the function that tests if any of the inputs are invalid. If so, it should return -1 as day of week.In your main function write a test driver that checks if dayOfWeek is returning the correct values. Your set of test cases should include at least two cases with invalid input. "
Here's is my Code so far(This problem requires to code this first)
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
#include <iostream>
#include <cmath>
using namespace std;

void getInput(int& month, int& day, int& year);
bool isLeapYear(int year);
int getCentury(int year); 
int getYear(int year);
int getMonth(int month, int year);
void getOutput(int day, int century, int monthVal, int yearVal); 

int main()
{
    int century, monthVal, yearVal, day, year, month;


    getInput(month, day, year);
    isLeapYear(year);
    century = getCentury(year);
    yearVal = getYear(year);
    monthVal= getMonth(month, year);
    getOutput(day, century, monthVal, yearVal); 

    system("PAUSE");
    return 0;

}
void getInput(int& month, int& day, int& year)
{
     char garbage1, garbage2; 
     cout << "This Program Determines the Days of Week at a given Date. \n\n";
     cout << "To Begin Please Enter the Date in Month, Day, and Year(Only from 1850 ~ 2200):(ex. 7/4/2008)\n";
     cin  >> month;
     cin  >> garbage1;
     cin  >> day; 
     cin  >> garbage2; 
     cin  >> year;
     while(month <= 0 || month >= 13)
     {
        cout << "Invalid Month!!\n";
        cout << "Please Enter the Date in Month, Day, and Year (ex. 7/4/2008)\n";
        cin  >> month;
        cin  >> garbage1;
        cin  >> day; 
        cin  >> garbage2; 
        cin  >> year;
     } 
     while(year < 1850 || year > 2200)
     {
        cout << "Invalid Year!!\n";
        cout << "Please Enter the Date in Month, Day, and Year(ex. 7/4/2008)\n";
        cin  >> month;
        cin  >> garbage1;
        cin  >> day; 
        cin  >> garbage2; 
        cin  >> year;
     }
     while(day < 1 || day > 31)
     {
        cout << "Invalid Date!!\n";
        cout << "Please Enter the Date in Month, Day, and Year(ex. 7/4/2008)\n";
        cin  >> month;
        cin  >> garbage1;
        cin  >> day; 
        cin  >> garbage2; 
        cin  >> year;
     }

}
bool isLeapYear(int year)
{
    if(year%4 ==0 && year % 100 > 0)
    {
       return true;
    }
    else
        return false;
}
int getCentury(int year)
{
    int year1, total1, total;

    year1 = year / 100;
    total1 = year1 % 4;
    total = 3 - total1;
    return (total * 2);
}
int getYear(int year)
{
    int year1, year2, total1, total;

    year1 = year / 100;
    year2 = year - (year1 * 100);
    total1 = year2 / 4; 

    return ( total + year2);
}
int getMonth(int month, int year)
{
    int value = 0;
    if(isLeapYear(year))
    {
       switch(month)
       {
          case 1: 
               value = 6;
               break;
          case 2: 
               value = 2;
               break;
       }
    }
    else
    {
        switch(month)
        {
           case 1:
                value = 0;
                break;
           case 2:
                value = 3;
                break;
           case 3:
                value = 3;
                break;
           case 4:
                value = 6;
                break;
           case 5:
                value = 1;
                break;
           case 6:
                value = 4;
                break;
           case 7:
                value = 6;
                break;
           case 8:
                value = 2;
                break;
           case 9:
                value = 5;
                break;
           case 10:
                value = 0;
                break;
           case 11:
                value = 3;
                break;
           case 12:
                value = 5;
                break;
           default:
                value = 0 ; 
        }
    }
    return value; 
}
void getOutput(int day, int century, int monthVal, int yearVal)
{
     int total1;  
     string total;
     const int CALC_DAYS = 7 ;  

     total1 = (day + monthVal + yearVal + century ) % CALC_DAYS; 

     switch(total1)
     {
        case 0:
             total = " Sunday ";
             break; 
        case 1:
             total = " Monday ";
             break;
        case 2:
             total = " Tuesday ";
             break;
        case 3:
             total = " Wednesday ";
             break;
        case 4:
             total = " Thursday ";
             break;
        case 5:
             total = " Friday ";
             break;
        case 6:
             total = " Saturday ";
             break;
     }

     cout << "At the Date You have inputed, \n"
          << "The Day of the Week Should be a" << total << ".\n"; 
}
Last edited on
in your code
1
2
getInput(month, day, year);
void getOutput(int day, int century, int monthVal, int yearVal);

are top level functions
Topic archived. No new replies allowed.