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)
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.
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.
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:
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.