Calculate # of days between dates - program continuously runs

Mar 24, 2014 at 8:00pm
I have an assignment to write a program that will calculate the number of days between two dates (including leap years). We have to read the data from a file and in my current input file I have:

1 1 2000 1 5 2004
5 2 2005 2 5 2009
1 15 1998 1 15 2001

for testing purposes. When I run the program the console runs continuously spouting out "The days between your two dates are 1097." Which would be the correct output for the last set of dates entered, but I need to find out where I've messed up in my main.

Thanks for any help.

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
#include <iostream>
#include <fstream>
using namespace std;

bool isLeap(int x)
{
	if (x % 400 == 0)
	{
		return true;
	}
	else if (x % 4 == 0 && x % 100 != 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int daysinyear(int year)
{
	if (isLeap(year))
		return 366;
	else 
		return 365;
}

int daysinmonth(int month, int y)
{
	switch (month)
	{
	case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;
	case 4: case 6: case 9: case 11: return 30;
	case 2: if (isLeap(y))
				return 29;
			else
				return 28;
	default: cout << "That's not a valid month." << endl;
	}
	return 0;
}

int main()
{
	ifstream inputData;
	inputData.open("Input.txt");
	int month_1, month_2, day_1, day_2, year_1, year_2, daysbetween(0);
	inputData >> month_1 >> day_1 >> year_1 >> month_2 >> day_2 >> year_2;
	while(month_1!=-1)
	{
		if (year_1 == year_2 && month_1 == month_2)
		{
			daysbetween = (day_2 - day_1) + 1;
		}
		else if (year_1 == year_2)
		{
			daysbetween = daysinmonth(month_1, year_1) - day_1 + 1;
			for (int i = month_1 + i; i < month_2; ++i)
			{
				daysbetween += daysinmonth(i, year_1);
			}
			daysbetween += day_2;
		}
		else
		{
			daysbetween = daysinmonth(month_1, year_1) - day_1 +1;
			for (int i = month_1 + 1; i <= 12; i++)
			{
				daysbetween += daysinmonth(i, year_1);
			}
			for (int i = (year_1 + 1); i < year_2; i++)
			{
				daysbetween += daysinyear(i);
			}
			for (int i = 1; i < month_2; i++)
			{
				daysbetween += daysinmonth(i, year_2);
			}
			daysbetween += day_2;
		}
		cout << "The days between your two dates are " << daysbetween << "." << endl;
		inputData >> month_1 >> day_1 >> year_1 >> month_2 >> day_2 >> year_2;
	}
	return 0;
}



Mar 24, 2014 at 8:56pm
At line 50:

 
while(month_1!=-1)


you need a month_1==-1 in order for the program to halt. Since the last line is not -1 in your file it just keeps spitting out the last result. You need an input file that looks something like this:


1 1 2000 1 5 2004
5 2 2005 2 5 2009
1 15 1998 1 15 2001
-1 -1 -1 -1 -1 -1
Last edited on Mar 24, 2014 at 8:59pm
Mar 24, 2014 at 9:03pm
Oh! Duh! That did it, thanks a lot.
Mar 24, 2014 at 9:41pm
I've actually got another error now.

The program was working fine will all the dates I entered but when I use this input:

7 4 1776 1 1 1987
2 1 1983 3 15 1984
7 7 1983 9 4 1983
7 3 1983 7 20 1983
12 25 1990 12 31 1992
-1 -1 -1 -1 -1 -1

I'm getting problems.

The date that seems to be giving me the problems is:
7 7 1982 9 4 1983

When I enter 9 4 1984 instead of 9 4 1983 it proceeds to work again.

The console will read out That's not a valid month over and over.

I'm getting these error messages with Microsoft Visual 2010:

'Project 3.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'Project 3.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'Project 3.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'Project 3.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Symbols loaded.
'Project 3.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
Run-Time Check Failure #3 - The variable 'i' is being used without being initialized.
The thread 'Win32 Thread' (0x1b08) has exited with code -1073741749 (0xc000004b).
The program '[5264] Project 3.exe: Native' has exited with code -1073741510 (0xc000013a).
Mar 24, 2014 at 10:15pm
Check out line 59:

 
for (int i = month_1 + i; i < month_2; ++i)


i think you meant

 
for (int i = month_1 + 1; i < month_2; ++i)


month_1 + 1 and not month_1 + i

Only enters that section when the years are equal, that's why it never showed up before.
Last edited on Mar 24, 2014 at 10:20pm
Mar 24, 2014 at 10:18pm
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
#include <iostream>
#include <fstream>
using namespace std;

bool isLeap(int x)
{
	if (x % 400 == 0)
	{
		return true;
	}
	else if (x % 4 == 0 && x % 100 != 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

int daysinyear(int year)
{
	if (isLeap(year))
		return 366;
	else 
		return 365;
}

int daysinmonth(int month, int y)
{
	switch (month)
	{
	case 1: case 3: case 5: case 7: case 8: case 10: case 12: return 31;
	case 4: case 6: case 9: case 11: return 30;
	case 2: if (isLeap(y))
				return 29;
			else
				return 28;
	default: cout << "That's not a valid month." << endl;
	}
	return 0;
}

int main()
{
	ifstream inputData;
	inputData.open("Input.txt");
	int month_1, month_2, day_1, day_2, year_1, year_2, daysbetween(0);
	inputData >> month_1 >> day_1 >> year_1 >> month_2 >> day_2 >> year_2;
	while(month_1!=-1)
	{
		if (year_1 == year_2 && month_1 == month_2)
		{
			daysbetween = (day_2 - day_1) + 1;
		}
		else if (year_1 == year_2)
		{
			daysbetween = daysinmonth(month_1, year_1) - day_1 + 1;
			for (int i = month_1 + i; i < month_2; ++i)
			{
				daysbetween += daysinmonth(i, year_1);
			}
			daysbetween += day_2;
		}
		else
		{
			daysbetween = daysinmonth(month_1, year_1) - day_1 +1;
			for (int i = month_1 + 1; i <= 12; i++)
			{
				daysbetween += daysinmonth(i, year_1);
			}
			for (int i = (year_1 + 1); i < year_2; i++)
			{
				daysbetween += daysinyear(i);
			}
			for (int i = 1; i < month_2; i++)
			{
				daysbetween += daysinmonth(i, year_2);
			}
			daysbetween += day_2;
		}
		cout << "The days between your two dates are " << daysbetween << "." << endl;
		inputData >> month_1 >> day_1 >> year_1 >> month_2 >> day_2 >> year_2;
	}
	system("pause");
}


I'm pretty sure nothing has changed. VS has given me strange unexplainable errors in the past and I don't know if this is another weird problem.
Mar 24, 2014 at 10:26pm
I found it, this is a copy of the above one since I don't think you get a notice if just edit my reply.

Check out line 59:


for (int i = month_1 + i; i < month_2; ++i)


i think you meant


for (int i = month_1 + 1; i < month_2; ++i)


month_1 + 1 and not month_1 + i

Only enters that section when the years are equal, that's why it never showed up before.
Mar 24, 2014 at 10:48pm
Aha! That did the trick! Thanks so much, you were very helpful!
Topic archived. No new replies allowed.