4 Replies - 9207 Views - Last Post: 11 March 2009 - 02:51 PM Rate Topic: -----

#1 BoyWonder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-February 09

days of the week

Post icon  Posted 09 March 2009 - 03:04 PM

Here is what my teacher is asking:
Write a program that contains a class that implements the days of the week.
Set the day
Print the day
Return the day
Return the next day
Return the previous day
Calculate and return the day by adding a certain amount of days to the current day. For example if you add 5 days to Saturday, the day to be returned is Thursday. Likewise, if we add 12 days to Wednesday, the the day returned will be Monday.


Ok, I wrote a code prior to this, here it is:
One question?
1.How do I implement mon=Monday, etc on the output of this code?

Also, I will try what my teacher is asking after I have corrected this code output.
Can I get any suggestions that can help me in the right directions.
Should I use a pointer to DayOfTheWeek::myDay?
And If there are any helpful hints, I would be much abligied.

#include <iostream>
#include <string>

using namespace std;

class DayOfTheWeek
{
public:
	private:
		string day;	//Day of the week
		

	public:
		string  Monday;
		string	Tuesday;		
		string	Wednesday;
		string	Thursday;
		string	Friday;
		string	Saturday;
		string	Sunday;
		string  pointer;			
					
					
		DayOfTheWeek(void);		
		  ~DayOfTheWeek(void);	   
		string getDay();		  
		void setDay(string);	
		void printDay();		
		void myDay(void);
};
int main()
{
	
	string DayOfWeek = "";		 
	DayOfTheWeek myDay;
	DayOfTheWeek DayOne;			 
	DayOfTheWeek DayTwo;			 
	
	DayOfWeek = DayOne.getDay();   
	DayOne.setDay(DayOfWeek);	  

	DayOfWeek = DayTwo.getDay();  
	DayTwo.setDay(DayOfWeek);	  

	DayOne.printDay();			

	DayTwo.printDay();			
	
	return 0;
}
void DayOfTheWeek::myDay()
{
const string mon = Monday;
const string tues = Tuesday;
const string weds = Wednesday;
const string thurs = Thursday;
const string fri = Friday; 
const string  sat = Saturday;
const string  sun = Sunday; 
}



DayOfTheWeek::DayOfTheWeek(void)
{
	day  = "default";
	
}

DayOfTheWeek::~DayOfTheWeek(void)
{
}
string DayOfTheWeek::getDay()
{
	string tempDay;

	cout << "Enter Day: ";
	cin  >> tempDay;

	return tempDay;
}

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

void DayOfTheWeek::printDay()
{
	
	
	cout << "The Day Is : " << day << endl;


Mod edit - Please :code:

This post has been edited by BetaWar: 09 March 2009 - 03:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: days of the week

#2 floatingpoint  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 05-March 09

Re: days of the week

Posted 09 March 2009 - 06:02 PM

I always solved this prob in school using a simple string array :
daysofweek[6]={"Sunday","Monday",...."Saturday"}

This should take care of most of the basic probs.. also in order to find the day which is n days from the present, just do a MOD 7 of n and add it to the current days index in the daysofweek array. In course of such manipulation take care that indexes start at 0. Also while adding, make sure u loop round, that is

//withing the for loop to add n mod 7 times
//check after each addition
if (index>6) then
index=0; // note your index will be never greater than 7 after an addition because n mod 7 generates a result between 0-6, including them
Was This Post Helpful? 0
  • +
  • -

#3 BoyWonder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-February 09

Re: days of the week

Posted 11 March 2009 - 12:01 PM

View Postfloatingpoint, on 9 Mar, 2009 - 05:02 PM, said:

I always solved this prob in school using a simple string array :
daysofweek[6]={"Sunday","Monday",...."Saturday"}

This should take care of most of the basic probs.. also in order to find the day which is n days from the present, just do a MOD 7 of n and add it to the current days index in the daysofweek array. In course of such manipulation take care that indexes start at 0. Also while adding, make sure u loop round, that is

//withing the for loop to add n mod 7 times
//check after each addition
if (index>6) then
index=0; // note your index will be never greater than 7 after an addition because n mod 7 generates a result between 0-6, including them

Was This Post Helpful? 0
  • +
  • -

#4 BoyWonder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 16-February 09

Re: days of the week

Posted 11 March 2009 - 12:15 PM

Here is what I got after adding the array.
I get three errors in the same spot:
1>f:\oop2\oop2\week2.cpp(82) : error C2059: syntax error : '{'
1>f:\oop2\oop2\week2.cpp(82) : error C2143: syntax error : missing ';' before '{'
1>f:\oop2\oop2\week2.cpp(82) : error C2143: syntax error : missing ';' before '}'

I'm trying to:
Set the day
Print the day to the computer monitor (not the printer)
Return the day, sun =Sunday, mon = Monday, etc..
I put comments by the errors


#include <iostream>
#include <string>

using namespace std;

class DayOfTheWeek
{
public:
private:
string day; //Day of the week
char daysofweek[6];

public:
string Monday;
string Tuesday;
string Wednesday;
string Thursday;
string Friday;
string Saturday;
string Sunday;



DayOfTheWeek(void);
~DayOfTheWeek(void);
string getDay();
void setDay(string);
void printDay();
void myDay(void);
};
int main()
{

string DayOfWeek = "";
DayOfTheWeek myDay;
DayOfTheWeek DayOne;
DayOfTheWeek DayTwo;

DayOfWeek = DayOne.getDay();
DayOne.setDay(DayOfWeek);

DayOfWeek = DayTwo.getDay();
DayTwo.setDay(DayOfWeek);

DayOne.printDay();

DayTwo.printDay();

return 0;
}

DayOfTheWeek::DayOfTheWeek(void)
{
day = "default";

}

DayOfTheWeek::~DayOfTheWeek(void)
{
}
string DayOfTheWeek::getDay()
{
string mon = Monday;
string tues = Tuesday;
string weds = Wednesday;
string thurs = Thursday;
string fri = Friday;
string sat = Saturday;
string sun = Sunday;
string tempDay;

cout << "Enter Day: ";
cin >> tempDay;

return tempDay;
}

void DayOfTheWeek::setDay(string daysofweek)
{
day = daysofweek[6];
daysofweek[6]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; // Here is the error!

}

void DayOfTheWeek::printDay()
{


cout << "The Day Is : " << day << endl;

}

View Postfloatingpoint, on 9 Mar, 2009 - 05:02 PM, said:

I always solved this prob in school using a simple string array :
daysofweek[6]={"Sunday","Monday",...."Saturday"}

This should take care of most of the basic probs.. also in order to find the day which is n days from the present, just do a MOD 7 of n and add it to the current days index in the daysofweek array. In course of such manipulation take care that indexes start at 0. Also while adding, make sure u loop round, that is

//withing the for loop to add n mod 7 times
//check after each addition
if (index>6) then
index=0; // note your index will be never greater than 7 after an addition because n mod 7 generates a result between 0-6, including them

Was This Post Helpful? 0
  • +
  • -

#5 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: days of the week

Posted 11 March 2009 - 02:51 PM

This may get you started ...

#include <iostream>
using namespace std;


const char* dayNames[] =
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

class Day
{
	public:
		Day() { dayNumber = 0; }
		const char* getDay() { return dayNames[dayNumber]; }
		const char* getNextDay() { return dayNames[(dayNumber+1) % 7]; }
		const char* getPrevDay() { return dayNames[(dayNumber+7-1) % 7]; }

		const char* getDayPlus( int n ) { return dayNames[(dayNumber+n) % 7]; }
		
		void setDay( int n ) { dayNumber = n; normalize(); }
		void printDay() { cout << dayNames[dayNumber]; }
	private:
		int dayNumber;
		void normalize() { dayNumber = dayNumber % 7; }
};


int main()
{
	Day d;
	cout << "The (default) day is " << d.getDay() <<endl
		 << "The next day is " << d.getNextDay() <<endl
		 << "The day plus 5 is " << d.getDayPlus(5) <<endl
		 << "The prev day is " << d.getPrevDay() <<endl;
		 
	d.setDay(6);
	cout << "After 'd.setDay(6)' ... the day now is " << d.getDay() <<endl
		 << "The next day is " << d.getNextDay() <<endl
		 << "The day plus 5 is " << d.getDayPlus(5) <<endl
		 << "The prev day is " << d.getPrevDay() <<endl;

	d.setDay(3);
	cout << "After 'd.setDay(3)' ... the day now is " << d.getDay() <<endl
		 << "The next day is " << d.getNextDay() <<endl
		 << "The day plus 12 is " << d.getDayPlus(12) <<endl
		 << "The prev day is " << d.getPrevDay() <<endl;

	cout << "The day still is ... ";
	d.printDay();

	cout << "\nPress 'Enter' to continue ... " << flush;
	cin.get();
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1