Make an if statement execute an equation and use answer

Hello,

I've been beating my head against a wall running into different error/not having wanted/correct results. This program is supposed to return what day an entered date is attached to.

First problem, if dayOfWeek is >7, I want it to divide the dayOfWeek by 4 and then use the remainder as the number to display what day of the week it is.

Second question, on lines 34-35, I am getting the error uninitialized local variable 'century' and 'yearValue' used. Which is weird to me. If I remove the declarations before I call the functions on lines 33-35 then initialized them just above like I did before, it will compile, but this doesn't seem the correct way to solve it.

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
137
138
139
//Using zeller's algorithm
//F=K+((13*M+1)/5)+D+(D/4)+(C/4)-2*C --- Formula
//(F=Day of week), (K=Day of month), (D=Last two digits of year), 
//(C=first two digits of year(century)

#include <iomanip>
#include <iostream>
#include <string>
#include <cmath>
//using namespace std;

bool isLeapYear(int);
int getCenturyValue(int);
int getYearValue(int);
void getInput(int&, int&, int&);
void returnWeekDay(int, std::string, int);

int main() {

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

	getInput(year, month, day);

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

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

	returnWeekDay(dayOfWeek, weekDay, dayOfWeekRem);

	system("pause");
}

void getInput(int &year, int &month, int &day) {

	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 true;
		}
	}
	/*else {
		std::cout << "This year is not a leap year.\n";
		return false;
	}*/

	return false;

}

int getCenturyValue(int year) {

	return year / 100;

}

int getYearValue(int year) {

	return year % 100;

}

void returnWeekDay(int dayOfWeek, std::string weekDay, int dayOfWeekRem) {

	/*std::string weekDay;
	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) {
		
		dayOfWeekRem = dayOfWeek % 4;
		std::cout << "I DONT UNDERSTAND THIS AHHHH";
	}

}
Last edited on
I think I solved my problems? (probably an easier way) by adding more if statements once the remainder is found.
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
void returnWeekDay(int dayOfWeek, std::string weekDay, int dayOfWeekRem) {

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

	if (dayOfWeek == 0) {
		std::cout << "This is a Sunday.\n";
	}
	if (dayOfWeek == 1) {
		std::cout << "This is a Monday.\n";
	}
	if (dayOfWeek == 2) {
		std::cout << "This is a Tuesday.\n";
	}
	if (dayOfWeek == 3) {
		std::cout << "This is a Wednesday.\n";
	}
	if (dayOfWeek == 4) {
		std::cout << "This is a Thursday.\n";
	}
	if (dayOfWeek == 5) {
		std::cout << "This is a Friday.\n";
	}
	if (dayOfWeek == 6) {
		std::cout << "This is a Saturday.\n";
	}
	if (dayOfWeek > 7) {
		
		dayOfWeekRem = dayOfWeek % 4;

		if (dayOfWeekRem == 0) {
			std::cout << "This is a Sunday.\n";
		}
		if (dayOfWeekRem == 1) {
			std::cout << "This is a Monday.\n";
		}
		if (dayOfWeekRem == 2) {
			std::cout << "This is a Tuesday.\n";
		}
		if (dayOfWeekRem == 3) {
			std::cout << "This is a Wednesday.\n";
		}
		if (dayOfWeekRem == 4) {
			std::cout << "This is a Thursday.\n";
		}
		if (dayOfWeekRem == 5) {
			std::cout << "This is a Friday.\n";
		}
		if (dayOfWeekRem == 6) {
			std::cout << "This is a Saturday.\n";
		}

		//std::cout << "I DONT UNDERSTAND THIS AHHHH";
	}

}


And I removed the declarations
1
2
3
bool leapYear = isLeapYear(year);
int century = getCenturyValue(century);
int yearValue = getYearValue(yearValue);

to
1
2
3
isLeapYear(year);
getCenturyValue(century);
getYearValue(yearValue);

& declared them just above

but I don't know if I should have done that...


EDIT: Changed the way the algorithm was set up to reflect correct numbers.

Still waiting on this question though just to verify my work.

Thanks!
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
53
54
55
56
57
58
59
60
61
62
63
64
65
// Using zeller's algorithm
// F = K + ( (13 * M + 1) / 5 ) + D + (D / 4) + (C / 4) - 2 * C
// (F=Day of week), (K=Day of month), (D=Last two digits of year), 
// (C=first two digits of year(century)
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>

void getInput(int&, int&, int&);
bool isLeapYear(int);
int getCenturyValue(int);
int getYearValue(int);
void displayWeekDay(int);

int main()
{
    int year = 0;
    int month = 0;
    int day = 0;
    getInput(year, month, day);
    std::cout << "This is "
              << (isLeapYear(year) ? " " : "not ")
              << "a leap year\n";

    int century = getCenturyValue(year);
    int yearValue = getYearValue(year);

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

    displayWeekDay(dayOfWeek);
}

void getInput(int& year, int& month, int& day)
{
    std::cout << "Enter a year: ";
    std::cin >> year;
    // If M is 1 or 2, add 12 to M, and subtract 1 from Y
    std::cout << "Enter a month (1-12): ";
    std::cin >> month;
    if (month == 1 || month == 2) { month += 12; --year; }
    std::cout << "Enter a day (1-31): ";
    std::cin >> day;
    // Further controls needed: what if the user input 31 February?
}

bool isLeapYear(int year)
{
    // copied from here: http://www.cplusplus.com/forum/beginner/224391/#msg1027238
    return !(year % 4) && (year % 100 || !(year % 400));
}

int getCenturyValue(int year) { return year / 100; }

int getYearValue(int year) { return year % 100; }

void displayWeekDay(int dayOfWeek)
{
    std::vector<std::string> days { "Saturday", "Sunday",    "Monday",
                                    "Tuesday",  "Wednesday", "Thursday",
                                    "Friday" };
    std::cout << "This is a " << days.at(dayOfWeek % 7) << '\n';
}

Topic archived. No new replies allowed.