assinging an object into a private string

I need to make my monday object print Monday as Mon to the console, but I am stuck at this point can anyone provide some insite?

This is what I have for errors:

s\week1lab_benjamin_horne1\week1lab_benjamin_horne1\dayoftheweek.cpp(46) : error C2664: 'DayOfTheWeek::setDay' : cannot convert parameter 1 from 'DayOfTheWeek' to 'std::string'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\ben\documents\visual studio 2008\projects\week1lab_benjamin_horne1\week1lab_benjamin_horne1\dayoftheweek.cpp(47) : error C2146: syntax error : missing ';' before identifier 'monday'
1>c:\users\ben\documents\visual studio 2008\projects\week1lab_benjamin_horne1\week1lab_benjamin_horne1\dayoftheweek.cpp(49) : error C2143: syntax error : missing ';' before 'return'






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
//DayOfTheWeek.cpp : main project file.

#include <string>
#include <iostream>

using namespace std;

class DayOfTheWeek
{
public:

	void setDay(string);

	void printDay() const;

	string getDay() const;


private:
	string day;

};

void DayOfTheWeek::printDay()const
{
	cout << "The value of the" << day <<  "object is" << endl;
	
}

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

}

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

int main()
{
	DayOfTheWeek monday;
	DayOfTheWeek tuesday;

	monday.setDay(monday)
		monday.printDay()
 
return 0;
}
Last edited on
A few things:

1
2
3
4
5
void DayOfTheWeek::setDay(string)
{
	day = day;

}


This function does nothing. You're just taking day and assigning it to itself. What you probably meant to do was assign the passed parameter to day. So you should give your parameter a name and then assign it appropriately.

As for your errors:

 
monday.setDay(monday)


2 things are wrong with this.

1) You're missing the semicolon at the end
2) 'monday' isn't a string, it's a DayOfTheWeek object. setDay needs to take a string as a parameter. So you probably meant to do something like this instead:

 
monday.setDay("Monday"); // give it a string 



monday.printDay() is also missing a semicolon.
Thanks, it compiled. Please forgive me of my nieveness but how do you assign the passed parameter to day?
you have to give the passed parameter a name:

1
2
3
4
void DayOfTheWeek::setDay(string daystring)  // <- now it's named 'daystring'
{
  //...
}


Now the passed parameter is named 'daystring' so you can use it in expressions. Now if you want to assign whatever was passed to the function to your 'day' member, you can just assign 'daystring' to 'day'.
Thanks. It compiled and the parameter is being passed however I a trying to convert the sting that is bing passed into an abriviated Monday and Tuesday, but I am getting a unidentified identifiers.

1
2
3
4
5
6
void DayOfTheWeek::printDay()const
{
	if (day == Monday)
		cout << "The value of the " << day << " object is Mon" << endl;
	if (day == Tuesday)
		cout << "The value of the " << day << " object is Tues" << endl;


Heres the fix from earlier:

1
2
3
4
void DayOfTheWeek::setDay(string d)
{
day = d;
}
You are trying to compare day with a string such as "Monday" and not a variable such as Monday. You should use quotation marks (Ex. if (day == "Monday")).

Edit: Since you generally want to print the 3/4 first letters of the word you should take a look at substr [http://cplusplus.com/reference/string/string/substr].
Last edited on
Thanks. I managed to figure it out.

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
//DayOfTheWeek.cpp : main project file.

#include <string>
#include <iostream>

using namespace std;



class DayOfTheWeek
{
public:

	void setDay(string);

	void printDay() const;

	string getDay() const;


private:
	string day;

};

void DayOfTheWeek::printDay()const
{
		cout << "The value of the " << day << " object is "; 
		if (day == "Monday")
			cout << "Mon" << endl;
		else cout << "Tues" << endl;
}

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


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

int main()
{
	DayOfTheWeek monday;
	DayOfTheWeek tuesday;

	monday.setDay("Monday");
	monday.printDay();

	tuesday.setDay("Tuesday");
	tuesday.printDay();
 
return 0;
}
Your solution is great for your specific test cases but what if you wanted to create a DayOfTheWeek by the name of "Saturday"? It would still be abbreviated to "Tue" which isn't the output you're expecting.
I assume that by now you know how to fix that (and maybe you haven't posted the complete code here) but I still felt this was worth mentioning.
It was just to test. I eventually fixed the code and it worked out great. Thanks for the heads up though. In case you were wondering here is the code with the applied fixes.


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

using namespace std;



class DayOfTheWeek
{
public:

	void setDay(string);

	void printDay() const;

	string getDay() const;


private:
	string day;

};

void DayOfTheWeek::printDay()const
{
		cout << "The value of the " << day << " object is "; 
		if (day == "Monday")
			cout << "Mon" << endl;
	
		if (day == "Tuesday")
			cout << "Tues" << endl;

		if (day == "Wednesday")
			cout << "Wed" << endl;

		if (day == "Thursday")
			cout << "Thur" << endl;

		if (day == "Friday")
			cout << "Fri" << endl;

		if (day == "Saturday")
			cout << "Sat" << endl;

		if (day == "Sunday")
			cout << "Sun" << endl;

}

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


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

int main()
{
	DayOfTheWeek monday;
	DayOfTheWeek tuesday;
	DayOfTheWeek wednesday;
	DayOfTheWeek thursday;
	DayOfTheWeek friday;
	DayOfTheWeek saturday;
	DayOfTheWeek sunday;
	

	monday.setDay("Monday");	
	monday.printDay();

	tuesday.setDay("Tuesday");
	tuesday.printDay();
 
	wednesday.setDay("Wednesday");
	wednesday.printDay();

	thursday.setDay("Thursday");
	thursday.printDay();

	friday.setDay("Friday");
	friday.printDay();

	saturday.setDay("Saturday");
	saturday.printDay();

	sunday.setDay("Sunday");
	sunday.printDay();

return 0;
}
Topic archived. No new replies allowed.