1,105,310 Community Members

Homework Help!

Member Avatar
King_Alucard
Newbie Poster
8 posts since Jul 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I got a homework problem, I kind of got the idea for the code but I'm getting errors when I compile it, can somebody give me an idea of what I need to fix so I can make this work.

The task is to write a program that contains a class that implements the days of the week. The program should be able to perform the following on an object of the class:
1. set the day
2. print the day to the computer monitor (not the printer)
3. return the day

#include <iostream>	// Required by cout command
#include <string>		// Required by string data type
using namespace std;	// Default namespace defining library functions (e.g. cout, endl)

class DayOfTheWeek
{
public: 	//these are the member functions
	void setDay(string);		//set the day of the week
	void printDay() const;	//print the day of the week
	string getDay() const;	//get the day of the week
	DayOfTheWeek();		//constructor

private:	//this section is for data members only
	string day;	//this is where the value is stored
};

DayOfTheWeek::DayOfTheWeek()
{
	day = "default";	// sets the data member by default
}

string DayOfTheWeek::getDay() const
{
	string simDay;

	cout << "Enter The Day Of The Week:\n";
	cin >> simDay;

	return simDay;
}

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

void DayOfTheWeek::printDay() const
{
	cout << day;
}

int main()
{
	string DayOfTheWeek = " ";	// Local variables to hold object value

	DayOfTheWeek First;		// Instantiate the first object of class DaysOfTheWeek
	DayOfTheWeek Second;	// Instantiate the second object of class DaysOfTheWeek

	First.getDay();				// get the value of the first object
	First.setDay(DayOfTheWeek); 	// set the value of the object
	Second.getDay();				// get the value of the tuesday object
	Second.setDay(DayOfTheWeek);	// set the value of the object

	cout << "The Value For The First Object is: " << First.printDay() << endl;		// print out the value of the monday object
	cout << "The Value For The Second Day is: " << Second.printDay() << endl;	// print out the value of the tuesday object

	return 0;
}

These are the errors I get:

1>------ Build started: Project: Lab1 test, Configuration: Debug Win32 ------
1>Compiling...
1>Lab1 test.cpp
1>.\Lab1 test.cpp(46) : error C2146: syntax error : missing ';' before identifier 'First'
1>.\Lab1 test.cpp(46) : error C2065: 'First' : undeclared identifier
1>.\Lab1 test.cpp(47) : error C2146: syntax error : missing ';' before identifier 'Second'
1>.\Lab1 test.cpp(47) : error C2065: 'Second' : undeclared identifier
1>.\Lab1 test.cpp(49) : error C2065: 'First' : undeclared identifier
1>.\Lab1 test.cpp(49) : error C2228: left of '.getDay' must have class/struct/union
1> type is ''unknown-type''
1>.\Lab1 test.cpp(50) : error C2065: 'First' : undeclared identifier
1>.\Lab1 test.cpp(50) : error C2228: left of '.setDay' must have class/struct/union
1> type is ''unknown-type''
1>.\Lab1 test.cpp(51) : error C2065: 'Second' : undeclared identifier
1>.\Lab1 test.cpp(51) : error C2228: left of '.getDay' must have class/struct/union
1> type is ''unknown-type''
1>.\Lab1 test.cpp(52) : error C2065: 'Second' : undeclared identifier
1>.\Lab1 test.cpp(52) : error C2228: left of '.setDay' must have class/struct/union
1> type is ''unknown-type''
1>.\Lab1 test.cpp(54) : error C2065: 'First' : undeclared identifier
1>.\Lab1 test.cpp(54) : error C2228: left of '.printDay' must have class/struct/union
1> type is ''unknown-type''
1>.\Lab1 test.cpp(55) : error C2065: 'Second' : undeclared identifier
1>.\Lab1 test.cpp(55) : error C2228: left of '.printDay' must have class/struct/union
1> type is ''unknown-type''
1>Lab1 test - 16 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Please Help and thanks in advance

Member Avatar
Chilton
Junior Poster
107 posts since Oct 2009
Reputation Points: 22 [?]
Q&As Helped to Solve: 16 [?]
Skill Endorsements: 0 [?]
 
0
 

DayOfTheWeek is your class type; you're trying to make it a string variable name as well.

Use a different name for the string.

Also,

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

DayOfTheWeek needs to be changed, and your get function doesn't store the returned value anywhere.

Member Avatar
Akill10
Posting Pro
585 posts since Sep 2010
Reputation Points: 96 [?]
Q&As Helped to Solve: 81 [?]
Skill Endorsements: 0 [?]
 
0
 

You don't seem to grasp the use of getters/setters. They are used to set/get member variables for each instance of a class.

string DayOfTheWeek::getDay() const
{
string simDay;
 
cout << "Enter The Day Of The Week:\n";
cin >> simDay;
 
return simDay;
}

Tell me which member variable you should be "getting" here? Don't forget, you have already used the setDay() function, what did that do? It SET the member variable day to whatever param was passed in. So now you know that day contains something, which is where the getDay() function comes in.


Your setDay is also incorrect, you are USING it correctly though. You need to think of better variable names, do as Chilton explained to correct those errors.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: