Something is wrong


Joined
Mar 7, 2022
Messages
1
Reaction score
0
So I my teacher asked me to make a code and I did. Most test cases pass, but 1 hidden test fails. I really do not know how to test which one failed and why it failed. Any ideas?
code:
#include <iostream>
using namespace std;
//Conidtional Sections
// Making each conditional to a function
int y;
int m;
int d;



//Checking if year is in range
bool inRange(int y, int m, int d) {
if (y < 2022) {
return true;
}
else if (y == 2000) {
if (m == 2 && d <= 28) {
return true;
}
}
else {
return false;
}
}
//Checking if leap year:
bool leapYear(int y) {
if (((y % 4 == 0) && (y % 100 != 0)) || (y % 400 == 0)) {
return true;
}
return false;
}

// If date is valid:
bool valid(int y, int m, int d) {
if (m <= 0 || m >= 13) {
return false;
}
if (d <= 0 || d >= 32) {
return false;
}

// Fixing Months
if (m == 4 || m == 6 || m == 9 || m == 11) {
return d <= 30;
}

// checking if leap year(situational)
if (m == 2) {
if (leapYear(y)) {
return d <= 29;

}
else {
return d <= 28;
}

}
// if all of them are okay
return true;

}
int zeller(int y, int m, int d) {

int h, k, j;

if (m > 2)
m = m;
else {
m = m + 12;
y--;
}
k = y % 100;
j = y / 100;
h = (d + ((13 * (m + 1)) / 5) + k + (k / 4) + j / 4 - (2 * j)) % 7;
// Fixing H if its negative:
if (h < 0) {
h += 7;
}


if (h < 0) {
h += 7;
}

switch (h) {
case (0):
cout << "Saturday" << endl;
break;
case (1):
cout << "Sunday" << endl;
break;
case (2):
cout << "Monday" << endl;
break;
case (3):
cout << "Tuesday" << endl;
break;
case (4):
cout << "Wednesday" << endl;
break;
case (5):
cout << "Thursday" << endl;
break;
case (6):
cout << "Friday" << endl;
break;
}
return 0;
}


// Main solution after functions:
int main() {
int yyyy, mm, dd;
cout << "Please enter your birth date (yyyy mm dd): ";
cin >> yyyy >> mm >> dd;

if (inRange(yyyy, mm, dd)== true && valid(yyyy, mm, dd)==true) {
zeller(yyyy, mm, dd);

}
else if ((inRange(yyyy, mm, dd) == false && valid(yyyy, mm, dd)== true)) {
cout << "Out of range" << endl;
}
else {
cout << "Invalid date" << endl;
}

return 0;
}
 
Ad

Advertisements

Joined
Mar 3, 2021
Messages
243
Reaction score
29
Very hard to say without knowing the tests. Spot checking the code, there are two potential problems.

inRange() has code paths that don't return a value. Throw a "return false" at the end there or something.
In zeller(), right inside the first if, you're setting m = m;. Which is either wrong or superfluous.

I'm not familiar with that equation for calculating the day of the week, so I can't really attest to the math. Do you have a source for that? I could verify it if you got it from a site or something.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top