Do I need parameters in this class code?

The following code is intended to output the day that comes before and after the dayName. However, I run into trouble when I have saturdays and sundays involved; I'm wondering why returnNext() is returning the same day instead of what comes after it, and why returnprevious() does the opposite. Do I need to include the dayNum in the parameters or something?

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

class dayType
{
    public:
    string dayName = "";
    int dayNum = 0;
    dayType(string dayName) //Constructor returns dayNum for function calls.  
    {
        if(dayName == "Sunday") {dayNum = 1;}
        else if(dayName == "Monday") {dayNum = 2;}
        else if(dayName == "Tuesday") {dayNum = 3;}
        else if(dayName == "Wednesday") {dayNum = 4;}
        else if(dayName == "Thursday") {dayNum = 5;}
        else if(dayName == "Friday") {dayNum = 6;}
        else if(dayName == "Saturday") {dayNum = 7;}
        else {cout << "Day may have invalid value:  Error code 1.  Exiting...";}
    }
    string returnDayName()
    {
        switch(dayType::dayNum)
        {
            case 1:  return "Sunday";
            case 2:  return "Monday";
            case 3:  return "Tuesday";
            case 4:  return "Wednesday";
            case 5:  return "Thursday";
            case 6:  return "Friday";
            case 7:  return "Saturday";
            default: return "Day may have invalid value:  Error code 1.  Exiting...";
        }
    }
    string returnNext()
    {
        dayType::dayNum += 1;
        if(dayType::dayNum > 7) {dayType::dayNum -= 7;}
        switch(dayType::dayNum)
        {
            case 1:  return "Sunday";
            case 2:  return "Monday";
            case 3:  return "Tuesday";
            case 4:  return "Wednesday";
            case 5:  return "Thursday";
            case 6:  return "Friday";
            case 7:  return "Saturday";
            default: return "Day may have invalid value:  Error code 1.  Exiting...";
        }
    }
    string returnPrevious()
    {
        dayType::dayNum -= 1;
        if(dayType::dayNum < 1) {dayType::dayNum = 1;}
        
        if(dayType::dayNum > 7) {dayType::dayNum -= 7;}
        
        switch(dayType::dayNum)
        {
            case 1:  return "Sunday";
            case 2:  return "Monday";
            case 3:  return "Tuesday";
            case 4:  return "Wednesday";
            case 5:  return "Thursday";
            case 6:  return "Friday";
            case 7:  return "Saturday";
            default: return "Day may have invalid value:  Error code 1.  Exiting...";
        }
    }
    string addDay(int daysAdded)
    {
        dayType::dayNum += daysAdded;
        if(dayType::dayNum > 7) {dayType::dayNum -= 7;}
        switch(dayType::dayNum)
        {
            case 1:  return "Sunday";
            case 2:  return "Monday";
            case 3:  return "Tuesday";
            case 4:  return "Wednesday";
            case 5:  return "Thursday";
            case 6:  return "Friday";
            case 7:  return "Saturday";
            default: return "Day may have invalid value:  Error code 1.  Exiting...";
        }
    }
};

int main() 
{
    dayType myDay("Saturday");
    dayType temp("Sunday");
    //previous and next day, when adding 1 to sat, and subtracting 1 from sunday, return the same day instead of next/previous repsectively.  debu
    cout << "Day name:  " << myDay.returnDayName() << endl;
    cout << "Previous day:  " << myDay.returnPrevious() << endl;
    cout << "Next day:  " << myDay.returnNext() << endl;

    cout << "Day name:  " << temp.returnDayName() << endl;
    cout << "Previous day:  " << temp.returnPrevious() << endl;
    cout << "Next day:  " << temp.returnNext() << endl;

    return 0;
}
Take a good look at this line.
if(dayType::dayNum < 1) {dayType::dayNum = 1;}
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
#include <iostream>
#include <string>
//#include "dayType.h"
using namespace std;

class dayType
{
    public:
    string dayName = "";
    int dayNum = 0;
    dayType(string dayName) //Constructor returns dayNum for function calls.  
    {
        if(dayName == "Sunday") {dayNum = 1;}
        else if(dayName == "Monday") {dayNum = 2;}
        else if(dayName == "Tuesday") {dayNum = 3;}
        else if(dayName == "Wednesday") {dayNum = 4;}
        else if(dayName == "Thursday") {dayNum = 5;}
        else if(dayName == "Friday") {dayNum = 6;}
        else if(dayName == "Saturday") {dayNum = 7;}
        else {cout << "Day may have invalid value:  Error code 1.  Exiting...";}
    }
    string returnDayName()
    {
        switch(dayType::dayNum)
        {
            case 1:  return "Sunday";
            case 2:  return "Monday";
            case 3:  return "Tuesday";
            case 4:  return "Wednesday";
            case 5:  return "Thursday";
            case 6:  return "Friday";
            case 7:  return "Saturday";
            default: return "Day may have invalid value:  Error code 1.  Exiting...";
        }
    }
    string returnNext()
    {
        int temp = dayNum + 1;
        if(temp > 7) {temp -= 7;}
        switch(temp)
        {
            case 1:  return "Sunday";
            case 2:  return "Monday";
            case 3:  return "Tuesday";
            case 4:  return "Wednesday";
            case 5:  return "Thursday";
            case 6:  return "Friday";
            case 7:  return "Saturday";
            default: return "Day may have invalid value:  Error code 1.  Exiting...";
        }
    }
    string returnPrevious()
    {
        int temp = dayNum - 1;
        if(temp < 1) {temp += 7;}
        switch(temp)
        {
            case 1:  return "Sunday";
            case 2:  return "Monday";
            case 3:  return "Tuesday";
            case 4:  return "Wednesday";
            case 5:  return "Thursday";
            case 6:  return "Friday";
            case 7:  return "Saturday";
            default: return "Day may have invalid value:  Error code 1.  Exiting...";
        }
    }
    string addDay(int daysAdded)
    {
        int temp = dayNum + daysAdded;
        if(temp > 7) {temp -= 7;}
        switch(temp)
        {
            case 1:  return "Sunday";
            case 2:  return "Monday";
            case 3:  return "Tuesday";
            case 4:  return "Wednesday";
            case 5:  return "Thursday";
            case 6:  return "Friday";
            case 7:  return "Saturday";
            default: return "Day may have invalid value:  Error code 1.  Exiting...";
        }
    }
};

int main() 
{
    dayType myDay("Saturday");
    dayType temp("Sunday");
    //previous and next day, when adding 1 to sat, and subtracting 1 from sunday, return the same day instead of next/previous repsectively.  debu
    cout << "Day name:  " << myDay.returnDayName() << endl;
    cout << "Previous day:  " << myDay.returnPrevious() << endl;
    cout << "Next day:  " << myDay.returnNext() << endl;

    cout << "Day name:  " << temp.returnDayName() << endl;
    cout << "Previous day:  " << temp.returnPrevious() << endl;
    cout << "Next day:  " << temp.returnNext() << endl;

    return 0;
}
Last edited on
In the function returnNext(), dayNum++ is executed. Let's say the dayNum was seven, now 8. The block of code evaluating whether or not dayNum is 8 runs, if true setting dayNum to 1. Somehow, it still returns saturday, or dayNum = 7 basically. If I set the day to sunday, it returns the value sunday as well.

Is dayNum not the same throughout each code block? I thought, maybe if it's 7 when constructed, that be 7 each time a new code block is loaded.

Edit: Lesson learned, if you have to use the same variable in multiple code blocks but you want it to be the same, make a dummy copy of it to use.
Last edited on
Topic archived. No new replies allowed.