Finding a specific weekday

I was given the assignment of finding the specific weekday of a date while using January 1st as a reference point. This is what I have done but I'm kind of stuck on what to do next :/


#include <iostream>
using namespace std;

int main(){
int weekday, month, day, i=1;
cout<<"Which weekday was January 1st from 1-7?"<<endl;
cin>>weekday

if((weekday<1)||(weekday>7))
{
cout<<"Invalid value."<<endl;
return 0;
}
cout<<"Enter the month."<<endl;
cin>>month;
if((month<1)||(month>12))
{
cout<<"Invalid value."<<endl;
return 0;
}
cout<<"Enter the day."<<endl;
cin>>day;
if((day<1)||(day>31))
{
cout<<"Invalid value."<<endl;
return 0;
}
while(i<month)
{
if((i==1)||(i==3)||(i==5)||(i==7)||(i==8)||(i==10)||(i==12))
{
weekday+=3;
weekday%=7;
}
else if((i==4)||(i==6)||(i==9)||(i==11)
{
weekday+=2;
weekday%=7;
}
else if(i==2)
{
weekday+=0;
weekday%=7;
}
}
I would create a enum/array containing the amount of days in each month then, after getting the date to find the day of, sum up the months before and add up the remaining days. It should be relatively easy to figure out the day after that. (totalDays % 7 then compensate for whatever offset Jan 1st is(?))
Last edited on
In your while() loop, i never changes, so the loop will never end. Also, your code only works for non-leap years.
This is what I was talking about:
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
#include <iostream>
#include <vector>

int main( /* int argc, char** argv */ ) {
	std::vector<int> months(12); 
	months[0]   = 31; // Jan
	months[1]   = 28; // Feb
	months[2]   = 31; // Mar
	months[3]   = 30; // Apr
	months[4]   = 31; // May
	months[5]   = 30; // Jun
	months[6]   = 31; // Jul
	months[7]   = 31; // Aug
	months[8]   = 30; // Sep
	months[9]   = 31; // Oct
	months[10]  = 30; // Nov
	months[11]  = 31; // Dec
	int jan1    = -1,
		month   = -1,
		day     = -1,
		weekday = 0;	

	std::cout << "What day is Jan 1st? ";
	std::cin >> jan1;

	std::cout << "Enter the month: ";
	std::cin >> month;

	std::cout << "Enter the day: ";
	std::cin >> day;

	int total = day;
	for(int i = 0; i < (month - 1); i++) {
		total += months[i];
	} // END for(i)

	weekday = total % 7;
	weekday += (jan1 - 1);
	std::cout << "That date was a ";
	switch(weekday) {
		case 1 :
			std::cout << "Sunday\n";
			break;
		case 2 :
			std::cout << "Monday\n";
			break;
		case 3 :
			std::cout << "Tuesday\n";
			break;
		case 4 :
			std::cout << "Wednesday\n";
			break;
		case 5 :
			std::cout << "Thursday\n";
			break;
		case 6 :
			std::cout << "Friday\n";
			break;
		case 7 :
			std::cout << "Saturday\n";
			break;
		default:
			std::cout << "You entered an invalid date.";
	} // END switch(weekday)

return 0;
} // END main() 


There is no error checking and leap years are not accounted for (although I'm fairly certain you can't deduce if it is a leap year simply with what day Jan 1st is and no knowledge of year. 7 days and a leap year every 4 years would mean it creates a progression rather than cycle (?)). A simpler solution might be to just ask for the year and use Zeller's congruence to figure the day out, as it accounts for leap years and requires no logic statements.

Wiki: https://en.wikipedia.org/wiki/Zeller's_congruence
Last edited on
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
#include <iostream>
using namespace std;


//======================================================================


int input( const char *msg, int low, int high )
{
   int value;
   bool ok = false;
   while ( !ok )
   {
      cout << msg << " ";
      cin >> value;
      ok = ( value >= low && value <= high );
      if ( !ok ) cout << "ERROR!" << endl;
   }
   return value;
}


//======================================================================


int main()
{
   string monthNames[1+12] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };   // ignore [0] element
   int    monthDays [1+12] = {  0,   31 ,   28 ,   31 ,   30 ,   31 ,   30 ,   31 ,   31 ,   30 ,   31 ,   30 ,   31  };   
   string dayNames  [1+7 ] = { "", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

   int jan1 = 0, month = 0, day = 0;

   jan1  = input( "Which day of the week is Jan 1st? (1=Sunday, 7=Saturday)", 1, 7 );

   month = input( "Which month would you like? (1=January, 12=December)", 1, 12 );
   if ( month >= 2 )                   // deal with leap year
   {
      char ans;
      cout << "Is it a leap year? (y/n) ";
      cin >> ans;
      if ( ans == 'y' || ans == 'Y' ) monthDays[2]++;
   }

   day = input( "Which day of the month?", 1, monthDays[month] );

   int test = jan1 + day - 1;
   for ( int i = 1; i < month; i++ ) test += monthDays[i];
   test = ( test - 1 )%7 + 1;          // between 1 and 7, not 0 and 6

   cout << day << " " << monthNames[month] << " is a " << dayNames[test];
}

Last edited on
closed account (48T7M4Gy)
edge6768 has the right idea. Need to accumulate leaps years but the principle is the same. Leap centuries ... whatever.

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

int main()
{
    
    const int NO_MONTHS = 12;
    const int NO_DAYS = 7;
    
    std::string month_names[NO_MONTHS] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    int days_per_month[NO_MONTHS] = { 31 ,   28 ,   31 ,   30 ,   31 ,   30 ,   31 ,   31 ,   30 ,   31 ,   30 ,   31  };
    
    std::string day_name[NO_DAYS] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
    int cumulative_days[NO_MONTHS] = {0};
    
    // *** CALCULATE CUMULATIVE DAYS
    cumulative_days[0] = 0;
    for(int i = 1; i < NO_MONTHS; i++ )
        cumulative_days[i] =  cumulative_days[i-1] + days_per_month[i-1];
    
    //SAMPLE DAY - FORGET THE YEAR FOR THIS SAMPLE - LEAP YEARS SEPARATE ACCUMULATION
    int day = 20;
    int month = 4;
    int day_one_index = 0; //SUNDAY
    
    // *** GENERAL FUNCTIONALITY //
    int no_days_elapsed = cumulative_days[month-1] + day;
    std::cout << no_days_elapsed << '\n';
    int current_day_index = (no_days_elapsed + day_one_index - 1) % 7;
    std::cout << "Day: " << day_name[current_day_index] << '\n';
    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.