Help with header C++

I've finish all the required part but sadly I've encountered errors on the (Sunday and Saturday part) during the run. * I was so close to the final result, can anybody guide/help me on my errors. Thank you and God Bless you.

The errors are with my output.

1a). Design and implement a class dayType that implements the day of the week in a program. The class dayType 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:

a. Set the day
b. Print the day
c. Return the day
d. Return the next day
e. Return the previous day
f. Calculate and return the day by adding certain days to the current day. For example if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
g. Add the appropriate constructors.

1b). Then, write the definitions of the functions to implement the operations for the class dayType as defined in Programming above. Also, write a program to test various operations on this class.

The following are my Header file, Implementation file and Main file.

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

#include <iostream>
#include<string>  

using namespace std; 

 class dayType  {
 	public:  
	void print()  const;  
	void setDay(string day);     
	int getPrevDay(); 
	int getNextDay();
	void randDay(int num);
	dayType();  
	dayType(string day);
	private:  
    int num;
	string today; 
	string prevDay;
	string nextDay;
};


void dayType::print()  const  
{  
	cout<< "Today is: "<< today <<endl;  
}  
void dayType::setDay(string day)  
{ 
    
	if ((day == "Monday") || (day == "monday"))
	{
		num=0;
		today=day;	
	}
	else if ((day == "Tuesday") || (day == "tuesday"))
	{
		num=1;
		today=day;	
	}
	else if ((day == "Wednesday") || (day == "wednesday"))
	{
		num=2;
		today=day;	
	}
	else if ((day == "Thursday") || (day == "thursday"))
	{
		num=3;
		today=day;	
	}
	else if ((day == "Friday") || (day == "friday"))
	{
		num=4;
		today=day;	
	}
	else if ((day == "Saturday") || (day == "saturday"))
	{
		num=5;
		today=day;	
	}
	else if ((day == "Sunday") || (day == "sunday"))
	{
		num=6;
		today=day;	
	}
	else
	    cout<<"Not a valid day. "<<endl;

	
}   
int dayType::getPrevDay()    
{  
     num--;
	return num;  
} 
int dayType::getNextDay() 
{
     num++;
	return num;
}

 //Defaultconstructor  
dayType::dayType()  
{ 
	today = "";  
 
}  
//Constructorwithparameters 
dayType::dayType(string day)  
{
	string today;
	day = today;  
}
void randDay(int num)
{	
	int random, add;
	cout << "How many days would you like to add to the current day? ";
	cin >> add;
	if (add > 6)
	{
		random = num + add / 7;
	}
	else
	random = num + add;
	
		if (random==0) 
	{
	   cout << "In " << add << " days, it will be " << "Sunday";
	}
	else if (random==1)
	{
	    cout << "In " << add << " days, it will be " << "Monday";
	}
	else if (random==2)
	{
	   cout << "In " << add << " days, it will be " << "Tuesday";
	}
	else if (random==3)
	{
		cout << "In " << add << " days, it will be " << "Wednesday";
	}
	else if (random ==4)
	{
		cout << "In " << add << " days, it will be " << "Thursday";
	}
	else if (random == 5)
	{
		cout << "In " << add << " days, it will be " << "Friday";
	}
	else 
		cout << "In " << add << " days, it will be " << "Saturday";
	
}

int main()
{
	string today;
	int dayNum, numDay, newNum;


	cout << "Enter the current day of the week: ";
	cin >> today;
	
	dayType week;
	week.setDay(today);
	dayNum = week.getPrevDay();
	numDay = week.getNextDay();
	newNum = week.getNextDay();

	cout<<"Yesterday was: ";
	if (dayNum==0) 
	{
	    cout<< "Monday" << endl;
	}
	else if (dayNum==1)
	{
	    cout<< "Tuesday" << endl;
	}
	else if (dayNum==2)
	{
	    cout<< "Wednesday" << endl;
	}
	else if (dayNum==3)
	{
		cout << "Thursday" << endl;
	}
	else if (dayNum ==4)
	{
		cout << "Friday" << endl;
	}
	else if (dayNum == 5)
	{
		cout << "Saturday" << endl;
	}
	else 
		cout << "Sunday" << endl;
		
	week.print();

	cout<<"Tomorrow is: ";
	if (numDay==0) 
	{
	    cout<< "Tuesday" << endl;
	}
	else if (numDay==1)
	{
	    cout<< "Wednesday" << endl;
	}
	else if (numDay==2)
	{
	    cout<< "Thursday" << endl;
	}
	else if (numDay==3)
	{
		cout << "Friday" << endl;
	}
	else if (numDay ==4)
	{
		cout << "Saturday" << endl;
	}
	else if (numDay == 5)
	{
		cout << "Sunday" << endl;
	}
	else 
		cout << "Monday" << endl;
		
	randDay(newNum);

	
	
	
	return 0;	
}
Last edited on
What is the observed problem?
* Fails to compile?
* Crash on run?
* Unexpected result?

At least line 74 is suspicious.
Hello jsbd29,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

Your program did compile, but after some checking I did find some problems.

In main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int d{}, p{}, n{};  // <--- Good practice to initialize your variables.
//declare the class object
dayType obj;

//Prompt and read the input day
std::cout << " Enter the number:\n 1 - Monday "  // <--- Added "\n". Monday should be on its own line.
	<< "\n 2 - Tuesday"  // <--- Formatting needs to be consistent.
	<< "\n 3 - Wednesday"
	<< "\n 4 - Thursday"
	<< "\n 5 - Friday"
	<< "\n 6 - Saturday"
	<< "\n 7 - Sunday \n";  // <--- Added second "\n".
	// <--- Needs an option to quit.

//prompt the input day
std::cout << " Enter day: ";
std::cin >> d;

// <--- Needs  check here to end the program. 


Still looking into the rest of main, but some parts d not seem right.

In the header file:
Header files are in the wrong place. They should be in the main file with "main".

"using namespace std;" should never be in a header file.
http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/ good reading.

For the class this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class dayType
{
public: int presday;
		int prevday;
		int nextday;
		//Constructor
		dayType()
		{
			presday = 0;
			nextday = 0;
			prevday = 0;
		}
		//Member function declarations
		void set(int day);
		void print(int day);
		int Next_day(int pres);
		int Add_Next_days(int day);
		int day();
		int Prev_day(int pres);
};


Should be this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Class declaration
class dayType
{
	public:
		//Constructor
		dayType()
		{
			presday = 0;
			nextday = 0;
			prevday = 0;
		}
		//Member function declarations
		void set(int day);
		void print(int day);
		int Next_day(int pres);
		int Add_Next_days(int day);
		int day();
		int Prev_day(int pres);

	private:
		int presday;
		int prevday;
		int nextday;
};


Otherwise you defeat the purpose of the class.

In the "void dayType::print(int d)" function:
Each cout statement would look better if it ended with a "\n"

You are missing if (d == 5) , so "Friday" always prints.

After that I will point out that you include "<iomanip>, but never make use of all that is available. Some of the print calls use a "\t" in the previous cout and spacing is not always the best with a tab. The use of "std::setw()" wuld work better.

Hope that helps,

Andy
Thank You Andy! I works now . I am having problems to create the header.h
Last edited on
Topic archived. No new replies allowed.