Click here to Skip to main content
14,392,909 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to create day to month day converter. Just like if I put value to be 1 it should show January 1, 32 should show February, it will go upto 365 and 365 should show December 31.

But there is a logical error. It worked well only for the original object. When I changed this object using prefix increment and assigned it to another one. Both Assigned value and incremented value shown is not what I am expecting.

*Note:- I am completely excluding leap year.

What I have tried:

#include<iostream>
#include<string>
using namespace std;

class DayOfYear
{
	private:
		int day;
		string month;
	public:				
		DayOfYear(){}
		DayOfYear(int d){
			day = d;
			month = "";
		}
	
		DayOfYear convert()           
		{
			if(day<=31 ){
				month = "January";	
			}
			else if(day<=59){
				day = day-31;
				month = "February";
			}					
			else if(day<=90){
				day = day-59;
				month = "March";
			}			
			else if(day<=120){
				day = day-90;
				month = "April";
			}			
			else if(day<=151){
				day = day-120;
				month = "May";
			}			
			else if(day<=181){
				day = day-151;
				month = "June";
			}			
			else if(day<=212){
				day = day-181;
				month = "July";
			}			
			else if(day<=243){
				day = day-212;
				month = "August";
			}			
			else if(day<=273){
				day = day-243;
				month = "September";
			}            
			else if(day<=304){
				day = day-273;
				month = "October";
			} 			
			else if(day<=334){
				day = day-304;
				month = "November";
			}			
			else if(day<=365){
				day = day-334;
				month = "December";
			}			
			else if(day==366){
				day = 1;
				month = "January";
			}
			else if(day==0){
				day = 31;
				month = "December";
			}
			else{
				"";
			}
			return 0;
		}
		void print(){
			convert();
			cout<<"mm/dd = "<<month<<" "<<day<<endl;
		}
		
		DayOfYear operator++(){
			DayOfYear d;
			d.day = ++day;
			d.month = month;
			return d;				
		}				
};

int main()
{
	DayOfYear d1(222), d2;
	d1.print();
	d2 = ++d1;
	d1.print();
	d2.print();
}
Posted
Updated 23-Jul-19 22:39pm
v4
Rate this:
Please Sign up or sign in to vote.

Solution 1

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

I'll give you a hint though: even if we ignore leap years, how many days are there in February?
   
Comments
Member 14520059 23-Jul-19 5:17am
   
I appreciate your suggesion.. I will work on it again properly.. And this date is according to calender of 2019..So, There are 28 days in February.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Try (with a modern compiler)

#include <string_view>
#include <iomanip>
#include <iostream>
#include <array>
#include <algorithm>
#include <optional>


class Year
{
  std::array<int, 12> m_days;
  int m_year;

  static std::array <std::string_view, 12> s_month_name;

public:
  Year(int year):m_year(year)
  {
    m_days[0] = 31;
    m_days[1] = m_days[0] + 28;
    if (is_leap() ) ++m_days[1];

    m_days[2] = m_days[1] + 31;
    m_days[3] = m_days[2] + 30;
    m_days[4] = m_days[3] + 31;
    m_days[5] = m_days[4] + 30;
    m_days[6] = m_days[5] + 31;
    m_days[7] = m_days[6] + 31;
    m_days[8] = m_days[7] + 30;
    m_days[9] = m_days[8] + 31;
    m_days[10] = m_days[9] + 30;
    m_days[11] = m_days[10] + 31;
  }

  int year() const {return m_year;}

  std::optional < std::pair < std::string_view, int > > month_and_day(int day) const
  {
    auto it = std::lower_bound( m_days.begin(), m_days.end(), day);
    if ( it == m_days.end() )
      return std::nullopt;

    int offset = it == m_days.begin() ? 0 : *(it-1);
    return make_pair( s_month_name[ it - m_days.begin() ], (day - offset));
  }

  bool is_leap() const
  {
    return ((m_year % 400) == 0) || ((m_year % 4)==0 && (m_year % 100) != 0);
  }
};

using namespace std::literals;
std::array <std::string_view, 12> Year::s_month_name = { "Jan"sv, "Feb"sv, "Mar"sv, "Apr"sv, "May"sv, "Jun"sv, "Jul"sv, "Aug"sv, "Sep"sv, "Oct"sv, "Nov"sv, "Dec"sv};

int main()
{
  std::array<Year,3> year = { 1900, 2000, 2019};

  for ( const auto & y : year)
  {
    for (int d=1; d<=366; ++d)
    {
      auto result = y.month_and_day(d);
      if ( result )
      {
        auto [month, day] = result.value();
        std::cout << std::setw(3) << d << " is " << month << " " << std::setw(2) << day << " " << y.year() << std::endl;
      }
    }
    std::cout << "######################################################################\n";
  }
}
   
Comments
KarstenK 26-Jul-19 12:30pm
   
You can advertise your skill with: "Do you homework!" ;-)
CPallini 26-Jul-19 16:44pm
   
A bit belittling, in my humble opinion. :-D
More on the line of "hey darling, take a walk on the wild side". :-D :-D :-D
Rate this:
Please Sign up or sign in to vote.

Solution 2

You havent understood the language for what you want to do. Provide also a default constructor with setting all values. (To zero or to some jan. 1)

Operators need to be implemented else C++ does somethings you might not want. Read this fine tutorial on operators.

You missed the leap year problem completly.

tip: I would work with named constants and check for leap year first.
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100