Code will compile, but doesn't work correctly.

Here is the purpose of the program (I'm sure the "DayOfTheWeek" program has popped up here before):

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 a certain amount of days to the current day. For example, if you add five days to Saturday, the day to be returned is Thursday. Likewise, if we add 12 days to Wednesday, the day returned will be Monday.

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
#include <iostream>
#include <string>

using namespace std;

class DayOfTheWeek

{

private:		
	string day;

public:

	void setDay(string currentDay);
	string getDay() const;
	void printDay() const;
	string plusOneDay();
	string minusOneDay();
	string addDays(int daysahead);

};



void DayOfTheWeek::setDay(string currentDay)
{ day = currentDay;}

string DayOfTheWeek::getDay() const
{return day;}

void DayOfTheWeek::printDay() const
{ }

string DayOfTheWeek::plusOneDay() 
{
		string dayarray[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

		int indexofday;

	for(int i = 0; i < 7; i++)
	{
	if (day == dayarray[i])
	{
		indexofday = i;
		break;
				}
	}
	return dayarray[ (indexofday + 1) % 7]; 
	}

string DayOfTheWeek::minusOneDay()
{
	string dayarray[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
	int indexofday;

for( int i = 0; i < 7; i++)
{
   if (day == dayarray[i])
   {
       indexofday = i;
       break;
   }
}

if(indexofday = 0)
   return dayarray[6];
else
   return dayarray[ (indexofday - 1) % 7];
}

string DayOfTheWeek::addDays(int daysahead)
{
	string dayarray[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
	int indexofday;

for(int i = 0; i < 7; i++)
{
   if (day == dayarray[i])
   {
       indexofday = i;
       break;
   }
}

return dayarray[ (indexofday + daysahead) % 7];  
}


int main()
{

	DayOfTheWeek monday;
	monday.setDay("Monday");
	string currentDay = monday.getDay();
	monday.printDay();

	
	
	cout <<"The value of the monday object is "<<monday.getDay()<<"."<<endl;
	cout <<"The day after monday object is "<<monday.plusOneDay()<<"."<<endl;
	cout <<"The day before monday object is "<<monday.minusOneDay()<<"."<<endl;
	cout <<"Monday + 3 = "<<monday.addDays(3)<<"."<<endl;
	cout <<"The value of the monday object is still "<<monday.getDay()<<"."<<endl;
	cout <<"Monday - 3 = "<<monday.addDays(4)<<"."<<endl;
	cin.ignore();
        return 0;
}


Line 101 Works correctly. Supposed to use plusOneDay to add 1 day, and it equals Tuesday.

Line 102 Does not work. Supposed to to use minusOneDay to subtract 1 day, and equal Sunday. But its always blank.

Line 103 Works correctly. Supposed to use addDays to add 3 days, and it equals Thursday.

Line 105 Does not work like its supposed to. It is supposed to use addDays to subtract 3 days, it should equal Friday, but its always blank. Instead I just added by 4 because I can't get it to work otherwise.
1. Use an enumerator for the days of the week.
2. Use a for loop to add the days, and in each iteration, check to see if the current iteration % 7 is 0. If it is 0, then set the number back to 1 and continue.
I'm not allowed to use enumerator for this assignment, unfortunately.

If I turned it in as is, I'd still get a 50/60 for my grade, but I want that 60/60.
Also line 66 should read if(indexofday == 0). This will fix your problem.
Last edited on
Thanks!

Seems like all I needed was a better pair of eyes lol.
Topic archived. No new replies allowed.