7 Replies - 4448 Views - Last Post: 27 September 2008 - 07:34 PM Rate Topic: -----

#1 Soyaku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 28-February 08

Prefix and Postfix Overloading Problem

Posted 23 September 2008 - 05:06 PM

This is the header file.
 #ifndef DAYOFYEAR_H
#define DAYOFYEAR_H
#include <iostream>
#include <string>
using namespace std;

class DayOfYear
{
private:
   int day; 
public:
   static const int daysAtEndOfMonth[ ];
   static const string monthName[ ];
   void print();
   void setDay(int d){day = d;}

   //New Constructor (NOT BEING USED)
   void setTime (string m, int i) { day = i; }

   //Overloaded Operators
	DayOfYear DayOfYear::operator++();
	DayOfYear DayOfYear::operator++(int); 
};

//***************************************
//		  DayOfYear::print.		   *
// Convert and print day of year		* 
//***************************************								 
void DayOfYear::print()
{
	int month = 0;
	
	//CALC MONTH 
	while (daysAtEndOfMonth[month] < day)
		 month = (month + 1) %12;
	
	//OUTPUT MONTH AND DAY
	if (month == 0) 
	  cout << "January " << day;
	else
	  {  cout << DayOfYear::monthName[month] <<  " " 
			  << day - DayOfYear::daysAtEndOfMonth[month-1];
	  }	
};



//*****************************************************
// Overloaded prefix ++ operator.					 *
//*****************************************************
DayOfYear DayOfYear::operator++()
{
	++day;
	if (day = 366)
	{ day = day - 366; }
	return *this;
}

//*******************************************************
// Overloaded postfix ++ operator.					  * 
//******************************************************* 
DayOfYear DayOfYear::operator++(int)
{
	DayOfYear temp;
	temp.setDay(day);

	day++;
	if (day = -1)
	{ day = day + 365; }
	return temp;
}


#endif



This is the main program.
#include <iostream>
#include <string>
#include "dayofyear.h"
using namespace std;


const int DayOfYear::daysAtEndOfMonth[ ] = {31, 59, 90, 120, 
									   151, 181, 212, 243, 273,
									   304, 334, 365};
const string DayOfYear::monthName[ ]= {"January", "February", "March",
				 "April", "May", "June", "July",
				 "August", "September", "October",
				 "November", "December"};


int main()
{
	DayOfYear dy;
	//CONVERSION FROM DAY to MONTH AND DAY
	cout << "This program converts a day given by a number 1 through 365" << 
			"\ninto a month and a day.";
	//USER INPUT			
	cout << "\nEnter a number: ";
	int day;
	cin >> day;

	if (day <= 0 || day > 365)
	{   cout << "Invalid range for a day." << endl;
		exit(1);
	}

	//INCREMENT OF OVERLOADED PREFIX
	++day;
	cout << endl << endl;
	//TEXT COUT TO SEE IF INCREMENT HAPPENED
	cout << day << " is the day.";
	//PRINT
	dy.setDay(day);
	dy.print();
	cout << endl;
	return 0;
}
								  






Basically, if you enter: 40 It'll return February 9th after the conversion has been made.
Now, what I'm trying to do is have the user enter the day 365 and then use a prefix to increment it to 366. The user cannot enter in the console anything above 365 so it has to be incremented using the overloaded operator, in which I only want to increment one time only. Therefore, IF it's 366 it needs to subtract 365 from the DAY and then do the print function in which should return January 1st.

My problem is that it's hanging up after the increment of DAY to the number 366. It won't print the month and day and I've been trying to find the answer to this for quite some time and finally turned to DIC. I have a very limited understanding of operator overloading.

Thanks,
Soyaku

Is This A Good Question/Topic? 0
  • +

Replies To: Prefix and Postfix Overloading Problem

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,563
  • Joined: 25-January 07

Re: Prefix and Postfix Overloading Problem

Posted 23 September 2008 - 11:38 PM

I am not sure, whether this is the exact reason of your problem. But you used assignment operator instead of comparison operator in you if.

DayOfYear DayOfYear::operator++()
{
    ++day;
    if (day == 366)
    { day = day - 366; }
    return *this;
}



plus in your main you are incrementing variable day, which is an integer and then setday of your object [which might be causing this problem.]. So you might want to setday first and then increment dy to see the effect.

I hope this will help you. :)
Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Prefix and Postfix Overloading Problem

Posted 23 September 2008 - 11:52 PM

View PostAmitTheInfinity, on 23 Sep, 2008 - 11:38 PM, said:

I am not sure, whether this is the exact reason of your problem. But you used assignment operator instead of comparison operator in you if.

DayOfYear DayOfYear::operator++()
{
    ++day;
    if (day == 366)
    { day = day - 366; }
    return *this;
}



plus in your main you are incrementing variable day, which is an integer and then setday of your object [which might be causing this problem.]. So you might want to setday first and then increment dy to see the effect.

I hope this will help you. :)



Also ... fix <--------------------
//*******************************************************
// Overloaded postfix ++ operator.					  *
//*******************************************************
DayOfYear DayOfYear::operator++(int)
{
	DayOfYear temp;
	temp.setDay(day);

	day++;
	if (day = -1) // <-------------- fix to ... if (day ==-1)
	{ day = day + 365; }
	return temp;
}


Was This Post Helpful? 0
  • +
  • -

#4 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,563
  • Joined: 25-January 07

Re: Prefix and Postfix Overloading Problem

Posted 24 September 2008 - 12:00 AM

Well, David W, the logic of that post increment operator is itself wrong. That looks like the logic of decrement operator to me, so I kept it aside and tried to resolve his first problem at first. Then he might come back with the remaining problems. :)

Trying to catch all fishes in lake in one go might cause catching nothing. :)
Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: Prefix and Postfix Overloading Problem

Posted 24 September 2008 - 01:01 AM

Here is a 'test' of some fixes, (see the fixes), that seem to show the correct output now ...

#include <iostream>
#include <string>
#include "dayofyear.h"
using namespace std;

const int DayOfYear::daysAtEndOfMonth[ ] = 
{   31, 59, 90, 120, 151, 181, 
	212, 243, 273, 304, 334, 365
};
const string DayOfYear::monthName[ ]= 
{
	"January", "February", "March", "April", "May", "June", 
	"July", "August", "September", "October", "November", "December"
};


int main()
{
	DayOfYear dy;
	//CONVERSION FROM DAY to MONTH AND DAY
	cout << "This program converts a the day number 1 through 365, into \n" <<
			"the month and the day ( assuming it's NOT a LEAP year).";
	
	//USER INPUT  
	int day;
	
START_TEST: 
	day = -1;	   
	cout << "\n\nEnter the 'day number' in the year: ";
	cin >> day;
	cin.clear();
	cin.sync();
	if( day == -1) goto START_TEST;
	
	if (day < 1 || day > 365)
	{   cout << "Invalid range for a day in a NON LEAP year." << endl;
		//system("pause");
		goto START_TEST; //exit(1);
	}
	
	dy.setDay(day);
	dy.print();
	cout << endl;
	
	goto START_TEST;
}





#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H
#include <iostream>
#include <string>
using namespace std;

class DayOfYear
{
private:
   int day;
public:
   static const int daysAtEndOfMonth[ ];
   static const string monthName[ ];
   void print();
   void setDay(int d){day = d;}

   //New Constructor (NOT BEING USED)
   void setTime (string m, int i) { day = i; }

   //Overloaded Operators
	DayOfYear DayOfYear::operator++();
	DayOfYear DayOfYear::operator++(int);
};

//***************************************
//		  DayOfYear::print.		   *
// Convert and print day of year		*
//***************************************								
void DayOfYear::print()
{
	int month = 0;
	
	//CALC MONTH
	while (daysAtEndOfMonth[month] < day)
		 month = (month + 1) %12;
	
	//OUTPUT MONTH AND DAY
	if (month == 0)
	  cout << "January " << day;
	else
	  {  cout << DayOfYear::monthName[month] <<  " "
			  << day - DayOfYear::daysAtEndOfMonth[month-1];
	  }	
};

//*****************************************************
// Overloaded prefix ++ operator.					 *
//*****************************************************
DayOfYear DayOfYear::operator++()
{
	++day;
	if (day == 366) // <------------------------------------------------fix 1 //
	{ day = day - 366; }
	return *this;
}

//*******************************************************
// Overloaded postfix ++ operator.					  *
//*******************************************************
DayOfYear DayOfYear::operator++(int)
{
	DayOfYear temp;
	temp.setDay(day);

	day++;
	if (day == -1) // <-------------------------------------------------fix 2 //
	{ day = day + 365; }
	return temp;
}

#endif





You might try this 'test' also ...


#include <iostream>
#include <string>
#include "dayofyear.h"
using namespace std;

const int DayOfYear::daysAtEndOfMonth[ ] = 
{   31, 59, 90, 120, 151, 181, 
	212, 243, 273, 304, 334, 365
};
const string DayOfYear::monthName[ ]= 
{
	"January", "February", "March", "April", "May", "June", 
	"July", "August", "September", "October", "November", "December"
};


int main()
{
	DayOfYear dy;
	int dayNum;
	
	//CONVERSION FROM DAY to MONTH AND DAY
	cout << "This program converts a the day number 1 through 365, into \n"
		 << "the month and day of that month ...\n\n";
	// testing some values ...
	for ( dayNum =0; dayNum < 367; ++dayNum )
	{
		if (dayNum < 1 || dayNum > 365)
		{   cout << "Day number "<< dayNum 
				 << " is NOT IN VALID range 1..365 for NON LEAP years.\n\n";
			system("pause");
			//exit(1);
		}
		if (dayNum == 366) return 1;
		dy.setDay(dayNum);
		dy.print();
		cout << "\t";
	}
}


Was This Post Helpful? 0
  • +
  • -

#6 Soyaku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 28-February 08

Re: Prefix and Postfix Overloading Problem

Posted 25 September 2008 - 11:47 AM

Main program.
#include <iostream>
#include <string>
#include "dayofyear.h"
using namespace std;


const int DayOfYear::daysAtEndOfMonth[ ] = {31, 59, 90, 120, 
									   151, 181, 212, 243, 273,
									   304, 334, 365};
const string DayOfYear::monthName[ ]= {"January", "February", "March",
				 "April", "May", "June", "July",
				 "August", "September", "October",
				 "November", "December"};


int main()
{
	string month;
	int day;
	//Enter a month
	cout << "Please enter a month exactly as it is spelled\nwithout spaces and correct capitalization.";
	cout << "\nEnter month name:";
	cin  >> month; //INPUT FOR MONTH

	//VALIDATION FOR MONTHS WITH 31 DAYS
	if (month == "January" || month == "March" || month == "May" ||
		month == "July" || month == "August" || month == "October" ||
		month == "December") 
	{ cout << "There are 31 days in " << month << endl; 
	  cout << "Please enter a day within the month:";
	  cin  >> day; 
	  if (day > 31) { cout << "Please enter a correct day within 31 days next time! \n"; exit(1); }}

	//VALIDATION FOR MONTHS WITH 30 DAYS
	if (month == "April" || month == "June" || month == "September" || month == "November" ) 
	{ cout << "There are 30 days in " << month << endl; 
	  cout << "Please enter a day within the month:";
	  cin  >> day; 
	  if (day > 30) { cout << "Please enter a correct day within 30 days next time! \n"; exit(1); }}

	//VALIDATION FOR FEBRUARY
	if (month == "February") 
	{ cout << "There are 28 days in " << month << endl; 
	  cout << "Please enter a day within the month:";
	  cin  >> day; 
	  if (day > 28) { cout << "Please enter a correct day within 28 days next time!\n"; exit(1); }}

	++day;
	DayOfYear dy(month, day); //Send to constructor
	//Postfix and prefix demonstration
	dy.print();
	return 0;
}



Header file.
#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H
#include <iostream>
#include <string>
using namespace std;

//Made global because of overloading operators
//Operators need to be able to change this so monthName knows which month it is
int dummy_month = 0;

class DayOfYear
{
private:
   int day; 
   string mString;
public:
   static const int daysAtEndOfMonth[ ];
   static const string monthName[ ];
   void print();
   void plusPlus();
   // void setDay(int d){day = d;} This is no longer needed.

   //New Constructor
   //Insead of setDay being used, day is simply set 
   //to whatever the parameter of i is entered.
   DayOfYear (string m = "", int i = 0) { day = i; mString = m;}

   //Overloaded Operators
	DayOfYear DayOfYear::operator++();
	DayOfYear DayOfYear::operator++(int); 
};

//***************************************
//		  DayOfYear::print.		   *
// Convert and print day of year		* 
//***************************************								 
void DayOfYear::print()
{
	int month = 0;
	if (mString == "January") { month = 0; }
	if (mString == "February") { month = 1; }
	if (mString == "March") { month = 2; }
	if (mString == "April") { month = 3; }
	if (mString == "May") { month = 4; }
	if (mString == "June") { month = 5; }
	if (mString == "July") { month = 6; }
	if (mString == "August") { month = 7; }
	if (mString == "September") { month = 8; }
	if (mString == "October") { month = 9; }
	if (mString == "Novmeber") { month = 10; }
	if (mString == "December") { month = 11; }

	month = month + dummy_month; //Increment IF the day is incremented/decremented with ++ or --

	//OUTPUT MONTH AND DAY
	cout << endl;
	cout << DayOfYear::monthName[month] <<  " " 
		 << day;
	cout << endl << endl;
   
};


void DayOfYear::plusPlus()
{
	if (mString == "January" || mString == "March" || mString == "May" ||
		mString == "July" || mString == "August" || mString == "October" ||
		mString== "December" && day == 32)
	{day = day - 31; dummy_month = dummy_month + 1;}
	if (mString == "April" || mString == "June" || mString == "September" || mString == "November" && day == 31) 
	{day = day - 30; dummy_month = dummy_month + 1;}
	if (mString == "February" && day == 29) 
	{day = day - 28; dummy_month = dummy_month + 1;}
}


//*****************************************************
// Overloaded prefix ++ operator.					 *
//*****************************************************
DayOfYear DayOfYear::operator++()
{
	++day;
	plusPlus();
	return *this;
}

//*******************************************************
// Overloaded postfix ++ operator.					  * 
//******************************************************* 
DayOfYear DayOfYear::operator++(int)
{
	DayOfYear temp(mString, day);

	day++;
	plusPlus();
	return temp;
}



#endif



I appreciate both of you for helping me! I've came really far since then and have decided to change the program completely.

My new goal is to modify the ++ prefix and postfix operators to go into the next month. Right now, I'm nearly finished with this part of the program, but the only problem I have is the ++ operators not using the function that I've implemented. To explain further, IF the 31/30/28 day months exceed their day limit, the operator needs to make that extra day into the NEXT month. Example: User enter January 31st. The program proceeds to ++day; The day count is now January 32. There is no January 32, so the month needs to flip over to February 1st.

How can I make this work using the overloaded ++ operators?
Was This Post Helpful? 0
  • +
  • -

#7 Soyaku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 28-February 08

Re: Prefix and Postfix Overloading Problem

Posted 26 September 2008 - 09:01 AM

Bumping to top, still having problems with program, see above post.

Thanks!
Was This Post Helpful? 0
  • +
  • -

#8 Soyaku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 28-February 08

Re: Prefix and Postfix Overloading Problem

Posted 27 September 2008 - 07:34 PM

One last bump, I'm still having the same problems, have tried revamping the entire code with no luck.

If anything, can someone give me some input on how to utilize my static variables more? Instead of making a bunch of IF statements and all that sort of thing, use a different set but ones that use the arrays I have set? Again, this is fully experimental to better my understand of overloaded functions, I'd just like to clean up my code a little better and see if anyone else would do it different.

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1