I'm doing a series of excercises to get a better understanding of functions for college. I'm currently doing one now where
"A function NumDays that may be passed two dates in the form dd mm and which will
return the inclusive number of days between the two dates"
I wrote my solution on paper and the result is always correct, I then wrote the code for my solution, went through my code step by step on paper, my result on paper is always correct but the result when I run my code is always 5-25 days off and I've no idea why, It could be the fact that I'm only new to functions so maybe I could of made a mistake on the logic of functions itself or there could be a flaw in my mathematical logic. I will explain my logic below.
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 140 141 142 143 144 145 146 147
|
#include <iostream>
#include <iomanip>
using namespace std;
int Numdays(int dd1, int mm1, int dd2, int mm2);
int main()
{
int result;
int ddOne, mmOne, ddTwo, mmTwo;
cout << "Enter second date in the format dd mm " << endl;
cin >> ddTwo >> mmTwo;
cout << "Enter second date in the format dd mm " << endl;
cin >> ddTwo >> mmTwo;
Numdays(ddOne, mmOne, ddTwo, mmTwo);
result = Numdays(ddOne, mmOne, ddTwo, mmTwo);
cout << "The total days between the two dates = " << result << endl;
return 0;
}
int Numdays(int dd1, int mm1, int dd2, int mm2)
{
int jan = 31, feb = 59, march = 90, april = 120, may = 151, june = 181, july = 212, august = 243,
september = 273, october = 304, november = 334, december = 365;
int monthDaysOne, monthDaysTwo;
int dayInyearOne, dayInyearTwo;
int totalDaysBetween;
if (mm1 == 1 || 3 || 5 || 7 || 8 || 10 || 12)
monthDaysOne = 31 - dd1;
if (mm1 == 4 || 6 || 9 || 11)
monthDaysOne = 30 - dd1;
if (mm1 == 2)
monthDaysOne = 28 - dd1;
switch (mm1)
{
case 1:
dayInyearOne = jan - monthDaysOne;
break;
case 2:
dayInyearOne = feb - monthDaysOne;
break;
case 3:
dayInyearOne = march - monthDaysOne;
break;
case 4:
dayInyearOne = april - monthDaysOne;
break;
case 5:
dayInyearOne = may - monthDaysOne;
break;
case 6:
dayInyearOne = june - monthDaysOne;
break;
case 7:
dayInyearOne = july - monthDaysOne;
break;
case 8:
dayInyearOne = august - monthDaysOne;
break;
case 9:
dayInyearOne = september - monthDaysOne;
break;
case 10:
dayInyearOne = october - monthDaysOne;
break;
case 11:
dayInyearOne = november - monthDaysOne;
break;
case 12:
dayInyearOne = december - monthDaysOne;
break;
default: cout << "invalid date ";
break;
}
if (mm2 == 1 || 3 || 5 || 7 || 8 || 10 || 12)
monthDaysTwo = 31 - dd2;
if (mm2 == 4 || 6 || 9 || 11)
monthDaysTwo = 30 - dd2;
if (mm2 == 2)
monthDaysTwo = 28 - dd2;
switch (mm2)
{
case 1:
dayInyearTwo = jan - monthDaysOne;
break;
case 2:
dayInyearTwo = feb - monthDaysOne;
break;
case 3:
dayInyearTwo = march - monthDaysOne;
break;
case 4:
dayInyearTwo = april - monthDaysOne;
break;
case 5:
dayInyearTwo = may - monthDaysOne;
break;
case 6:
dayInyearTwo = june - monthDaysOne;
break;
case 7:
dayInyearTwo = july - monthDaysOne;
break;
case 8:
dayInyearTwo = august - monthDaysOne;
break;
case 9:
dayInyearTwo = september - monthDaysOne;
break;
case 10:
dayInyearTwo = october - monthDaysOne;
break;
case 11:
dayInyearTwo = november - monthDaysOne;
break;
case 12:
dayInyearTwo = december - monthDaysOne;
break;
default: cout << "invalid date ";
break;
}
if (dayInyearOne > dayInyearTwo)
totalDaysBetween = dayInyearOne - dayInyearTwo;
if (dayInyearOne < dayInyearTwo)
totalDaysBetween = dayInyearTwo - dayInyearOne;
return totalDaysBetween;
}
| |
Basically, Im promting the user to enter in two dates in dd mm format which is then sent to the function.
In the function I asigned each month to the position of the last day within a given month in a 365 day year e.g march = january + febuary + 31 = 90. Another example would be July = Jan + Feb + March + April + May + 31 = 212
Then we test the first dates month to see if it is a 31 day 30 day or 28 day month (we'll assume its not a leap year) and asign the amount of days in the month to a variable
We do the same to the second dates month.
Lets assume we have the date 14 6 and the date 23 8
6 = june which is a 30 day month, 8 is august which is a 31 day month.
We minus the day of the first date away from the amount of days in the month of the first date. 30 - 14 = 16. Same for the second date. 30 - 23 = 7.
We then take 16 from position of the last day in a given month in this case June which is 181 - 16 = 165. This gives us the position of the date on a 365 day calander year.
We do the same for the second date 243 - 7 = 236
We then minus these two numbers to give us the amount of days inbetween the two dates which gives us 236 - 165 = 71 days.
When I enter these same two dates in my programme
"The total days between these two dates = 62"
71 days is the answer, My logic is correct on paper but I cant spot where the flaw is in my code and why its outputting 62.
UPDATE**** It was a simple mistake in the second switch statment in the function. I copied and pasted the first switch statment and forgot to change one of the variables