Using class

Hi!I'm having some issues. I can't figure out how to pass my variables to each function. I also wonder if there's a better way to find the previous day and next day than using a million if statements? I need to be able to add numbers to the days. I'm not sure what to do. This is what I am supposed to do.

Write a definition of the class dayType that implements the day of the week in a program. The class dayType should store the day such as sun for Sunday. The program should be able to perform the following operations on an object of type dayType:

a. Set the day
b. Print the day
c. Return the day
d. Return the next day
e. Return the previous day
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. If the day is Tuesday and we add 13 days, the day to be returned is Monday.
g. Add appropriate constructers

Write a definition of the functions to implement the operations for the class dayType. Also, write a program to test various operations on this class.

This is the code I have so far:

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

using namespace std; 

 class dayType  {
 	public:  
	void print()  const;  
    string setDay(string day);     
	string getPrevDay (string day)  const; 
	string getNextDay() const;
	dayType(); 
	dayType(string day);  
	private:  
	string mon;  
	string tue;
	string wed;
	string thur;
	string fri;
	string sat;
	string sun; 
	string day; 
	string prevDay;
	string nextDay;
};


void dayType::print()  const  
{  
	cout<< day;  
}  
string dayType::setDay(string day)  
{ 
	//string mon, tue, wed, thur, fri, sat, sun;
	if ((day == "Monday") || (day == "monday"))
	{
		day = mon;
	}
	else if ((day == "Tuesday") || (day == "tuesday"))
	{
		day = tue;
	}
	else if ((day == "Wednesday") || (day == "wednesday"))
	{
		day = wed;
	}
	else if ((day == "Thursday") || (day == "thursday"))
	{
		day = thur;
	}
	else if ((day == "Friday") || (day == "friday"))
	{
		day = fri;
	}
	else if ((day == "Saturday") || (day == "saturday"))
	{
		day = sat;
	}
	else if ((day == "Sunday") || (day == "sunday"))
	{
		day = sun;
	}
	else
		cout << "Not a valid day. " << endl;
			
	return day;
		
}   
string dayType::getPrevDay(string day)  const  
{  
	if (day == mon)
	{
		prevDay == sun;
	}
	else if (day == tue)
	{
		prevDay == mon;
	}
	
	return prevDay;  
} 
string dayType::getNextDay() const
{
	return nextDay;
}
 //Defaultconstructor  
dayType::dayType()  
{ 
	day = "";  
 
}  
//Constructorwithparameters 
dayType::dayType(string day)  
{
	string today;
	day = today;  
}

int main()
{
	string day;
	
	cout << "Enter a day of the week: ";
	cin >> day;
	
	dayType today;
	today.setDay(day);
	today.getPrevDay(day);
	today.getNextDay();
	today.print();
	
	return 0;	
}

Last edited on
On lines 22 to 26, replace "dayType" with "today".

The setDay and getDay functions don't make any sense, and the way you call them makes even less sense.
This is what I have now..

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

using namespace std; 

 class dayType  {
 	public:  
	void print()  const;  //Functiontooutputthefirstnameandlast
    void setDay(string day);   
	string getDay()  const;  //Functiontoreturnthefirstname.  //Postcondition:ThevalueoffirstNameisreturned.  
	string getPrevDay()  const;  //Functiontoreturnthelastname.  //Postcondition:ThevalueoflastNameisreturned.  
	dayType();  //Defaultconstructor  //SetsfirstNameandlastNametonullstrings.  //Postcondition:firstName="";lastName="";  
	dayType(string day);  //Constructorwithparameters.  //SetsfirstNameandlastNameaccordingtotheparameters.  //Postcondition:firstName=first;lastName=last;  
	private:  
	string mon;  
	string tue;
	string wed;
	string thur;
	string fri;
	string sat;
	string sun; 
	string day; 
	string prevDay;
};


void dayType::print()  const  
{  
	cout<< day;  
}  
void dayType::setDay(string day)  
{ 
	string mon, tue, wed, thur, fri, sat, sun;
	if ((day == "Monday") || (day == "monday"))
	{
		day = mon;
	}
	else if ((day == "Tuesday") || (day == "tuesday"))
	{
		day = tue;
	}
	else if ((day == "Wednesday") || (day == "wednesday"))
	{
		day = wed;
	}
	else if ((day == "Thursday") || (day == "thursday"))
	{
		day = thur;
	}
	else if ((day == "Friday") || (day == "friday"))
	{
		day = fri;
	}
	else if ((day == "Saturday") || (day == "saturday"))
	{
		day = sat;
	}
	else if ((day == "Sunday") || (day == "sunday"))
	{
		day = sun;
	}
	else
		cout << "Not a valid day. " << endl;

}  
string dayType::getDay()  const 
{  
	return day;  
}  
string dayType::getPrevDay()  const  
{  
	return prevDay;  
} 
 //Defaultconstructor  
dayType::dayType()  
{ 
	day = "";  
 
}  
//Constructorwithparameters 
dayType::dayType(string day)  
{
	string today;
	day = today;  
}

int main()
{
	string day;
	
	cout << "Enter a day of the week: ";
	cin >> day;
	
	dayType today;
	today.setDay(day);
	today.getDay();
	today.getPrevDay();
	today.print();
	
	return 0;	
}
^ What L B said.


But let me try to point you in the correct direction here.

First of all, you can't use private variables outside a class out. This is what's causing an error when you attempting to call mon, tue, wed, thur, fri, sat, sun in your main function. You will need to declare strings in your main function to be passed into the "setDay" and "getDay" functions.

Additionally, your setDay and getDay functions don't actually do anything. The parameters state that they take a string but no variable follows and it isn't actually set to the private variables in the class. You'll need to do something like the following:

1
2
3
4
5
void setDay(string m, string t,ect..){
mon=m;
tue=t;
..
}


However, I think your setDay is only meant to have 1 parameter as mutator functions are typically meant to set 1 thing at a time in order for the get function to make sense (get functions can only return 1 value at a time).

So, unless the assignment specifically says so I would highly suggest doing the following:

1
2
3
void setDay(string d){
day = d;
}


Then, of course, you would have to make a private variable called "day".

Your getDay function should not be a void either. It is meant to have a return type and it just returns the private variable in the class. Example:

1
2
3
string getDay(){
return day;
}


So you can enter any day of the week in the parameter of your setDay function like today.setDay("Monday") and then run the get function to display it. So, cout<<today.getDay().

If you call setDay again you can change the value of "day" and use the get function to display the new value.

For more information on classes, I would suggest the following link:
http://www.cplusplus.com/doc/tutorial/classes/
Thank you! I have changed my code a bit (to what I think works). It at least compiles. I'm trying to figure out how to get the previous day and the next day without having to use a bunch of if else statements. I thought about using a character array, but I'm not really sure how that would work? I need to be able to add numbers to the days to get a new day (monday plus 3 days is thursday) something like that.. Not sure what the best way to do it is? Also, not sure how I can pass my day variables to each function.. Thanks for all the help!

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

using namespace std; 

 class dayType  {
 	public:  
	void print()  const;  
    string setDay(string day);     
	string getPrevDay (string day)  const; 
	string getNextDay() const;
	dayType(); 
	dayType(string day);  
	private:  
	string mon;  
	string tue;
	string wed;
	string thur;
	string fri;
	string sat;
	string sun; 
	string day; 
	string prevDay;
	string nextDay;
};


void dayType::print()  const  
{  
	cout<< day;  
}  
string dayType::setDay(string day)  
{ 
	//string mon, tue, wed, thur, fri, sat, sun;
	if ((day == "Monday") || (day == "monday"))
	{
		day = mon;
	}
	else if ((day == "Tuesday") || (day == "tuesday"))
	{
		day = tue;
	}
	else if ((day == "Wednesday") || (day == "wednesday"))
	{
		day = wed;
	}
	else if ((day == "Thursday") || (day == "thursday"))
	{
		day = thur;
	}
	else if ((day == "Friday") || (day == "friday"))
	{
		day = fri;
	}
	else if ((day == "Saturday") || (day == "saturday"))
	{
		day = sat;
	}
	else if ((day == "Sunday") || (day == "sunday"))
	{
		day = sun;
	}
	else
		cout << "Not a valid day. " << endl;
			
	return day;
		
}   
string dayType::getPrevDay(string day)  const  
{  
	if (day == mon)
	{
		prevDay == sun;
	}
	else if (day == tue)
	{
		prevDay == mon;
	}
	
	return prevDay;  
} 
string dayType::getNextDay() const
{
	return nextDay;
}
 //Defaultconstructor  
dayType::dayType()  
{ 
	day = "";  
 
}  
//Constructorwithparameters 
dayType::dayType(string day)  
{
	string today;
	day = today;  
}

int main()
{
	string day;
	
	cout << "Enter a day of the week: ";
	cin >> day;
	
	dayType today;
	today.setDay(day);
	today.getPrevDay(day);
	today.getNextDay();
	today.print();
	
	return 0;	
}
Last edited on
I hate to tell you this, but with classes and the given information, you may have to make a couple more if statements than you would like.

I've given you an example of what will need to be done. The code runs fine but you'll need to add the remaining days to the if statements.

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

using namespace std; 

 class dayType  {
 	public:  
	void print()  const;  //Functiontooutputthefirstnameandlast
	void setDay(string day);     
	int getPrevDay(); 
	int getNextDay();
	dayType();  //Defaultconstructor  //SetsfirstNameandlastNametonullstrings.  //Postcondition:firstName="";lastName="";  
	dayType(string day);  //Constructorwithparameters.  //SetsfirstNameandlastNameaccordingtotheparameters.  //Postcondition:firstName=first;lastName=last;  
	private:  
     int num;
	string mon;  
	string tue;
	string wed;
	string thur;
	string fri;
	string sat;
	string sun; 
	string day; 
	string prevDay;
	string nextDay;
};


void dayType::print()  const  
{  
	cout<< "Today is: "<< day <<endl;  
}  
void dayType::setDay(string d)  
{ 
    
	if ((d == "Monday") || (d == "monday"))
	{
		num=0;
		day=d;	
	}
	else if ((d == "Tuesday") || (d == "tuesday"))
	{
		num=1;
		day=d;	
	}
	else if ((d == "Wednesday") || (d == "wednesday"))
	{
		num=2;
		day=d;	
	}
	else if ((d == "Thursday") || (d == "thursday"))
	{
		num=3;
		day=d;	
	}
	else if ((d == "Friday") || (d == "friday"))
	{
		num=4;
		day=d;	
	}
	else if ((d == "Saturday") || (d == "saturday"))
	{
		num=5;
		day=d;	
	}
	else if ((d == "Sunday") || (d == "sunday"))
	{
		num=6;
		day=d;	
	}
	else
	    cout<<"Not a valid day. "<<endl;

	
}   
int dayType::getPrevDay()    
{  
     num--;
	return num;  
} 
int dayType::getNextDay() 
{
     num++;
	return num;
}

 //Defaultconstructor  
dayType::dayType()  
{ 
	day = "";  
 
}  
//Constructorwithparameters 
dayType::dayType(string day)  
{
	string today;
	day = today;  
}

int main()
{
	string day;
	int dayNum;


	cout << "Enter a day of the week: ";
	cin >> day;
	
	dayType today;
	today.setDay(day);
	dayNum=today.getPrevDay();

	cout<<"Yesterday was: ";
	if (dayNum==0){
	    cout<<"Monday"<<endl;
	}
	if (dayNum==1){
	    cout<< "Tuesday"<<endl;
	}
	if (dayNum==2){
	    cout<<"Wednesday"<<endl;
	}


	today.print();
	
	system("pause");
	return 0;	
}
Thanks so much! It (sorta) works now! My adding is off but other than that I think it's good!! Thank you!!

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <iostream>
#include<string>  

using namespace std; 

 class dayType  {
 	public:  
	void print()  const;  
	void setDay(string day);     
	int getPrevDay(); 
	int getNextDay();
	void randDay(int num);
	dayType();  
	dayType(string day);
	private:  
    int num;
	string today; 
	string prevDay;
	string nextDay;
};


void dayType::print()  const  
{  
	cout<< "Today is: "<< today <<endl;  
}  
void dayType::setDay(string day)  
{ 
    
	if ((day == "Monday") || (day == "monday"))
	{
		num=0;
		today=day;	
	}
	else if ((day == "Tuesday") || (day == "tuesday"))
	{
		num=1;
		today=day;	
	}
	else if ((day == "Wednesday") || (day == "wednesday"))
	{
		num=2;
		today=day;	
	}
	else if ((day == "Thursday") || (day == "thursday"))
	{
		num=3;
		today=day;	
	}
	else if ((day == "Friday") || (day == "friday"))
	{
		num=4;
		today=day;	
	}
	else if ((day == "Saturday") || (day == "saturday"))
	{
		num=5;
		today=day;	
	}
	else if ((day == "Sunday") || (day == "sunday"))
	{
		num=6;
		today=day;	
	}
	else
	    cout<<"Not a valid day. "<<endl;

	
}   
int dayType::getPrevDay()    
{  
     num--;
	return num;  
} 
int dayType::getNextDay() 
{
     num++;
	return num;
}

 //Defaultconstructor  
dayType::dayType()  
{ 
	today = "";  
 
}  
//Constructorwithparameters 
dayType::dayType(string day)  
{
	string today;
	day = today;  
}
void randDay(int num)
{	
	int random, add;
	cout << "How many days would you like to add to the current day? ";
	cin >> add;
	if (add > 6)
	{
		random = num + add / 7;
	}
	else
	random = num + add;
	
		if (random==0) 
	{
	   cout << "In " << add << " days, it will be " << "Sunday";
	}
	else if (random==1)
	{
	    cout << "In " << add << " days, it will be " << "Monday";
	}
	else if (random==2)
	{
	   cout << "In " << add << " days, it will be " << "Tuesday";
	}
	else if (random==3)
	{
		cout << "In " << add << " days, it will be " << "Wednesday";
	}
	else if (random ==4)
	{
		cout << "In " << add << " days, it will be " << "Thursday";
	}
	else if (random == 5)
	{
		cout << "In " << add << " days, it will be " << "Friday";
	}
	else 
		cout << "In " << add << " days, it will be " << "Saturday";
	
}

int main()
{
	string today;
	int dayNum, numDay, newNum;


	cout << "Enter the current day of the week: ";
	cin >> today;
	
	dayType week;
	week.setDay(today);
	dayNum = week.getPrevDay();
	numDay = week.getNextDay();
	newNum = week.getNextDay();

	cout<<"Yesterday was: ";
	if (dayNum==0) 
	{
	    cout<< "Monday" << endl;
	}
	else if (dayNum==1)
	{
	    cout<< "Tuesday" << endl;
	}
	else if (dayNum==2)
	{
	    cout<< "Wednesday" << endl;
	}
	else if (dayNum==3)
	{
		cout << "Thursday" << endl;
	}
	else if (dayNum ==4)
	{
		cout << "Friday" << endl;
	}
	else if (dayNum == 5)
	{
		cout << "Saturday" << endl;
	}
	else 
		cout << "Sunday" << endl;
		
	week.print();

	cout<<"Tomorrow is: ";
	if (numDay==0) 
	{
	    cout<< "Tuesday" << endl;
	}
	else if (numDay==1)
	{
	    cout<< "Wednesday" << endl;
	}
	else if (numDay==2)
	{
	    cout<< "Thursday" << endl;
	}
	else if (numDay==3)
	{
		cout << "Friday" << endl;
	}
	else if (numDay ==4)
	{
		cout << "Saturday" << endl;
	}
	else if (numDay == 5)
	{
		cout << "Sunday" << endl;
	}
	else 
		cout << "Monday" << endl;
		
	randDay(newNum);

	
	
	
	return 0;	
}

You're welcome! It looks good. Let us know if you need any more help.
Some code to consider:

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

struct day_type
{
    enum weekday { SUN, MON, TUE, WED, THU, FRI, SAT };

    day_type(weekday day) : _day(day) {}
    day_type(std::string day);

    day_type prev() const { return dec(_day); }
    day_type next() const { return inc(_day); }

    day_type operator+(unsigned n_days) { return weekday((_day + n_days) % 7); }
    day_type operator-(unsigned n_days) { return *this; /* I leave this to you! */ }

    operator std::string() const;

private:
    weekday _day;

    static weekday dec(weekday d) { return d == 0 ? weekday(6) : weekday(d - 1); }
    static weekday inc(weekday d) { return weekday((d + 1) % 7); }
};

std::string to_string(day_type::weekday day)
{
    static const char* days[] =
    {
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
    };

    return days[day];
}

void to_lower(std::string& str)
{
    for (auto&ch : str)
        ch = std::tolower(ch);
}

day_type::operator std::string() const
{
    return to_string(_day);
}

day_type::day_type(std::string day)
{
    static const std::string days[] =
    {
        "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
    };

    unsigned matches = 0;
    unsigned match_index;

    to_lower(day);

    for (unsigned index = 0; index != 7 && matches <= 1; ++index)
    {
        if (days[index].find(day) != std::string::npos)
        {
            match_index = index;
            ++matches;
        }
    }

    if (matches != 1)
        throw std::invalid_argument("\"" + day + "\" is not a valid day");

    _day = weekday(match_index);
}

int main()
{
    const char* day_prompt = "Enter a day of the week (quit to stop):\n";
    std::string today;

    while (std::cout << day_prompt && std::cin >> today && today != "quit")
    {
        try
        {
            day_type day(today);

            std::cout << "Day is " << std::string(day) << ".\n";
            std::cout << "Previous day was " << std::string(day.prev()) << ".\n";
            std::cout << "Next day is " << std::string(day.next()) << ".\n";


            std::cout << "How many days would you like to add?\n";
            unsigned to_add;
            std::cin >> to_add;

            std::cout << "In " << to_add << " days it will be ";
            std::cout << std::string(day + to_add) << ".\n\n";
        }

        catch (std::exception & ex)
        {
            std::cout << "ERROR: " << ex.what() << "\n\n";
        }
    }
}


http://ideone.com/SV7gyl
Topic archived. No new replies allowed.