Click here to Skip to main content
15,311,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This code used to work back in 1999. I'm currently using Microsoft Visual Studio, and this code is showing a number of errors. Kindly help me fix it, while keeping as close as possible to the original code.
C++
#include <iostream>
using namespace std;

class date {

protected:

    int month;                  // 1 through 12
    int day;                    // 1 through max_days
    int year;                   // 1500 through 2200
    static char out_string[25]; // Format output area
    static char format;         // Format to use for output

          // Calculate how many days are in any given month
          // Note - This is a private method which can be called only
          //        from within the class itself
    int days_this_month(void);

public:

    // Constructor - Set the date to the current date and set
    //               the format to 1
    date(void);

    // Set the date to these input parameters
    //  if return = 0 ---> All data is valid
    //  if return = 1 ---> Something out of range
    int set_date(int in_month, int in_day, int in_year);

    // Get the month, day, or year of the stored date
    int get_month(void) { return month; };
    int get_day(void) { return day; };
    int get_year(void) { return year; };

    // Select the desired string output format for use when the
    //  get_date_string is called
    void set_date_format(int format_in) { format = format_in; };

    // Return an ASCII-Z string depending on the stored format
    //   format = 1    Aug 29, 1991
    //   format = 2    8/29/91
    //   format = 3    8/29/1991
    //   format = 4    29 Aug 1991    Military time
    //   format = ?    Anything else defaults to format 1
    char* get_date_string(void);

    // Return Jan Feb Mar Apr etc.
    char* get_month_string(void);
};

class time_of_day
{
protected:
    int hour;                   // 0 through 23
    int minute;                 // 0 through 59
    int second;                 // 0 through 59
    static char format;         // Format to use for output
    static char out_string[25]; // Format output area

public:
    // Constructor - Set time to current time and format to 1
    time_of_day(void);
    time_of_day(int H) { hour = H; minute = 0; second = 0; };
    time_of_day(int H, int M) { hour = H; minute = M; second = 0; };
    time_of_day(int H, int M, int S) {
        hour = H;
        minute = M; second = S;
    };

    // Set the time to these input values
    //  return = 0 ---> data is valid
    //  return = 1 ---> something is out of range
    int set_time(void);
    int set_time(int hour_in);
    int set_time(int hour_in, int minute_in);
    int set_time(int hour_in, int minute_in, int second_in);

    // Select string output format
    void set_time_format(int format_in) { format = format_in; };

    // Return an ASCII-Z string depending on the stored format
    //   format = 1    13:23:12
    //   format = 2    13:23
    //   format = 3     1:23 PM
    char* get_time_string(void);

};

char date::format;         // This defines the static data member
char date::out_string[25]; // This defines the static string

         // Constructor - Set date to current date, and
         //               set format to the default of 1
date::date(void)
{
    time_t time_date;
    struct tm* current_date;

    time_date = time(NULL);                // DOS system call
    current_date = localtime(&time_date);  // DOS system call
    month = current_date->tm_mon + 1;
    day = current_date->tm_mday;
    year = current_date->tm_year + 1900;
    format = 1;
}


// Set the date to these input parameters
//  if return = 0 ---> All data is valid
//  if return = 1 ---> Something out of range
int date::set_date(int in_month, int in_day, int in_year)
{
    int temp = 0;
    int max_days;
    // The limits on the year are purely arbitrary
    if (in_year < 1500)             // Check that the year is between
    {
        year = 1500;                 //  1500 and 2200
        temp = 1;
    }
    else
    {
        if (in_year > 2200)
        {
            year = 2200;
            temp = 1;
        }
        else
        {
            year = in_year;
        }
    }

    if (in_month < 1)                // Check that the month is between
    {                               //  1 and 12
        month = temp = 1;
    }
    else
    {
        if (in_month > 12)
        {
            month = 12;
            temp = 1;
        }
        else
        {
            month = in_month;
        }
    }

    max_days = days_this_month();
    if (in_day < 1)                 // Check that the day is between
    {                               //  1 and max_days
        day = temp = 1;
    }
    else
    {
        if (in_day > max_days)
        {
            day = max_days;
            temp = 1;
        }
        else
        {
            day = in_day;
        }
    }

    return temp;
}


static char* month_string[13] = { " ", "Jan", "Feb", "Mar", "Apr",
                                      "May", "Jun", "Jul", "Aug",
                                      "Sep", "Oct", "Nov", "Dec" };

// Return Jan Feb Mar Apr etc.
char* date::get_month_string(void)
{
    return month_string[month];
}


// Return an ASCII-Z string depending on the stored format
//   format = 1    Aug 29, 1991
//   format = 2    8/29/91
//   format = 3    8/29/1991
//   format = 4    29 Aug 1991    Military time
//   format = ?    Anything else defaults to format 1
char* date::get_date_string(void)
{
    switch (format)
    {
        // This printout assumes that the year will be
        //  between 1900 and 1999
    case 2: sprintf(out_string, "%02d/%02d/%02d",
        month, day, year - 1900);
        break;

    case 3: sprintf(out_string, "%02d/%02d/%04d",
        month, day, year);
        break;

    case 4: sprintf(out_string, "%d %s %04d",
        day, month_string[month], year);
        break;

    case 1: // Fall through to the default case
    default: sprintf(out_string, "%s %d, %04d",
        month_string[month], day, year);
        break;
    }
    return out_string;
}


int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// Since this is declared in the private part of the class
//  header is is only available for use within the class.
//  It is hidden from use outside of the class.
int date::days_this_month(void)
{
    if (month != 2)
        return days[month];

    if (year % 4)       // Not leap year
        return 28;
    if (year % 100)     // It is leap year
        return 29;
    if (year % 400)     // Not leap year
        return 28;
    return 29;          // It is leap year
}


int main(void)
{
    date today, birthday;

    birthday.set_date(7, 21, 1960);
    cout << "Limited test of the date class\n";
    cout << "Today is " << today.get_date_string() << "\n";
    cout << "Birthday is " << birthday.get_date_string() << "\n";

    today.set_date_format(4);
    cout << "Today is " << today.get_date_string() << "\n";
    cout << "Birthday is " << birthday.get_date_string() << "\n";

    return 0;
}


What I have tried:

This is slightly above my comprehension, and I haven't been in touch with the changes in C++ since that time.
These are the errors I get when trying to compile the code:
Severity	Code	Description	Project	File	Line	Suppression State
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	175	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	175	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	175	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	175	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	176	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	176	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	176	
Error (active)	E0144	a value of type "const char *" cannot be used to initialize an entity of type "char *"	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	176	
Error	C4996	'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	100	
Error	C2440	'initializing': cannot convert from 'const char [2]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error	C2440	'initializing': cannot convert from 'const char [4]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error	C2440	'initializing': cannot convert from 'const char [4]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error	C2440	'initializing': cannot convert from 'const char [4]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error	C2440	'initializing': cannot convert from 'const char [4]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	174	
Error	C2440	'initializing': cannot convert from 'const char [4]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	175	
Error	C2440	'initializing': cannot convert from 'const char [4]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	175	
Error	C2440	'initializing': cannot convert from 'const char [4]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	175	
Error	C2440	'initializing': cannot convert from 'const char [4]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	175	
Error	C2440	'initializing': cannot convert from 'const char [4]' to 'char *'	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	176	
Error	C4996	'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	198	
Error	C4996	'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	202	
Error	C4996	'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	206	
Error	C4996	'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.	ConsoleApplication1	C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp	211	
Posted
Updated 7-Dec-21 5:58am
v3
Comments
jeron1 5-Dec-21 19:23pm
   
" a number of errors"

...and they would be?
Baz19 6-Dec-21 9:36am
   
30 errors when compiling, this is after I made the two changes suggested by Rick York. I updated the question with the list of errors I get when compiling the original code.
Severity Code Description Project File Line Suppression State
Error (active) E0147 declaration is incompatible with "int date::format" (declared at line 12) ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 89
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 174
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 174
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 174
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 174
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 174
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 175
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 175
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 175
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 175
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 176
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 176
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 176
Error (active) E0144 a value of type "const char *" cannot be used to initialize an entity of type "char *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 176
Error C2371 'format': redefinition; different basic types ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 89
Error C4996 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 100
Error C2440 'initializing': cannot convert from 'const char [2]' to 'char *' ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 174
Error C2440 'initializing': cannot convert from 'const char [4]' to 'char *' ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 174
Error C2440 'initializing': cannot convert from 'const char [4]' to 'char *' ConsoleApplication1 C:\Users\
Richard MacCutchan 6-Dec-21 10:40am
   
Those errors indicate that you are trying to store a constant in a variable that does not include the const declaration. The compiler is trying to protect you from yourself. So go to the lines indicated by the error messages and change the declarations so both halves match.
Baz19 6-Dec-21 11:10am
   
So, do I keep the changes Rick's suggested or not?
Richard MacCutchan 6-Dec-21 11:36am
   
Yes, you should not try to store an int value (32 or 64 bits wide) into a char variable which is only 8 bits wide.
Baz19 6-Dec-21 13:44pm
   
I seem to have made it worse. Could you kindly provide me with the correct code, so I could study it?
Richard MacCutchan 7-Dec-21 3:22am
   
Correct code for what?
Baz19 7-Dec-21 10:17am
   
I need a working program so I can study it. There are too many errors showing that I can't fix on my own. Thank you for your time and patience.
Baz19 8-Dec-21 18:02pm
   
Thank you for your help. I managed to get it down to one error, but there is something missing. Here is the part I suspect is causing the problem:
date::date(void)
{
time_t time_date;
struct tm* current_date;

time_date = time(NULL); // DOS system call
errno_t terror = localtime_s(&time_date); // *****DOS system call
month = current_date->tm_mon + 1;
day = current_date->tm_mday;
year = current_date->tm_year + 1900;
format = 1;
}
And the errors that show up are these:
Severity Code Description Project File Line Suppression State
Error (active) E0167 argument of type "time_t *" is incompatible with parameter of type "tm *" ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 100
Error (active) E0165 too few arguments in function call ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 100
Error C2660 'localtime_s': function does not take 1 arguments ConsoleApplication1 C:\Users\bazki\Downloads\C++\ConsoleApplication1\ConsoleApplication1.cpp 100
Richard MacCutchan 9-Dec-21 4:24am
   
My solution below compiles and runs with no errors. Your version of date::date is incorrect.

And please remove those comments that state "DOS system call", there is no such thing. They are all Windows Run-time Library calls.

I use Visual Studio 2019 in 64-bit mode and the only thing it complained about was converting an int to a char when setting the formats so I made that variable (format) an int in both classes and it compiles and runs without errors.
   
Comments
Baz19 6-Dec-21 9:39am
   
I made the two changes, but the program still doesn't compile correctly. I updated my question with the list of errors I get after changing the code to what you suggested.
Following the error messages, and checking the documentation, none of this was too difficult.
C++
#include <iostream>
using namespace std;

class date {

protected:

    int month;                  // 1 through 12
    int day;                    // 1 through max_days
    int year;                   // 1500 through 2200
    static char out_string[25]; // Format output area
    static int format;         // Format to use for output *****changed to int type

          // Calculate how many days are in any given month
          // Note - This is a private method which can be called only
          //        from within the class itself
    int days_this_month(void);

public:

    // Constructor - Set the date to the current date and set
    //               the format to 1
    date(void);

    // Set the date to these input parameters
    //  if return = 0 ---> All data is valid
    //  if return = 1 ---> Something out of range
    int set_date(int in_month, int in_day, int in_year);

    // Get the month, day, or year of the stored date
    int get_month(void) { return month; };
    int get_day(void) { return day; };
    int get_year(void) { return year; };

    // Select the desired string output format for use when the
    //  get_date_string is called
    void set_date_format(int format_in) { format = format_in; };

    // Return an ASCII-Z string depending on the stored format
    //   format = 1    Aug 29, 1991
    //   format = 2    8/29/91
    //   format = 3    8/29/1991
    //   format = 4    29 Aug 1991    Military time
    //   format = ?    Anything else defaults to format 1
    char* get_date_string(void);

    // Return Jan Feb Mar Apr etc.
    const char* get_month_string(void); // ***** make this const
};

class time_of_day
{
protected:
    int hour;                   // 0 through 23
    int minute;                 // 0 through 59
    int second;                 // 0 through 59
    static int format;         // Format to use for output
    static char out_string[25]; // Format output area

public:
    // Constructor - Set time to current time and format to 1
    time_of_day(void);
    time_of_day(int H) { hour = H; minute = 0; second = 0; };
    time_of_day(int H, int M) { hour = H; minute = M; second = 0; };
    time_of_day(int H, int M, int S) {
        hour = H;
        minute = M; second = S;
    };

    // Set the time to these input values
    //  return = 0 ---> data is valid
    //  return = 1 ---> something is out of range
    int set_time(void);
    int set_time(int hour_in);
    int set_time(int hour_in, int minute_in);
    int set_time(int hour_in, int minute_in, int second_in);

    // Select string output format
    void set_time_format(int format_in) { format = format_in; };

    // Return an ASCII-Z string depending on the stored format
    //   format = 1    13:23:12
    //   format = 2    13:23
    //   format = 3     1:23 PM
    char* get_time_string(void);

};

int date::format;         // This defines the static data member ***** make this int type
char date::out_string[25]; // This defines the static string

         // Constructor - Set date to current date, and
         //               set format to the default of 1
date::date(void)
{
    time_t time_date;
    struct tm current_date; // ***** a local variable for localtime_s

    time_date = time(NULL);                // system call
    errno_t terror = localtime_s(&current_date, &time_date);  // ***** change this to localtime_s
    month = current_date.tm_mon + 1;
    day = current_date.tm_mday;
    year = current_date.tm_year + 1900;
    format = 1;
}


// Set the date to these input parameters
//  if return = 0 ---> All data is valid
//  if return = 1 ---> Something out of range
int date::set_date(int in_month, int in_day, int in_year)
{
    int temp = 0;
    int max_days;
    // The limits on the year are purely arbitrary
    if (in_year < 1500)             // Check that the year is between
    {
        year = 1500;                 //  1500 and 2200
        temp = 1;
    }
    else
    {
        if (in_year > 2200)
        {
            year = 2200;
            temp = 1;
        }
        else
        {
            year = in_year;
        }
    }

    if (in_month < 1)                // Check that the month is between
    {                               //  1 and 12
        month = temp = 1;
    }
    else
    {
        if (in_month > 12)
        {
            month = 12;
            temp = 1;
        }
        else
        {
            month = in_month;
        }
    }

    max_days = days_this_month();
    if (in_day < 1)                 // Check that the day is between
    {                               //  1 and max_days
        day = temp = 1;
    }
    else
    {
        if (in_day > max_days)
        {
            day = max_days;
            temp = 1;
        }
        else
        {
            day = in_day;
        }
    }

    return temp;
}


static const char* month_string[13] = { " ", "Jan", "Feb", "Mar", "Apr", // ***** make this const
                                      "May", "Jun", "Jul", "Aug",
                                      "Sep", "Oct", "Nov", "Dec" };

// Return Jan Feb Mar Apr etc.
const char* date::get_month_string(void) // ***** make this const
{
    return month_string[month];
}


// Return an ASCII-Z string depending on the stored format
//   format = 1    Aug 29, 1991
//   format = 2    8/29/91
//   format = 3    8/29/1991
//   format = 4    29 Aug 1991    Military time
//   format = ?    Anything else defaults to format 1
char* date::get_date_string(void)
{
    switch (format)
    {
        // This printout assumes that the year will be
        //  between 1900 and 1999
    case 2: sprintf_s(out_string, "%02d/%02d/%02d", // change all sprintf calls to sprintf_s
        month, day, year - 1900);
        break;

    case 3: sprintf_s(out_string, "%02d/%02d/%04d",
        month, day, year);
        break;

    case 4: sprintf_s(out_string, "%d %s %04d",
        day, month_string[month], year);
        break;

    case 1: // Fall through to the default case
    default: sprintf_s(out_string, "%s %d, %04d",
        month_string[month], day, year);
        break;
    }
    return out_string;
}


int days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// Since this is declared in the private part of the class
//  header is is only available for use within the class.
//  It is hidden from use outside of the class.
int date::days_this_month(void)
{
    if (month != 2)
        return days[month];

    if (year % 4)       // Not leap year
        return 28;
    if (year % 100)     // It is leap year
        return 29;
    if (year % 400)     // Not leap year
        return 28;
    return 29;          // It is leap year
}


int main(void)
{
    date today, birthday;

    birthday.set_date(7, 21, 1960);
    cout << "Limited test of the date class\n";
    cout << "Today is " << today.get_date_string() << "\n";
    cout << "Birthday is " << birthday.get_date_string() << "\n";

    today.set_date_format(4);
    cout << "Today is " << today.get_date_string() << "\n";
    cout << "Birthday is " << birthday.get_date_string() << "\n";

    return 0;
}
   
v5
Too lazy to check Richards solution, but a quick check showed me there's only three lines of code to fix.

I'll give you a hint how to do this on your own: the trick is not to get intimidated by a long list of errors: most likely many of those errors are just a consequence of the compiler losing track of the intended code due to an early misinterpretation.

Step 1: go to VisualStudios output window, then search for the first occurrence of the string "error". In this case, the message points to line 173. Check the error message, and fix this error.

Then check again: compile and go to the output window. Now you suddenly only get one error, in line 180. Read the message, and fix this error (hint: the error is not in this line).

If you only fixed one line in the previous step, the next step will show you there's still one more error to fix.

If you followed these steps, you should have changed lines 48, 173, and 178. And now the program runs.
   
Comments
Richard MacCutchan 7-Dec-21 12:09pm
   
There were actually 12 lines that needed changing. The addition of const only fixed some of the errors.
Stefan_Lang 7-Dec-21 12:30pm
   
Ok, admittedly I didn't use VS - godbolt is so much faster. Unfortunately it doesn't support the MS compilers, but I hope my explanation is still somewhat useful.
Richard MacCutchan 7-Dec-21 12:55pm
   
Never heard of godbolt; I'll check it out. Yes, obviously your explanation is useful.
Baz19 9-Dec-21 9:39am
   
Thank you for your help. I managed to narrow the errors down to one, and Richard's code compiled without errors.
He made some changes to the code though, like using localtime_s() instead of localtime(), but I guess that's due to the changes that accrued over time.
This code used to work like it is back in 1999.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900