Get Day of the Week Program

How can I check for an invalid date when I run the program? I am trying to enter a conditional statement that checks for a valid or invalid date.

Programing Question: Write a program that inputs a date (e.g., July 4, 2008) and outputs the day of the week that corresponds to that date. Here is what I Have. The program runs. I need it to loop around.

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

using namespace std; 
  
const int JANUARY   = 1; 
const int FEBRUARY  = 2; 
const int MARCH     = 3; 
const int APRIL     = 4; 
const int MAY       = 5; 
const int JUNE      = 6; 
const int JULY      = 7; 
const int AUGUST    = 8; 
const int SEPTEMBER = 9; 
const int OCTOBER   = 10; 
const int NOVEMBER  = 11; 
const int DECEMBER  = 12; 

// Returns true if the given year is a leap year
bool is_leap_year(int year); 
// Returns a value computed from the century of the year
int get_century_value(int year); 
// Returns a value computed based on the years since the beginning of the century.
int get_year_value(int year);
// Returns a value (from a table) for the given month  
int get_month_value(int month, int year);  

int main() 

{  
 
 int month, day, year, day_of_week;
	
cout << " Please enter a month: "; 
cin >> month; 
cout << endl;
cout << " Please enter a day: "; 
cin >> day;
cout << endl;
cout << " Please enter a year: "; 
cin >> year;
    
	// Compute the day of the week
	day_of_week = day + get_month_value(month, year) + get_year_value(year) + get_century_value(year);    
    
	day_of_week = day_of_week % 7; 
 
    cout << "\n The date " << month << "/" << day << "/" << year 
         << " is a ";  

    if (day_of_week == 0) 
    { 
		cout << "Sunday."; 
    }  
    else if (day_of_week == 1) 
    { 
		cout << "Monday."; 
    }  
    else if (day_of_week == 2) 
    { 
		cout << "Tuesday."; 
    }  
    else if (day_of_week == 3) 
    { 
		cout << "Wednesday."; 
    }  
    else if (day_of_week == 4) 
    { 
		cout << "Thursday."; 
    }  
    else if (day_of_week == 5) 
    { 
		cout << "Friday."; 
    }  
    else if (day_of_week == 6) 
    { 
		cout << "Saturday."; 
    }  
    cout << endl; 
    return 0; 
} 

// Prompts the user to input a month, day and year 
void get_input(int month, int day, int year) 

{  
	cout << endl;
	cout << " Please enter a month: "; 
    cin >> month; 
    cout << endl;
    cout << " Please enter a day: "; 
    cin >> day;
    cout << endl;
    cout << " Please enter a year: "; 
	cout << endl;
    cin >> year;
}

// Returns true if the given year is a leap year 

bool is_leap_year(int year) 

{  
    return (((year % 400) == 0) ||(((year % 4) == 0) 
	&& ((year % 100) != 0))); 

} 

// Returns a value computed from the century of the year 

int get_century_value(int year) 

{  
    int century;
	int remainder; 
    century = year/100;  
    remainder = (century % 4); 
    return ((3 - remainder) * 2); 
}

// Returns a value computed based on the years since 
// the beginning of the century. 
int get_year_value(int year) 

{  
    int sinceCentury;  
    sinceCentury = year % 100;  
    return (sinceCentury + (sinceCentury/4)); 
} 

// Returns a value (from a table) for the given month 

int get_month_value(int month, int year) 
{  
    int result;  
    if (month == JANUARY) 
    { 
		if (is_leap_year(year))  
        { 
    		result = 6; 
        } 
        else 
		{ 
    		result = 0; 
		} 
	}
    if (month == FEBRUARY) 
	{ 
		if (is_leap_year(year))  
        { 
    		result = 2; 
        } 
        else 
		{ 
    		result = 3; 
		} 

	}
    else if (month == MARCH) 

    	{ 
			result = 3; 
    	}  

    else if (month == APRIL) 

    { 
		result = 6; 
    }  

    else if (month == MAY) 

    { 
		result = 1; 
    }  

    else if (month == JUNE) 

    { 
		result = 4; 
    }  

    else if (month == JULY) 

    { 
		result = 6; 
    }  

    else if (month == AUGUST) 

    { 
		result = 2; 
    }  

    else if (month == SEPTEMBER) 

    { 
		result = 5; 
    }  

    else if (month == OCTOBER) 

    { 
		result = 0;
    }  

    else if (month == NOVEMBER) 

    { 
		result = 3; 
    }  

    else if (month == DECEMBER) 

    { 
		result = 5; 
    } 
  
    return result; 
	
}
Last edited on
For validating days and months just check if they have values from 1 to 12 for the month and after that, depending on which month it is check if the day is 1-31, 1-30 or special case for february 1-28 / 1-29 which also depend from the year.
You mentioned putting the program into a loop...Just put all of your code in "main" in a "while(1)" loop and make sure you have a condition for exiting the while loop and quiting the program.

Tip: it's much better to use switch than ifelse when comparing same number to a lot of constant values.
Topic archived. No new replies allowed.