How to find first day of a month using C++

Suppose that the first day of a month is a Saturday. Write a program that gets the number (Day date) of the day in that month and prints out what day of the week that day is.

is there a way to do this with only using variables "day and month."?

Can someone show me the simplest way to do this? I'm just starting out.
No. You must also know the year.

1
2
3
4
5
6
7
8
9
10
11
12
// Algorithm:
// http://www.tondering.dk/claus/cal/chrweek.php#calcdow
// Returns: 0=Sunday, 1=Monday, etc
unsigned day_of_week( unsigned year, unsigned month, unsigned day )
  {
  unsigned a, y, m;
  a = (14 - month) / 12;
  y = year - a;
  m = month + (12 * a) - 2;
  // Gregorian:
  return (day + y + (y / 4) - (y / 100) + (y / 400) + ((31 * m) / 12)) % 7;
  }

Good luck!
How are you expected to get the date? from the user or the computer ?

If it's from the user you don' t need the month or year.
Every Saturday is going to be 1, 8, 15, 22 or 29.
The same with the rest of the days of the week.

August 2015
Su Mo Tu We Th Fr Sa
-- -- -- -- -- -- -- 1
02 03 04 05 06 07 08
09 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31


Last edited on
Here're a bunch of Saturdays that aren't 1, 8, 15, 22, or 29.
http://www.timeanddate.com/calendar/?year=2034&country=1
@Duoas

I believe SamuelAdams was going by the facts that ravencoat laid out.

Suppose that the first day of a month is a Saturday.


So, the only change would be, is if the month has 28, 29, (a leap year), 30 or 31 days.
So, the only change would be, is if the month has 28, 29, (a leap year), 30 or 31 days.
Notice that nothing says about any kind of error handling.

So you just need to get rid of full weeks by taking day modulo 7 and then deduce the day depending on remainder:

0: friday
1: saturday
2: sunday
3: monday
...
6: thursday
> Can someone show me the simplest way to do this? I'm just starting out.

We just exploit the simplifying assumptions that are present in the problem statement.
We are asked to write a program that "gets the number (Day date) of the day in that month"

1. We won't concern ourselves with days of any other month.
2. We know that the day of any month can't be less than one or greater than 31.
3. The program has a single input: "the number (Day date) of the day in that (unknown) month".
We know that any value in the range [1,31] could be valid for some of the months.
We assume that a value entered by the user in the range [1,31] is valid for for this particular (unspecified) month.

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

int main()
{
    // Suppose that the first day of the month is a Saturday.
    // invariant: the first day of an unknown month is a saturday

    //  Write a program that gets the number (Day date) of the day in that month
    // invariant: the day entered by the user is a valid day in the same (unknown) month

    std::cout << "enter day: " ;
    unsigned int day ;
    std::cin >> day ;

    // no matter what this unknown month is, day can't be zero, and it can't be more than 31
    if( day == 0 || day > 31 ) std::cerr << "invalid day\n" ;

    else // we assume that the day entered by the user is a valid day in this particular month
    {
        // we know that  1, 8, 15, 22 and 29 are saturdays (SamuelAdams)
        // therefore we know that  2, 9, 16, 23 and 30 are sundays
        // therefore we know that  3, 10, 17, 24 and 31 are mondays
        // etc.

        // if we divide 'day' by 7, the remainder would be
        //    1 for a saturday, 2 for a sunday, 3 for a monday, 4 for a tuesday ... 0 for a friday

        switch( day % 7 ) // assuming that we haven't come across arrays as yet
        {
            case 1 : std::cout << "saturday\n" ; break ;
            case 2 : std::cout << "sunday\n" ; break ;
            case 3 : std::cout << "monday\n" ; break ;
            case 4 : std::cout << "tuesday\n" ; break ;
            case 5 : std::cout << "wednesday\n" ; break ;
            case 6 : std::cout << "thursday\n" ; break ;
            case 0 : std::cout << "friday\n" ;
        }
    }
}
Wow, my brain is slipping. I totally missed the 'first day of the month is Saturday' bit...

Sorry. :o|
Here it is but using variables days and The_Date. Days for how many are in the month, and The_Date, for which day in the month you're asking 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
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

void print(int days, int The_Date)
{
	int x,a;
	string falls_on;
	string week_days[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
	a = (The_Date % 7)-2 ;
	if (a  < 0 )
	a = 6;
	falls_on = week_days[a];
	for (x = 0; x < 7; x++)
		cout << week_days[x][0] << week_days[x][1] << "  ";
	cout  << endl;
	for (x = 0; x < 6; x++)
		cout << "    ";
	for (x = 0; x < days; x++)
	{
		if (x+1 < 10)
			cout << " ";
		cout << x+1 << "  ";
		if (x % 7 == 0)
			cout << endl;
	}
	cout << "The date selected, falls on a " << falls_on << "\n";
}


int main()
{
	int days, The_Date;
	cout << "How many days are there in the month?" << endl;
	do
	{
		cin >> days;
		if (days < 28 || days >31)
			cout << "No month has that many days" << endl;
	} while (days < 28 || days >31);

	cout << "And which day do you want to know about?" << endl;
	do
	{
		cin >> The_Date;
		if (The_Date < 1 || The_Date > days)
			cout << "Wrong input somewhere. Try again.." << endl << endl;
	} while (The_Date < 1 || The_Date >days);
	print(days, The_Date);
	cin >> days;// Just to prevent screen from closing
	return 0;
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
 std:: string day[] = {"Fri", "Sat", "Sun", "Mon", "Tue", "Wed", "Thu"};
 
 int dayNo;
 std::cout << "Please enter date: ";
 std::cin >> dayNo;
 
 std::cout << day[dayNo % 7] << " " << dayNo << std:: endl;
}
Topic archived. No new replies allowed.