1

I am using code from here: https://www.geeksforgeeks.org/find-day-of-the-week-for-a-given-date/. It finds the day of the week a date falls on. The output is a number corresponding to the day of the week but I would like it to say 'Monday' instead of '1'. How do I change it?

#include <iostream>

using namespace std;

int dayofweek(int d, int m, int y)
{
    static int t[] = { 0, 3, 2, 5, 0, 3,
                       5, 1, 4, 6, 2, 4 };
    y -= m < 3;
    return ( y + y / 4 - y / 100 +
             y / 400 + t[m - 1] + d) % 7;
}

int main()
{
    int day = dayofweek(03, 02, 2020);
    cout << day << endl;

    return 0;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 1
    One way would be to make an array or vector of strings where `nameOfDay[1] == "Monday"`, then you could just output `nameOfDay[day]`. – Rup Aug 27 '20 at 23:52
  • Why are you not using case or if-else to map number with days? – Ahmad Ismail Aug 27 '20 at 23:54
  • Side note: You can also have a lot of fun with [`mktime`](https://en.cppreference.com/w/c/chrono/mktime) and modern C++'s [Date and time utilities](https://en.cppreference.com/w/cpp/chrono) – user4581301 Aug 28 '20 at 00:02
  • Tip: Avoid returning a negative value with extreme `y,m,d`. `% 7` --> `% 7u`. – chux - Reinstate Monica Aug 28 '20 at 01:33
  • Public service announcement. This algorithm assumes a proleptic Gregorian calendar. That is, it applies the rules the Gregorian calendar backward prior to its introduction (which is fine and recommended by ISO 8601). It has a validity limit of 0000-03-01. Prior to that it gives the wrong result. It is valid into the future for millions of years. – Howard Hinnant Aug 28 '20 at 02:56

2 Answers2

2

Write a method that takes an integer input and returns a string.

const char* day(int n) 
{
    static const char* days[] = {
        "Monday", "Tuesday", …, "Sunday"
    }
    if (n >= 1 && n <= 7)
        return days[n-1];
    else
        return "Failday";
}

I assume here Sunday is 7; adjust accordingly if it's 0.

You may prefer std::string to char*; I'll leave that detail to you.

J.Backus
  • 1,441
  • 1
  • 6
  • 6
  • Consider returning `nullptr` for failure. It's easier to test and a lot more noticeable if you forget to test. – user4581301 Aug 27 '20 at 23:57
  • 1
    Correct design depends on intended usage, I'd say. In this particular case the value is just going to be output on the console. If this were 'production' code I'd throw an exception. – J.Backus Aug 28 '20 at 00:20
  • 1
    Which part of my reply is not C++ ? – J.Backus Aug 28 '20 at 00:47
  • Advanced: Testing for a sub-range is good with `n >= 1 && n <= 7`, yet using a [`modulo_Euclidean(n, 7)`](https://stackoverflow.com/a/20638659/2410359) (or the like) would be another reasonable alternative to form the index and allow full range `n`. After all, days of the week repeat. – chux - Reinstate Monica Aug 28 '20 at 01:37
1

There are many ways on how to achieve your goal. One (naive) way would be to write an additional function which looks like

std::string to_date_string(int day) {
    switch(day) {
    case 1:
        return "Monday";
    case 2:
        return "Tuesday";
    // TODO: Continue this pattern.
    }
}

This requires including string header. Then you can simply write

std::cout << to_date_string(day) << std::endl;

As an additional hint: Try to program without using namespace std;. This line can be an evil trap - espacially for beginners.

Paulsor
  • 100
  • 7