Is there any better way to define PreviousDay Function???
plz let me know..

enum weekdayT { Monday,Tuesday,Wednesday,Thrusday,Friday,Saturday,Sunday };

weekdayT NextDay(weekdayT day){
	return weekdayT((day+1)%7);
}

weekdayT PreviousDay(weekdayT day){
	if(day==0)
		return weekdayT(day+6);
	else
		return weekdayT((day-1)%7);
}

What's wrong with what you have? I mean, you're not going to find anything a lot shorter.

Every thing but this makes sense to me :

return weekdayT((day-1)%7);

Assuming that the other user function which you have written is going to send values from 0 to 6 only for a weekday(if you have written that properly;)) then (day-1)%7 makes absolutely no sense and is redundant after what you have checked in your if statement.Only day-1 is enough as %7 there checks a thing you already know(that the value is between 0 and 6).

What CSurfer said!

You do need to assign Monday to the value of zero. ENUM specification doesn't specify zero or one for the first enum, and it becomes the call of the compiler manufacturer. So as insurance always assign the first one as zero if being used as math!

BTW Thursday is not spelled Thrusday.

In Professional code you would typedef your enum's for stronger typechecking, however that means enum addition is no longer a valid enum unless you cast it back into an enum!

typedef enum weekdayT { Monday = 0,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday };

http://www.kuzbass.ru:8086/docs/isocpp/dcl.html#dcl.enum

The identifiers in an enumerator-list are declared as constants, and can appear wherever constants are required. An enumerator-definition with = gives the associated enumerator the value indicated by the constant-expression. The constant-expression shall be of integral or enumeration type. If the first enumerator has no initializer, the value of the corresponding constant is zero. An enumerator-definition without an initializer gives the enumerator the value obtained by increasing the value of the previous enumerator by one.

What CSurfer said!

You do need to assign Monday to the value of zero. ENUM specification doesn't specify zero or one for the first enum, and it becomes the call of the compiler manufacturer. So as insurance always assign the first one as zero if being used as math!

BTW Thursday is not spelled Thrusday.

typedef enum weekdayT { Monday = 0,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday };

well Wildgoose plz confirm the logic before posting anything, as lot of newbie programmers are here to learn.
the first element in the enum will always hold ( 0 ) as default value.
and i ask for an afficient one line code, if there is any..

my problem is written below.

write a function IncrementDay(startDay,delta) that returns the day of the week that comes delta days after startday. Thus, IncrementDay(Thursday,4) should return Monday. your Implementation of IncrementDay should work if the value of delta is negative, in which case it should proceed bakward in time.

well below is the code working fine with +ve values for delta but unable to find a way out if we enter -ve values for delta, and if u have better code for PreviousDay(---) function plz let me know..

Thanks & regards.

enum weekdayT { Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday };

weekdayT NextDay(weekdayT day){
	return weekdayT((day+1)%7);
}

weekdayT PreviousDay(weekdayT day){
	if(day==0)
		return weekdayT(day+6);
	else
		return weekdayT(day-1);
}

weekdayT IncrementDay(weekdayT day,int pass){
	if(pass<0){
	//	return weekdayT((day-(day-pass))); unable to find a way out.. 
	}
	return weekdayT((day+pass)%7);	
}


int main ()
{
	cout << "Increment " << IncrementDay(Monday,-3) ;

	return 0;
}

I've been programming since the late 70's and was originally trained about setting the first enum as different compilers used zero or one. Admittedly that may have been the case in the early C compilers on main frames but standards get institued over time.

So though you're probably right that zero is the default, as a mater of consistency (since I don't always start my enums at zero) I'll continue setting zero on the first one.

Thanks for the enum information!

after paper work i got solution of the problem in IncrementDay function and now code is working exact as requirment , but still i m here to know if there is a better or a short code for the above mention problem ? plz let me know.

enum weekdayT { Monday=1,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday };

weekdayT NextDay(weekdayT day){
	return weekdayT((day%7)+1);
}

weekdayT PreviousDay(weekdayT day){
	if(day==0)
		return weekdayT(7);
	else
		return weekdayT(day-1);
}

weekdayT IncrementDay(weekdayT day,int pass){
	int p=0;
	int counter=0;
	p-=pass;
	if(pass<0){
		if(day>p)
		{
			return weekdayT(day-p);
		}
		else
		{
			counter = day;
			for(int i=0;i<p;i++)
			{
				counter--;
				if(counter<1)
				{
					counter = 7;
				}
			}
			return weekdayT(counter);
		}
	}
	return weekdayT((day%7)+pass);	
}


int main ()
{
	cout << "Increment " << IncrementDay(Tuesday,-4);

	return 0;
}

Well I looked at your code and it has a bunch of problems :

Error 1:

If this itself is your final code then you don't need NextDay() and PreviousDay() functions at all.

Error 2:

weekdayT IncrementDay(weekdayT day,int pass){
int p=0;
int counter=0;
p-=pass;
if(pass<0){
if(day>p)
{
return weekdayT(day-p);
}
else
{
counter = day;
for(int i=0;i<p;i++)
{
counter--;
if(counter<1)
{
counter = 7;
}
}
return weekdayT(counter);
}
}
return weekdayT((day%7)+pass);
}

This return should be:

return weekdayT((day+pass)%7);

Error 3:

Your code gives correct result for

cout << "Increment " << IncrementDay(Tuesday,0);

as Increment 0 but for

cout << "Increment " << IncrementDay(Tuesday,-7);

it gives Increment 7 instead of Increment 0 which even though correct is logically wrong.

And ya a general instruction about functions like below if you really use them :

weekdayT NextDay(weekdayT day){
	return weekdayT((day%7)+1);
}

Don't use a function call just for a single statement processing. As you know the processor needs to do a whole lot of stuff before calling a small function like this one too and in cases like this its nothing but waste of processor time.If you can incorporate functions as small as these in macros then it speeds up your program to a good extent.
Though not an error it helps you better your program so thought of mentioning it.:)

commented: Thanks for helping brother +1
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.