Program not giving wanted results

Hello,

I'm having some trouble with this program that returns what day is attached to the entered date. I understand the problem, but for some reason, it always returns the year as being a leap year when it shouldn't & it always displays Sunday as the day of the week.

I know my code is long and probably unnecessary, but I am still fairly new at this and would like to develop at my own pace. Your help is greatly appreciated.

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
#include <iomanip>
#include <iostream>
#include <string>
#include <cmath>
//using namespace std;

bool isLeapYear(int);
int getCenturyValue(int);
int getYearValue(int);
void getInput();
void returnWeekDay();

int main() {

	int year = int();
	int month = int();
	int day = int();
	int dayOfWeek = int();
	int century = int();
	int yearValue = int();
	std::string weekDay = std::string();
	int dayOfWeekRem = int();

	getInput();

	bool leapYear = isLeapYear(year);
	int getCenturyValue(century);
	int getYearValue(yearValue);

	dayOfWeek = day + ((13 * (month + 1)) / 5) + (yearValue) 
		+ (yearValue / 4) + (century / 4) - (2 * century);

	returnWeekDay();

	system("pause");
}

void getInput() {

	int year = int();
	int month = int();
	int day = int();

	std::cout << "Enter a year: ";
	std::cin >> year;
	std::cout << "Enter a month (1-12): \n";
	std::cout << "Jan. = 1\n";
	std::cout << "Feb. = 4\n";
	std::cout << "Mar. = 3\n";
	std::cout << "Apr. = 6\n";
	std::cout << "May. = 1\n";
	std::cout << "Jun. = 4\n";
	std::cout << "Jul. = 6\n";
	std::cout << "Aug. = 2\n";
	std::cout << "Sep. = 5\n";
	std::cout << "Oct. = 0\n";
	std::cout << "Nov. = 3\n";
	std::cout << "Dec. = 5\n";
	std::cin >> month;
	std::cout << "Enter a day (1-31): ";
	std::cin >> day;

}

bool isLeapYear(int year) {

	if (year % 4 == 0) {
		if (year % 100 == 0) {
			if (year % 400 == 0) {
				std::cout << "This year is a leap year.\n";
				return true;
			}
			else {
				std::cout << "This year is not a leap year.\n";
				return false;
			}
		}
		else {
			std::cout << "This year is not a leap year.\n";
			return false;
		}
	}
	else {
		std::cout << "This year is not a leap year.\n";
		return false;
	}

	return year;

}

int getCenturyValue(int century, int year) {

	century = year / 100;
	return true;

}

int getYearValue(int yearValue, int year) {

	yearValue = year % 100;
	return true;

}

void returnWeekDay() {

	std::string weekDay = std::string();
	int dayOfWeek = int();

	if (dayOfWeek == 0) {
		std::cout << "This is a Sunday.";
	}
	if (dayOfWeek == 1) {
		std::cout << "This is a Monday.";
	}
	if (dayOfWeek == 2) {
		std::cout << "This is a Tuesday.";
	}
	if (dayOfWeek == 3) {
		std::cout << "This is a Wednesday.";
	}
	if (dayOfWeek == 4) {
		std::cout << "This is a Thursday.";
	}
	if (dayOfWeek == 5) {
		std::cout << "This is a Friday.";
	}
	if (dayOfWeek == 6) {
		std::cout << "This is a Saturday.";
	}
	if (dayOfWeek > 7) {
		std::cout << "I STILL NEED TO DO THIS IF STATEMENT";
	}

}
The problem is that the year you pass to isLeapYear isn't the same one getInput uses -
i.e. you always pass 0 the isLeapYear.
Your isLeapYear function is returning "year" at the end instead of false. Since year is never non-zero, it's always interpretted as true. But isLeapYear's logic is wrong in any case.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool isLeapYear(int year) {
    if (year % 4 == 0) {
        if (year % 100 == 0) {
            if (year % 400 == 0)
                return true;
            else
                return false;
        }
        else               // is divisible by 4 but not by 100
            return false;  // so this should return true
    }
    else              // This part ...
        return false; // ... isn't needed ...
    return false;     //     ... since it returns false here anyway
}


So it simplifies down to:
1
2
3
4
5
6
7
8
bool isLeapYear(int year) {
    if (year % 4 == 0)
        if (year % 100 == 0)
            return (year % 400 == 0);
        else
            return true
    return false;
}



returnWeekDay always prints "Sunday" because it initializes dayOfWeek to 0 and then the first if statement is true. You need to pass in dayOfWeek as a parameter.


Also, getInput is not actually returning anything to main. year, month and day in main are totally separate variables from the ones declared in getInput. It needs to be something like:
1
2
3
4
5
6
7
8
9
// Called like this:
int year, month, day;
getInput(year, month, day);

// Declared like this:
void getInput(int &year, int &month, int &day) {
    // don't declare year, month or day as local variables!
    cin >> year >> month >> day;
}




I'm not quite sure what's happening in these two lines in main. I don't even understand why they compile.
1
2
    int getCenturyValue(century);
    int getYearValue(yearValue);


Ultimately, they should be something like
1
2
    int century = getCenturyValue(year);
    int yearValue = getYearValue(year);

Note that I've moved the declarations of century and yearValue to that point so they should be removed from the beginning of main. Modern style is to declare variables near the point of first use so they can be created with a useable value.

And those functions then become:
1
2
3
4
5
6
int getCenturyValue(int year) {
    return year / 100;
}
int getYearValue(int year) {
    return year % 100;
}

Those functions may be overkill, though.


A stylistic point:
1
2
3
4
5
6
//Instead of this
    int year = int();
//write this
    int year = 0;
//or the ultra-modern:
    int year{};


There's never ever any reason to do this
 
    std::string weekDay = std::string();

strings (and all properly made class-based objects) self-initialize to a default value.
 
    std::string weekDay;

Topic archived. No new replies allowed.