My variable will not cout

I am trying to get line 84 to print to screen.

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
#include <iostream>
using namespace std;
#include <string>

class DayOfTheWeek
{
public:
	
	void setDay(string);
	string getDay()const;
	void printDay() const;
	string plusOneDay();
	string minusOneDay();
	string addDays(int);
private:
	string newDay;
	int incr;
	int day; 
	int toNumber(string);
	string toName(int);
}; 


void DayOfTheWeek:: setDay(string day1 )
	{
		newDay = day1;
	} // end of function

string DayOfTheWeek::getDay()const
	{
		return newDay; // returns the DayOfTheWeek
	}

void DayOfTheWeek:: printDay() const
{
		cout << "The value of the " << getDay() << 
		" object is " << getDay() << endl;
}
 
string DayOfTheWeek::plusOneDay()
{
	return toName(day + 1);
}

string DayOfTheWeek::minusOneDay()
{
	
	return toName(day - 1 );
}

string DayOfTheWeek::addDays(int incr1)
{
	incr = incr1;
	int idx = day + incr;
	if (idx < 0 ) idx += 7;
	return toName(idx);
}

int DayOfTheWeek::toNumber(string name)
{
	if (name == "Monday") return 0;
	if (name == "Tuesday") return 1;
	if (name == "Wednesday") return 2;
	if (name == "Thursday") return 3;
	if (name == "Friday") return 4;
	if (name == "Saturday") return 5;
	if (name == "Sunday") return 6;
}

string DayOfTheWeek::toName(int idx)
{
	idx = idx % 6;

	switch (idx)
	{
		case 0: return "Monday"; break;
		case 1: return "Tuesday"; break;
		case 2: return "Wednesday"; break;
		case 3: return "Thursday"; break;
		case 4: return "Friday"; break;
		case 5: return "Saturday"; break;
		case 6: return "Sunday"; break;
	}
	cout << idx << endl;
}




int main()
{
	DayOfTheWeek Monday;
	Monday.setDay("Monday");
	Monday.getDay();
	Monday.printDay();
	Monday.plusOneDay();
	Monday.minusOneDay();
	Monday.addDays(5);
	Monday.printDay();

	cin.get();
	cin.get();
	

	return 0;

}
It will only display the value of idx if it is a negative value or if it is greater than 6. Since in line 72 you assign the modulus of six (should be seven, not six), the only possible values for idx are 0, 1, 2, 3, 4 and 5. All these are covered in the switch with return statements that make the code skip line 84.
Also, you could move your cout << idx << endl; above the switch().
Also, there's no need for break; after a return because a return will cause your function to terminate.

Nothing after a return X; instruction is executed anymore, which is why your cout doesn't work in the first place.
Last edited on
Topic archived. No new replies allowed.