Hi, I'm writing a code to print weekday (ie: mon, tue, wed, thurs, fri, sat, sun) when user enter day/month/year.
However, I do not know why this code doesn't work right.

I didn't get any error.
It actually works, but it just printed "Monday" for whatever date I putted in.

Could you please help me. I think I nearly get to the end. :)

here is the code:

date.h

#ifndef DATE_H
#define DATE_H

class Date {
public:
    Date (int day, int month, int year);
    Date();
    Date(const Date& orig);
    virtual ~Date();

    int getDay ();
    void setDay (int day);

    int getMonth ();
    void setMonth (int month);

    int getYear ();
    void setYear (int year);

    bool isLeapYear (int year);

    int daysInMonth (int day, int month, int year);
    void advance ();

    bool precedes (Date date);


private:
    int day_;
    int month_;
    int year_;


};

#endif  /* DATE_H */

date.cpp

#include "Date.h"

Date::Date(int day, int month, int year) {
    this->day_ = day;
    this->month_ = month;
    this->year_ = year;
}

int Date::getDay(){
    return this->day_;
}

void Date::setDay(int day){
    this->day_ = day;
}

int Date::getMonth(){
    return this->month_;
}

void Date::setMonth(int month){
   this->month_ = month; 
}

int Date::getYear() {
    return this->year_;
}

void Date::setYear(int year){
    this->year_ = year;
}

bool Date::isLeapYear(int year) {
  if ((year % 400 == 0 && year % 100 != 0) || (year % 4 == 0))
    return false; 
      else return true;
}

  Date::daysInMonth(int day, int month, int year){
      switch (month){
          case 9:   
          case 4:  
          case 6:   
          case 11:  
          return 30;

          default:
              return 31;

          case 2:
              return this-> isLeapYear(year_) ? 29 : 28;
      } 
  }

 void Date::advance() {
      this -> day_;
      if (this->day_ > this->daysInMonth(day_, month_, year_)) {
            this->day_ = 1;
            this->month_++;
        }
        if (this->month_ > 12) {
            this->month_ = 1;
            this->year_++;
        }   
  }

 bool Date::precedes(Date date){
     return this->year_ < date.year_
            || this->year_ == date.year_ && this->month_ < date.month_
            || this->year_ == date.year_ && this->month_ == date.month_ && this->day_ < date.day_;
 }


Date::Date(const Date& orig) {
}

Date::~Date() {
}

main.cpp

#include <cstdlib>
#include <iostream>
#include "Date.h"

using namespace std;

/*
 * 
 */
int main() {
    int day, month, year;
    cout << "What date (d m y)? ";
    cin >> day, month, year;





    const string days[] =  {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Date trial = Date (01,01,1900);
Date d2 = Date (01,01,1899);

    int weekday = 1;



    if (d2.precedes (trial)){

        cout << "Mysteryday" << endl;
    } else {
          while (trial.precedes(d2)) {
                trial.advance();
                weekday = (weekday+1) % 7;
            }

        cout << "That was a " + days [weekday];  

    }
//    return 0;
}

Your copy constructor doesn't do anything meaningful, which means that precedes() will always get a garbage initialized Date object. You also forgot to increment day_ in advance(), which means the loop in main() will run forever.

I find it odd that you say only "Monday" is ever printed when the code you've posted first and foremost doesn't compile, and second, clearly prints "Mysteryday" in all cases. When you fix the copy constructor bug, it's an infinite loop that prints nothing.

Is the code you posted a copy paste from your most current project?

that is the code I am writing, I tried to remove all the errors, but then it didn't work, and I couldn't find how to fix it.
Sorry, I've started C++ for couple of weeks, that's why I'm not very good. :)

Here is some code to convert gregorian (yyyy/mm/dd) dates to julian numbers, and to determine the day-of-week, and name-of-day that you need. I wrote this stuff some 15-20 years ago to run major semiconductor fabs:

typedef uint32 julian_t;
#define FWBAD_DATE (julian_t)UINT_MAX

// --------------------------- Static Data -------------------------
// daysInMonth and firstDayOfMonth are for non-leap-years.
const uint32 _daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};

const uint32 _firstDayOfMonth[] = {1,32,60,91,121,152,182,213,244,274,305,335};

const char* _months[] =
    {
        "January",      "February",     "March",        "April",
        "May",          "June",         "July",         "August",
        "September",    "October",      "November",     "December"
    };

const char* _days[] =
    {
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday"
    };

//
// Convert Gregorian calendar date to the corresponding Julian day
// number j.  Algorithm 199 from Communications of the ACM, Volume 6, No.
// 8, (Aug. 1963), p. 444.  Gregorian calendar started on Sep. 14, 1752.
// This function is not valid before that.
// NOTE: 2 digit years will be scaled according to the specified window and cross-over year
// where the default is <= 69 == 2000, >= 70 == 1900.
// Validated 5-dec-1997, bboyle.
//
julian_t toJulian( uint32 y, uint32 m, uint32 d, uint32 window, uint32 xover )
{
    julian_t retval = FWBAD_DATE;

    // Adjust for 2 digit year according to window provided.
    if (y < 100)
    {
        if (y <= window)
        {
            y += xover;
        }
        else
        {
            y += (xover - 100);
        }
    }
    if (validDayOfMonth(y, m, d))
    {
        register uint32 cc, yy = y, mm = m, dd = d;
        if (mm > 2)
        {
            mm -= 3;
        }
        else
        {
            mm += 9;
            yy--;
        }

        cc = yy / 100;
        yy -= (100 * cc);
        retval = ((146097 * cc) >> 2) + ((1461 * yy) >> 2)
                                      + (((153 * mm) + 2) / 5)
                                      + dd
                                      + 1721119;
    }
    return retval;
}

uint32 month( const FwString& m)
{
    uint32 found = 0;
    if (m != string())
    {
        const char* mstr = m.c_str();
        for (uint32 i = 0; i < 12; i++)
        {
            if (::strncasecmp(mstr, _months[i], strlen(mstr)) == 0)
            {
                // Check for duplicates to filter out invalid abbreviations.
                if (found)
                {
                    // Duplicate found, return invalid value.
                    return 0;
                }
                else
                {
                    found = i + 1;
                }
            }
        }
    }
    return found;
}

uint32 dayOfWeek( const string& d )
{
    uint32 found = 0;
    if (d != string())
    {
        const char* dstr = d.c_str();
        for (uint32 i = 0; i < 7; i++)
        {
            if (::strncasecmp(dstr, _days[i], strlen(dstr)) == 0)
            {
                // Check for duplicates, to filter out invalid abbreviations
                // such as "T" or "S".  "TU", "TH", "SA", or "SU" would be
                // valid as they uniquely identify the day.
                if (found)
                {
                    // Duplicate found, return invalid value.
                    return 0;
                }
                else
                {
                    found = i + 1;
                }
            }
        }
    }
    return found;
}

const char* nameOfDay( uint32 d )
{
    if (validDayOfWeek(d))
    {
        return _days[d-1];
    }
    else
    {
        return "unknown";
    }
}

const char* nameOfMonth( uint32 m )
{
    if (validMonth(m))
    {
        return _months[m-1];
    }
    else
    {
        return "unknown";
    }
}

// Algorithm from K & R, "The C Programming Language", 1st ed.
// Leap year is divisible by 4, but not 3600 or 100 unless divisible by 400.
FwBoolean isLeapYear( uint32 y )
{
    // Check for divisible by 4.
    bool isleap = ((y & 3) == 0);
    if (isleap)
    {
        // Leap if not divisible by 100.
        if ((y % 100) == 0)
        {
            // Leap if divisible by 400 but not 3600.
            isleap = (((y % 400) == 0) && ((y % 3600) != 0)); 
        }
    }
    return isleap;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.