Design and implement a class DayType that implements the day of the week in a program. The class should store the day such as Sun for “Sunday”. The program should be able to perform the following operations on an object of type DayType:
1. Set the day.
2. Print the day.
3. Return the day.
4. Return the next day.
5. Return the previous day.
6. Calculate and return the day by adding certain days to the current day.
7. Calculate and return the day by subtracting certain days to the current day.
8. Add the appropriate constructors.
Here's what if done i really dont know how to make question # 6 and 7 correctly
6 and 7 just want you to be able to add or subtract X days to/from the current day, eg Mon + 3 = Thu. You can essentially just call Day::next() and Day::prev() in a loop for adding and subtracting respectively.
if(a==6)
{
cout<<"Enter of number of days to add : "<<endl;
cin>>d;
Try.setDay(b);
do
{
d=d-1;
Try.next();
}
while(d!=1);
z=Try.next();
cout<<"After adding "<<d<<" days , Day is : "<<z;
}
if(a==7)
{
cout<<"Enter of number of days to subtract : "<<endl;
cin>>d;
Try.setDay(b);
do
{
d=d-1;
Try.prev();
}
while(d!=1);
z=Try.prev();
cout<<"After subtracting "<<d<<" days , Day is : "<<z;
I think if you store the days of a week into an array, this would be much simpler, and look cleaner. Then it all becomes simple. It just boils down to adding or subtracting on the index of your array. Just have to add some validation to make sure it doesnt go out of bounds, but that's neither here nor there.
On setday(), what if I type in "Sunday"? Things you have to think about it here. There are a couple solutions for this, easiest is just to add an else saying "Hey, thats not the way I wanted you to type this, jerk!", and recall the function.
Well first of all, I don't think you quite understand arrays, which is alright. Arrays in C++ start at index 0 (I believe this is true for most languages, but I think there is a few out there that start with 1). So if you want an array to hold 7 elements, you will create an array int x[7];. Now, it's normal as a human to start counting from 1 to 7, but computers don't think like us. So the elements of this array will from 0-6, which is 7 elements total. So you just need to change your array to hold 7 elements, and start counting at 0.
After all this, you can store the day of the week in a int variable, and use this variable as the index for the array. For example, let's say we start on Wednesday. That is the 3rd day of the week (if you start on Monday, 4th if you start on Sunday), so your day variable will be set to 3. To find the next day, you just use this variable plus 1 as your array index and that will tell you the next day. Same goes for previous.
But, you will have to add some validation to make sure that this variable never goes over 6 or below 0. And if it were to go beyond these bounds, you just wrap the value to the other side. For example:
Let's say we're on Saturday, and you want to know what day it will be in 3 days, so you add 3 to this variable. Well, your variable without any validation will equal 8, which would be out of bounds for your array and yield an error. So, we figure it takes 1 day from Saturday to reach Sunday, so that leaves 2 days left over. This will drop you off at Tuesday (1 day for Monday, and the last day will leave you at Tuesday).
I hope I explained this well enough, once you understand the logic behind this, your program becomes quick simple. Feel free to ask more if you're still confused!
class Day{
public:
Day next() const; //this makes more sense
Day prev() const;
};
1 2 3 4 5 6 7
Try.setDay(b); //¿how many times will you do this?
do{
d=d-1;
Try.next(); //That function doesn't modify 'Try'
}while(d!=1);
z=Try.next();
cout<<"After adding "<<d<<" days , Day is : "<<z;
No no no. You're not counting if you have no apples. I don't to my fridge and start counting my no apples. And if you have apples, you don't point to the empty space in front of your apples and say "Ok zero apples, one apple, two apple..." You just start at one lol. Not sure if trolled or not but I win this!
o_o When I count things, I count how many there are, which means I initially start at zero. I don't know where you went to school but I've never seen anyone say "Oh, because I don't have any apples there is no way for me to count how many I have, I guess I have undefined apples."
Anyway, I'm going to stop derailing this thread now XD
No one starts counting at 0! What is the point of that? I'm not saying you CAN'T say you have 0 apples, but you don't start counting your apples at 0. I want you to give me some dialog of you counting 3 apples.