Calculating weekday

Ok I'm in the middle of another assignment, for which I won't paste the whole code because it would take up at least a page and you would give up halfway through reading it. The problem is with two functions that would calculate the next weekday, and the weekday if x number of days are given.

e.g if today is Thursday, first function would output "After 1 day it is Friday." and if you use addDays(1000) it will output "After 1000 days it is Monday" (or whatever day it will be). Here is the header and .cpp file - I am completely stuck on how to implement this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//weekDay.h
#include<string>
class weekDay{
private:
	string day;
public:
	weekDay(string = "Mon"); // Initialize the day
	void print() const; // Print the day
	void set(string); // Set the day
	string get() const; // Return the day
	string getNext() const; // Return the next day
	string getPrevious() const; // Return the previous day
	string add(const int) const; // Add a number of days to the day and return
}


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
//weekDay.cpp
#include<iostream>
using namespace std;
#include "weekDay.h"


weekDay::weekDay(string weekday)
{
	day=weekday;
}

void weekDay :: set(string weekday){
	day=weekday;
}

void weekDay :: print() const{
	cout<<day<<" ";
}

string weekDay :: get() const{
	return day;
}

string weekDay :: getNext() const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"}

}

Ok I think I figured the add 1 day part using:

1
2
3
4
5
6
7
8
9
10
string weekDay :: getNext() const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"}
	for (int i=0; i<8; i++){
		if (i==7)
			i=0;
		if (weekday[i]==day)
			day=weekday[i+1];
	}

}


EDIT: I see a problem with this now... if weekday[i] is friday then it will set day to weekday[7] which doesn't exist...

Perhaps this would solve it?

1
2
3
4
5
6
7
8
9
10
11
12
string weekDay :: getNext() const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"}
	for (int i=0; i<8; i++){
		if (i==7)
			i=0;
		if (weekday[i]==day)
			if(i==6)
				day=weekday[0]
			day=weekday[i+1];
	}
	return day;
}

Last edited on
Here is my attempt:

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
#include<iostream>
using namespace std;
#include "weekDay.h"


weekDay::weekDay(string weekday)
{
	day=weekday;
}

void weekDay :: set(string weekday){
	day=weekday;
}

void weekDay :: print() const{
	cout<<day<<" ";
}

string weekDay :: get() const{
	return day;
}

string weekDay :: getNext() const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};
	for (int i=0; i<7; i++){
		if (weekday[i]==day)
			if(i==6)
				day=weekday[0];
			day=weekday[i+1];
	}
	return day;
}

string weekDay :: getPrevious() const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};
	for (int i=0; i<7; i++){
		if (weekday[i]==day)
			if(i==0)
				day=weekday[6];
			day=weekday[i-1];
	}
	return day;
}

string weekDay :: add(const int _days) const{
	string weekday[7]={"Sat","Sun","Mon","Tue","Wed","Thu","Fri"};

	for (int i=0; i<_days; i++){
		int j=i;

		if (weekday[i]==day)
			for(int k=0; k<(_days-i); k++){
				if (j==7)
					j=0;
				j++;

			}
			day=weekday[j];
			return day;
	}


}
Topic archived. No new replies allowed.