5 Replies - 286 Views - Last Post: 10 August 2012 - 07:45 AM Rate Topic: -----

#1 monkey1604  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-August 12

print weekday in C++ problem

Posted 09 August 2012 - 11:28 PM

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.

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

My code will include 3 files:

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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: print weekday in C++ problem

#2 GunnerInc  Icon User is online

  • "Hurry up and wait"
  • member icon




Reputation: 718
  • View blog
  • Posts: 1,975
  • Joined: 28-March 11

Re: print weekday in C++ problem

Posted 10 August 2012 - 12:05 AM

Quote

However, I do not know why this code doesn't work.

What doesn't work? You need to be more specific.

What error messages are you getting? Post the errors here exactly as the compiler prints them out.
Was This Post Helpful? 0
  • +
  • -

#3 monkey1604  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-August 12

Re: print weekday in C++ problem

Posted 10 August 2012 - 12:12 AM

View PostGunnerInc, on 10 August 2012 - 12:05 AM, said:

Quote

However, I do not know why this code doesn't work.

What doesn't work? You need to be more specific.

What error messages are you getting? Post the errors here exactly as the compiler prints them out.


sorry for that. :)

I didn't get any error.
It actually works, but it just printed "Monday" for whatever date I putted in.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1887
  • View blog
  • Posts: 5,676
  • Joined: 05-May 12

Re: print weekday in C++ problem

Posted 10 August 2012 - 01:07 AM

Your d2 is always hard coded to Date (01,01,1899) on line 22 of main.cpp so your input doesn't matter.

This post has been edited by Skydiver: 10 August 2012 - 01:07 AM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4879
  • View blog
  • Posts: 11,270
  • Joined: 16-October 07

Re: print weekday in C++ problem

Posted 10 August 2012 - 03:57 AM

Your class declaration has some questionable content:

class Date {
public:
    Date (int day, int month, int year);
    Date();
    Date(const Date& orig);
    // why?
    // virtual ~Date();
    
    // why pass a year, shouldn't you be using the year of the date?
    // bool isLeapYear (int year);
    bool isLeapYear();
    
    // why pass anything?
    // int daysInMonth (int day, int month, int year);
    int daysInMonth();
    void advance ();
    
    // see how you set that constructor, what not the same here?
    // bool precedes (Date date);
    bool precedes (const Date &date);
    
    
private:
	// wtf with all the underscores?!?
    int day, month, year;




You daysInMonth implementation doesn't have a return type, so the compiler should have screamed at you. If it didn't, get a new compiler.

Your method of finding weekday seems to be to start on 1899-01-01 and call it a Monday. Then just step through, incrementing as you go... It's certainly not very efficient. It should be part of your Date class, anyway?

Perhaps give your date class a new method, int daysDiff(const Date &) const;. Not only is it useful, but if you get it working, then you can apply the logic of your weekday code.
Was This Post Helpful? 0
  • +
  • -

#6 monkey1604  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 09-August 12

Re: print weekday in C++ problem

Posted 10 August 2012 - 07:45 AM

thank you for your help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1