Help on Classes

Here's the question

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// Day Type.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>


using namespace std;

class Day
{
	string day;

	
public:
	
	void printDay();
	void setDay(string);
	string next();
	string prev();
	
	

};



void Day :: setDay(string b)
{
	
	if(b=="Sun")
	
		day="Sun";
	

	

	
	if(b=="Mon")
	
		day="Mon";
		


	if(b=="Tue")
	
		day="Tue";
		
	
	if(b=="Wed")
	
		day="Wed";
		


	if(b=="Thu")
	
		day="Thu";
	

	if(b=="Fri")
	
		day="Fri";
	
	
	if(b=="Sat")
	
		day="Sat";
		
	



}
void Day :: printDay()
{
	cout<<"Day is : "<<day<<endl;
		

}

string Day :: next()
{
	string n;
	
	if(day=="Sun")
	n="Mon";

	if(day=="Mon")
	n="Tue";

	if(day=="Tue")
	n="Wed";

	if(day=="Wed")
	n="Thu";

	if(day=="Thu")
	n="Fri";

	if(day=="Fri")
	n="Sat";

	if(day=="Sat")
	n="Sun";



	
	
	return n;
}

string Day :: prev()
{
	string p;

	if(day=="Sun")
	p="Sat";

	if(day=="Mon")
	p="Sun";

	if(day=="Tue")
	p="Mon";

	if(day=="Wed")
	p="Tue";

	if(day=="Thu")
	p="Wed";

	if(day=="Fri")
	p="Thu";

	if(day=="Sat")
	p="Fri";
	
	return p;
}









int _tmain(int argc, _TCHAR* argv[])
{
	Day Try;
	int a,d;
	string b="Sun";
	string c;
	string z;
	cout<<" 1 = Set Day \n 2 = Print Day \n 3 = Return Day \n 4 =  Next Day \n 5 = Previous Day \n 6 = Add no. of days \n 7 = Subtract no. of days \n 8 = Exit"<<endl;
	
	
	
	do
	{
		cin>>a;
		if(a==1)
		{
			b="Sun";
			Try.setDay(b);
		cout<<"Enter Day : "<<endl;
		cin>>b;
		Try.setDay(b);


		}

		if(a==2)
		{
			Try.setDay(b);
			Try.printDay();
		}

		if(a==3)
		{
			Try.setDay(b);
			Try.printDay();
		}

		if(a==4)
		{
			Try.setDay(b);
			z=Try.next();
			cout<<"Tomorrow is : "<<z<<endl;
		}

		if(a==5)
		{
			Try.setDay(b);
			z=Try.prev();
			cout<<"Yesterday is : "<<z<<endl;
		}

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

while(a!=8);
	
	
	

return 0;
	
	}

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.
Last edited on
I think that i did that already in this part ,
Is there something wrong in my loop ?
Sorry for the bad english


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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.
How can i store the days of a week into an array ?

Our professor only requires us to input and output the 1st 3 letters of the word ..
Just have a string array with elements of Sun, Mon, Tue, etc.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
day::day()

{

dayNumber=7;

day[1]="Mon";

day[2]="Tue";

day[3]="Wed";

day[4]="Thu";

day[5]="Fri";

day[6]="Sat";

day[7]="Sun";

};


Then after this ? what's next sorry i'm new to c++ programming
Last edited on
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!
ResidentBiscuit wrote:
Now, it's normal as a human to start counting from 1
O_O what kind of human are you? I've always counted from 0...
1
2
3
4
5
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;
Lol who starts at 0?? When you're counting your apples in your fridge do you really start 0?? Haha
There are 0 apples in my fridge, so yeah.
Eggs start at 6 and toilet paper at 2. (no, I don't store the toilet paper in the fridge)
Last edited on
Yes, when I have no apples I have zero apples. Then I see and apple and I have one apple. I see another, and I have two, etc...
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.
Topic archived. No new replies allowed.