If I input 06/02/20 (no movies on my list for this date) as my starting date (a), I won't get anything printed on the screen. |
Right, I think I understand. So the (data != a) branch isn't being hit, and there's nothing else happened in your while loop besides the date being updated, so if the date is never equal to a, it won't go into the the inner loop (line 39).
I think a different approach should be taken.
You get the start date and the end date from the user. That's fine, but as I said earlier, you need some way to
compare dates for more than just equality to properly start/end your iteration.
I can't tell if your date format is MM/DD/YY or DD/MM/YY, but either way it isn't good for comparison. For example, 121201 (December 12, 2001) would be "greater than" 010120 (January 1st 2020) in this format.
What I would do is convert the date strings into formats that can compared using < and > (less than, greater than).
So for example, if your date is 25/12/20, this should be converted into 201225 (Year, Month, Day).
This way, the string "201225" will compare as being greater than the string "191225" for purposes of comparing against the start and end dates.
After you do the proper conversion, your loop can be something similar to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
start_date = /* get start date */
convert_to_YYMMDD(start_date)
end_date = /* get end date */
convert_to_YYMMDD(end_date)
while (read next movie from file)
{
convert_to_YYMMDD(movie_date)
if (movie_date >= start_date && movie_date <= end_date)
{
// print movie
}
}
|
At this point, the challenge becomes properly converting DD/MM/YY format to YYMMDD. This requires some parsing of strings. (Alternatively, a "Date" class could be made, with integer fields for year, month, and day, and comparison operators).
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
|
// Example program
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
// converts "DD/MM/YY" to "YYDDMM" to allow proper string comparison
// (note that input must contain the slashes)
// No error handling is done here... feel free to add some
string convert_slashed_DDMMYY_to_YYMMDD(const string& date)
{
istringstream iss(date);
string day;
getline(iss, day, '/');
string month;
getline(iss, month, '/');
string year;
getline(iss, year, '/');
// reorder (with slashes now removed):
return year + month + day;
}
int main()
{
std::cout << convert_slashed_DDMMYY_to_YYMMDD("25/12/20") << '\n';
}
|