Friday the 13th
List all the Friday 13ths in a given range, printed to a file.
This was a challenge on Program52 with a simple executable:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
for (int year = 0001; year <= 9999; ++year)
{
for (int month = 0; month < 12; ++month)
{
tm time_in = {0, 0, 0, 13, month, year - 1900}; // 13th day
time_t time_temp = mktime(&time_in);
tm *time_out = localtime(&time_temp);
if (time_out->tm_wday == 5)
{ //-------------------------------[This prints 17199 line file]
cout << put_time(time_out, "%Y-%m-%d") << endl;
}
}
}
return 0;
}
---------------------------------------------------------------------------
Then I made it into a usable program:
// sample program to list Friday the 13th dates in a given range of years
//outputs to default file "friday13.list" or with the -f param, your own outfile name
#include <iostream>
#include <iomanip>
#include <ctime>
#include <iostream>
#include <string>
#include <fstream>
#include <unistd.h>
using namespace std;
int dcount = 0;
string outputfile = "";
// HELP SECTION -----------------------------------------------------------|80
void printHelpMessage()
{
cout << "Usage: ./friday13 [OPTIONS]\n";
cout << "Options:\n";
cout << " -h --help Display this help message\n";
cout << " -v --version Display program version\n";
cout << " -f --output FILENAME Specify an output file\n";
}
bool GetDates(int start, int end, string outputfile)
{
if( start > end)
{
cout << "Starting year must be sooner than ending year.\n";
return false;
}
fstream filebuffer;
filebuffer.open(outputfile, ios::out | ios::in | ios::trunc);
if (!filebuffer.is_open())
{
cout << "Error: Unable to open " << outputfile << "!\n";
return -1;
}
else
{
cout << "\nSuccessfully completed. Saved to: " << outputfile << " \n";
}
for (int year = start; year <= end; ++year)
{
for (int month = 0; month < 12; ++month)
{
tm time_in = {0, 0, 0, 13, month, year - 1900}; // 13th day
time_t time_temp = mktime(&time_in);
tm *time_out = localtime(&time_temp);
if (time_out->tm_wday == 5)
{
dcount++;
filebuffer << put_time(time_out, "%Y-%m-%d") << endl;
}
}
}
return true;
}
int main(int argc, char* argv[])
{
try
{
for (int i = 1; i < argc; ++i)
{
string arg = argv[i];
if (arg == "--help" || arg == "-h")
{
printHelpMessage();
return 0; // Exit after displaying help
}
else if (arg == "--version" || arg == "-v")
{
cout << "friday13 - version 1.0 (c)2026 RMSYS \n";// << leaf << " \n";
return 0;
}
else if (arg == "--output" || arg == "-f")
{
if (i + 1 < argc)
{ // Check if there's a next argument for the filename
outputfile = argv[++i];// can be accessed later
cout << "Output file specified: " << outputfile << "\n";
}
else
{
cerr << "Error: --output requires a filename.\n";
return -1; // Indicate error -1
}
}
else
{
cerr << "Unknown argument: " << arg << "\n";
cerr << "Use --help for usage information.\n";
return -2; // Indicate error -2
}
}
if(outputfile == "")
{
outputfile = "friday13.list";
}
int start, end;
cout << "\nYou can enter one year, or a range of years. If you want just one year, enter it for start and end.\n";
cout << "For a Starting year : ____\b\b\b\b";
cin >> start;
cout << "For and Ending year : ____\b\b\b\b";
cin >> end;
if(GetDates(start, end, outputfile))
{
cout << "There were " << dcount << " dates found.";
cout << "\nCheck for file named " << outputfile << ".\n";
}
else
{
cout << "Please rerun program, in the correct year order.\n";
}
}//end of try{ }
catch (const exception& e)
{
cout << "Exception: " << e.what() << "\n";
cout << "Exited program gracefully...\n";
return -1;
}
return 0;
}//end of main()
OUTPUT >> using [start 2020 end 2030]
2020-03-13
2020-11-13
2021-08-13
2022-05-13
2023-01-13
2023-10-13
2024-09-13
2024-12-13
2025-06-13
2026-02-13
2026-03-13
2026-11-13
2027-08-13
2028-10-13
2029-04-13
2029-07-13
2030-09-13
2030-12-13
