Apr 22, 2018 at 7:41pm UTC
Hello. Everything is working fine except for the fact that I receive only the correct day when I enter a leap year. For non-leap years, it is returning the wrong day of the week.
I don't want to change my code entirely at this point, but I am looking on how to fix this. Any ideas?
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
#include <iomanip>
#include <iostream>
#include <string>
#include <cmath>
void getInput(int &, int &, int &);
int getCenturyValue(int );
int getYearValue(int );
bool isLeapYear(int );
void returnWeekDay(int , int );
int main() {
int year = 0;
int month = 0;
int day = 0;
int century = int ();
int yearValue = int ();
int dayOfWeekRem = int ();
getInput(year, month, day);
bool leapYear = isLeapYear(year);
getCenturyValue(century);
getYearValue(yearValue);
int dayOfWeek = day + ((13 * (month + 1)) / 5) + (yearValue)
+ (yearValue / 4) + (century / 4) + (5 * century);
returnWeekDay(dayOfWeek, dayOfWeekRem);
system("pause" );
}
void getInput(int &year, int &month, int &day) {
std::cout << "Enter a year: " ;
std::cin >> year;
std::cout << "-----------\n" ;
std::cout << "Enter a month (1-12): \n" ;
std::cout << "Jan. = 13 |\n" ;
std::cout << "Feb. = 14 |\n" ;
std::cout << "Mar. = 3 |\n" ;
std::cout << "Apr. = 4 |\n" ;
std::cout << "May. = 5 |\n" ;
std::cout << "Jun. = 6 |\n" ;
std::cout << "Jul. = 7 |\n" ;
std::cout << "Aug. = 8 |\n" ;
std::cout << "Sep. = 9 |\n" ;
std::cout << "Oct. = 10 |\n" ;
std::cout << "Nov. = 11 |\n" ;
std::cout << "Dec. = 12 |\n" ;
std::cin >> month;
std::cout << "-----------\n" ;
std::cout << "Enter a day (1-31): " ;
std::cin >> day;
std::cout << "-----------\n" ;
}
bool isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
std::cout << "This is a leap year.\n" ;
}
else {
std::cout << "This is not a leap year.\n" ;
}
return year;
}
int getCenturyValue(int year) {
return year / 100;
}
int getYearValue(int year) {
return year % 100;
}
void returnWeekDay(int dayOfWeek, int dayOfWeekRem) {
if (dayOfWeek == 1) {
std::cout << "This is a Sunday.\n" ;
}
if (dayOfWeek == 2) {
std::cout << "This is a Monday.\n" ;
}
if (dayOfWeek == 3) {
std::cout << "This is a Tuesday.\n" ;
}
if (dayOfWeek == 4) {
std::cout << "This is a Wednesday.\n" ;
}
if (dayOfWeek == 5) {
std::cout << "This is a Thursday.\n" ;
}
if (dayOfWeek == 6) {
std::cout << "This is a Friday.\n" ;
}
if (dayOfWeek == 0) {
std::cout << "This is a Saturday.\n" ;
}
if (dayOfWeek >= 7) {
dayOfWeekRem = dayOfWeek % 7;
if (dayOfWeekRem == 1) {
std::cout << "This is a Sunday.\n" ;
}
if (dayOfWeekRem == 2) {
std::cout << "This is a Monday.\n" ;
}
if (dayOfWeekRem == 3) {
std::cout << "This is a Tuesday.\n" ;
}
if (dayOfWeekRem == 4) {
std::cout << "This is a Wednesday.\n" ;
}
if (dayOfWeekRem == 5) {
std::cout << "This is a Thursday.\n" ;
}
if (dayOfWeekRem == 6) {
std::cout << "This is a Friday.\n" ;
}
if (dayOfWeekRem == 0) {
std::cout << "This is a Saturday.\n" ;
}
}
}
Last edited on Apr 22, 2018 at 7:43pm UTC
Apr 22, 2018 at 7:57pm UTC
There's still a number of errors in your code, every one of which I've already addressed.
century and yearValue are not getting values.
Try printing them after the getYearValue call and they will be zero.
And you aren't using leapYear in your calculation at all, so how could it possibly affect it?
Apr 22, 2018 at 8:12pm UTC
@tpb
So I need to pass by reference:
1 2 3 4 5 6 7 8 9 10 11
int getCenturyValue(int &year) {
return year / 100;
}
int getYearValue(int &year) {
return year % 100;
}
Just like I did with getInput, right?
Then,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
int main() {
int year = 0;
int month = 0;
int day = 0;
int dayOfWeekRem = int ();
getInput(year, month, day);
int yearValue = getYearValue(yearValue);
int century = getCenturyValue(century);
bool leapYear = isLeapYear(year);
int dayOfWeek = day + ((13 * (month + 1)) / 5) + (yearValue)
+ (yearValue / 4) + (century / 4) + (5 * century);
returnWeekDay(dayOfWeek, dayOfWeekRem);
system("pause" );
}
Also,
I notice now how I'm not using the leap year, but how would I implement it?
Last edited on Apr 22, 2018 at 8:13pm UTC