Classes C++ Assignment

This is my assignment:
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 Sunday for Sunday. The program should be able to perform the following operations on an object of type dayType:

Step a: Set the day.
Step b: Print the day.
Step c: Return the day.
Step d: Return the next day.
Step e: Return the previous day.
Step 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.
Step g: Add the appropriate constructors.
Write the definitions of the functions to implement the operations for the class dayType.

When I run my main.cpp file this is the output:
Monday
Sunday
Tuesday
Sunday

Basically, the code is working for Monday, but not for Sunday. The program outputs the previous day for Monday as well as the next day for Monday. The code prints the temp.print (Sunday) but not the next or previous day for Sunday. Can anyone please tell me why the code only works for Monday and not Sunday and what I am doing wrong here? Thank you in advance :-)

PS: I realize Step F is not completed yet but I want to get this problem solved first.

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
//daytype.cpp
#include <iostream>
#include <string>

#include "dayType.h"

using namespace std;

// array for days of the week
string weekDays[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
// part b
void dayType::print() const
{
cout << weekDay;
}

// part d
string dayType::nextDay() const 
{
string nextDay;
if (weekDay == "Monday")
    nextDay = "Tuesday";
    return nextDay;
if (weekDay == "Tuesday")
    nextDay = "Wednesday";
    return nextDay;
if (weekDay == "Wednesday")
    nextDay = "Thursday";
    return nextDay;
if (weekDay == "Thursday")
    nextDay = "Friday";
    return nextDay;
if (weekDay == "Friday")
    nextDay = "Saturday";
    return nextDay;
if (weekDay == "Saturday")
    nextDay = "Sunday";
    return nextDay;
if (weekDay == "Sunday")
    nextDay = "Monday";
    return nextDay;
}

// part e
string dayType::prevDay() const 
{
string prevDay;
if (weekDay == "Monday")
    prevDay = "Sunday";
    return prevDay;
if (weekDay == "Tuesday")
    prevDay = "Monday";
    return prevDay;
if (weekDay == "Wednesday")
    prevDay = "Tuesday";
    return prevDay;
if (weekDay == "Thursday")
    prevDay = "Wednesday";
    return prevDay;
if (weekDay == "Friday")
    prevDay = "Thursday";
    return prevDay;
if (weekDay == "Saturday")
    prevDay = "Friday";
    return prevDay;
if (weekDay == "Sunday")
    prevDay = "Saturday";
    return prevDay;
}

// part f
void dayType::addDay(int nDays) 
{

}


// part a
void dayType::setDay(string d)
{
weekDay = d;
}
// part c
string dayType::getDay() const 
{
return weekDay;
}

// part g
dayType::dayType() 
{
weekDay = "Sunday";
}

// part g
dayType::dayType(string d) 
{
setDay(d);
}

//dayType.h
#ifndef H_dayType 
#define H_dayType
 
#include <string>  

using namespace std;

class dayType
{
public:
    static string weekDays[7];
    void print() const;
    string nextDay() const;
    string prevDay() const;
    void addDay(int nDays);

    void setDay(string d);
    string getDay() const;

    dayType();
    dayType(string d);

private:
    string weekDay;
};

#endif

//main.cpp
#include <iostream>
#include <string>
  
#include "dayType.h" 
 
using namespace std;

int main() 
{
    dayType myDay("Monday");
    dayType temp("Sunday");

    myDay.print();
    cout << endl;

    cout << myDay.prevDay() << endl;

    cout << myDay.nextDay() << endl;
    
    temp.print(); 
    
    cout << endl;

    cout << temp.prevDay() << endl; //This is where the code doesn't work

    cout << temp.nextDay() << endl; //Also here

    return 0;
}
Last edited on
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
// your
string dayType::nextDay() const 
{
  string nextDay;
  if (weekDay == "Monday")
    nextDay = "Tuesday";
    return nextDay;
  if (weekDay == "Tuesday")
    nextDay = "Wednesday";
    return nextDay;
  // ...
}

// is same as
string dayType::nextDay() const 
{
  string nextDay;
  if (weekDay == "Monday") {
    nextDay = "Tuesday";
  }

  return nextDay; // we always return here

  if (weekDay == "Tuesday") {
    nextDay = "Wednesday";
  }

  return nextDay;
  // ...
}

// is same as
string dayType::nextDay() const 
{
  string nextDay;
  if (weekDay == "Monday") {
    nextDay = "Tuesday";
  }

  return nextDay;
}


Did you mean:
1
2
3
4
5
6
7
8
9
10
string dayType::nextDay() const 
{
  string nextDay = "Monday"; // always have some valid day

  if      (weekDay == "Monday" ) nextDay = "Tuesday";
  else if (weekDay == "Tuesday") nextDay = "Wednesday";
  else if // ...

  return nextDay;
}
Thank you so much for the help!!
Topic archived. No new replies allowed.