Nested Loop problem

I'm having trouble with this code, the purpose is to take in a list of names with corresponding birthdays and determine which is the closest to the date entered. My plan was to increment the entered day until it was equal to the day of the next birthday, but there seems to be a problem with the while loop. How do I fix this? (note: the code also takes into consideration the leap year)

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

bool leapyear(int year, bool& leap);

int main()
{
	bool leap = false;
	string name[14];
	int bmonth[14], bday[14], byear[14], month, day, year, counter = 0, i = 0;
	char c = '/', q = ':';
	
	ifstream in;
	in.open("birthdays.txt");
	if(in.fail()) {
		cout << "ERROR!" << endl;
		exit(1);
	}
	for(int i=0; i<14; i++) {
		in >> name[i] >> q >> bmonth[i] >> c >> bday[i] >> c >> byear[i];
	}
	cout << "enter the current date: " << endl;
	cin >> month >> c >> day >> c >> year;
	leapyear(year, leap);
	
	
	while(1) {
		i = 0;
		while(i<14) {
			if((day == bday[i]) && (month == bmonth[i])) {
			cout << "Next birthday: " << endl;
			cout << "in " << counter << " days" << endl;
			cout << "will turn " << (year-byear[i]) << endl;
			break;
			}
			i++;
		}
		if((day == bday[i]) && (month == bmonth[i])) {
			break;
		}
			day++;
			counter++;
				if((month = 1 || 3 || 5 || 7 || 8 || 10) && (day = 32)) {
					day = 1;
					month++;
				} else
				if((month = 12) && (day = 32)) {
					day = 1;
					month = 1;
					year++;
					leapyear(year, leap);
				} else
				if((month = 4 || 6 || 9 || 11) && (day = 31)) {
					day = 1;
					month++;
				} else
				if((leap = true) && (month = 2) && (day = 30)) {
					day = 1;
					month++;
				}
				 else if((leap = false) && (month = 2) && (day = 29)) {
					day = 1;
					month++;
				}
			}
	return 0;
}

bool leapyear(int year, bool& leap) {
	if(year%4 == 0) {
		return true;
		if(year%100 == 0) {
			 return false;
			if(year%400 == 0) {
				return true;
			}
		}
	}
	else { 
		return false;
	}
}
Hello BoomMan,

Welcome to the forum and thank you for using code tags.

It would help to know what the input file looks like. Either the whole file or at least a good sample so everyone knows what you are working with along with a sample date that you enter and what output you expect. All helps to track down what is going wrong.

You say you are having a problem with a while loop, but which one? I am guessing you mean the second while loop.

I will load up the program and see what I can figure out.

Andy
The txt file looks as follows:

Nick_Florin : 11/7/2000
David_Woodhouse : 8/12/1990
Michael_Smith : 11/13/1949
Mary_Rasmussen : 1/13/1987
Martin_Hughes : 3/13/1976
Melanie_Mouzon : 5/15/1933
Christine_Bonin : 2/10/1945
William_Holland : 6/13/1999
Doyle_Dye : 6/14/2014
Steve_Burkey_Frazier : 3/13/1972
Nell_Granberry : 4/25/1979
Madeleine_Daniel : 7/9/1968
Lillie_Callender : 6/16/1930
Shoshana_Falls : 5/27/1959

I have tested to make sure the values read in correctly so that doesn't seem to be the problem

side note: if I input a date that is the same date of a birthday listed above it outputs correctly
Hello BoomMan,

Thank you this makes testing easier.

Two things I noticed:

Good job on testing to see if the file is open.

When dealing with a file that did not open you might find this code useful:
1
2
3
4
5
6
	if (in.fail())
	{
		std::cout << "ERROR!" << std::endl;
		std::this_thread::sleep_for(std::chrono::seconds(3));  // Requires header files "chrono" and "thread"
		exit(1);
	}

The middle line will pause the program long enough to read the message before the program exits. I would consider something more then just "ERROR!" for the message. I generally say what file did not open before the program ends. It still comes down to your choice of what to do.

Off to testing.

Hope that helps,

Andy
Hello BoomMan,

When I started on the program I noticed the function "bool leapyear(int year, bool& leap)" is not quite right.

First the function returns a bool that is never used and the second parameter is never used. I am not sure if your if statements will actually work, but you may like this better:
1
2
3
4
5
6
7
bool leapyear(int year)
{
	if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0))
		return true;

	return false;
}
And your function call would be leap = leapyear(year);

As for the while loops my first thought is the way you are going about your logic. One question I had is how far after the entered date do you want to search for the next birthday? Weeks, months or until the end of the year?

This part will take some thought. I will work on it as quickly as I can.

Hope that helps,

Andy
I have fixed the code and it is now working properly

There was an issue with the if statements and all I had to was change the equal signs to greater than signs and change some of the values
Hello BoomMan,

Glad you got it working. I came to the same conclusion and was about to get started on figuring it out when I was interrupted.

Andy
Topic archived. No new replies allowed.