4 Replies - 1885 Views - Last Post: 17 January 2009 - 02:02 AM Rate Topic: -----

#1 gamejust4u  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 214
  • Joined: 19-November 08

Count the days of the week

Posted 16 January 2009 - 03:26 PM

My program runs fine. The only thing is that this code

cout<<"Five Days from today will be: ";
		dayOne.addDays("Wedsnday", 5);   


doesn't show. I don't know what went wrong. Can anyone figure out where I went wrong?

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace System;
using namespace std;

class DaysOfTheWeek
{
	private:
		string day;
		string mon;
		string tue;
		string wed;
		string thur;
		string fri;
		string sat;
		string sun;
		
				
	public:
		//constructors
		 DaysOfTheWeek();
		 DaysOfTheWeek(string);

			//Functions
			void setDay (string);
			void printDay (void);
			string getDay (void);
			void nextDay (string);
			void previousDay (string);
			void plusOneDay (int);
			void minusOneDay (int);
			void addDays (string, int);

};

int main()
{
	cout<<"Days of the week"<<endl;

	//Displays for Day 1
		cout<<"** Day 1 **"<<endl;
	DaysOfTheWeek dayOne;
	dayOne.setDay("Wedsnday");
	dayOne.getDay();

	cout<<"Today is :"; 	
	dayOne.printDay();

	cout<<"Yesterday was: ";
		dayOne.previousDay("Wedsnday");

	cout<<"Tomorrow will be: ";
		dayOne.nextDay("Wedsnday");

	cout<<"Five Days from today will be: ";
		dayOne.addDays("Wedsnday", 5);
		
		cout<<endl;
	//Displays for Day 2 
		cout<<"** Day 2 **"<<endl;
	DaysOfTheWeek dayTwo("Sunday");
	dayTwo.setDay("Sunday");
	dayTwo.getDay();

	cout<<"Today is :";
	dayTwo.printDay();

	cout<<"Yesterday was: ";
		dayTwo.previousDay("Sunday");
	
	cout<<"Tomorrow will be: ";
		dayTwo.nextDay("Sunday");

	cout<<"Two Days from today will be: ";
		dayTwo.addDays("Sunday", 2);
	
		cout<<endl;
	
	return 0;
}

DaysOfTheWeek::DaysOfTheWeek()
{
	day = " ";
}

DaysOfTheWeek::DaysOfTheWeek(string myDays)
{
	mon = "Monday";
	tue = "Tuesday";
	wed = "Wedsnday";
	thur = "Thursday";
	fri = "Friday";
	sat = "Saturday";
	sun = "Sunday";
	
}

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

string DaysOfTheWeek::getDay(void)
{
	return day;
}

void DaysOfTheWeek::printDay(void)
{
	cout<< day <<endl;
}

void DaysOfTheWeek::nextDay(string dayN)
{

	string nextDay = " ";
	//Enter loop to determine the following day

	if (dayN == "Monday")
		{cout<< "Tuesday" <<endl;}
		 if (dayN == "Tuesday")
			{cout<< "Wedsnday" <<endl;}
			 if (dayN == "Wedsnday")
				{cout<< "Thursday" <<endl;}
				 if (dayN == "Thursday")
					{cout<< "Friday" <<endl;}
					 if (dayN == "Friday")
						{cout<< "Saturday" <<endl;}
						 if (dayN == "Saturday")
							{cout<< "Sunday" <<endl;}
							 if (dayN == "Sunday")
								{cout<< "Monday" <<endl;}

	
}

void DaysOfTheWeek::previousDay(string dayP)
{
	string prevDay = " ";

	//entering loop for the past day
	if (dayP == "Monday")
		{cout<< "Sunday" <<endl;}
		 if (dayP == "Tuesday")
			{cout<< "Monday" <<endl;}
			 if (dayP == "Wedsnday")
				{cout<< "Tuesday" <<endl;}
				 if (dayP == "Thursday")
					{cout<< "Wedsnday" <<endl;}
					 if (dayP == "Friday")
						{cout<< "Thursday" <<endl;}
						 if (dayP == "Saturday")
							{cout<< "Friday" <<endl;}
							 if (dayP == "Sunday")
								{cout<< "Saturday" <<endl;}

}

void DaysOfTheWeek::plusOneDay(int add)
{
	//declared variables
	int added= 0;

	//calculations
	for (int a = 0; a < 7; a++){
		if (added < add)
		{added = added + 1;}
	
	//Displays
	cout<<added<<endl;
	}
}

void DaysOfTheWeek::minusOneDay(int sub)
{
	//declared variables
	int subt = 0;

	//calculations
	for (int b = 0; b > 7; b = b - 1){
		if (subt > sub)
		{subt = subt - 1;}
	
	//Displays
	cout<<subt<<endl;
	}
}

void DaysOfTheWeek::addDays(string current, int count)
{
	//Comparisons
	day = count;
	
	//entering loop
	

	if (current == "Monday"){
		if (count == 1)
		{cout<<tue<<endl;}
			if (count == 2)
			{cout<<wed<<endl;}
				if (count == 3)
				{cout<<thur<<endl;}
					if (count == 4)
					{cout<<fri <<endl;}
						if (count == 5)
						{cout<<sat <<endl;}
							if (count == 6)
							{cout<<sun<<endl;}
								if (count == 7)
								{cout<<mon <<endl;} }

		

	if (current == "Tuesday"){
		 if (count == 1)
		{cout<<wed<<endl;}
			if (count == 2)
			{cout<<thur<<endl;}
				if (count == 3)
				{cout<<fri<<endl;}
					if (count == 4)
					{cout<<sat <<endl;}
						if (count == 5)
						{cout<<sun <<endl;}
							if (count == 6)
							{cout<<mon<<endl;}
								if (count == 7)
								{cout<<tue <<endl;} }

	if (current == "Wedsnday"){
		if (count == 1)
		{cout<<thur<<endl;}
			if (count == 2)
			{cout<<fri<<endl;}
				if (count == 3)
				{cout<<sat<<endl;}
					if (count == 4)
					{cout<<sun <<endl;}
						if (count == 5)
						{cout<<mon<<endl;}
							if (count == 6)
							{cout<<tue<<endl;}
								if (count == 7)
								{cout<<wed <<endl;} }

	if (current == "Thursday"){
		if (count == 1)
		{cout<<fri<<endl;}
			if (count == 2)
			{cout<<sat<<endl;}
				if (count == 3)
				{cout<<sun<<endl;}
					if (count == 4)
					{cout<<mon <<endl;}
						if (count == 5)
						{cout<<tue <<endl;}
							if (count == 6)
							{cout<<wed<<endl;}
								if (count == 7)
								{cout<<thur <<endl;} }

	if (current == "Friday"){
		 if (count == 1)
		{cout<<sat<<endl;}
			if (count == 2)
			{cout<<sun<<endl;}
				if (count == 3)
				{cout<<mon<<endl;}
					if (count == 4)
					{cout<<tue <<endl;}
						if (count == 5)
						{cout<<wed <<endl;}
							if (count == 6)
							{cout<<thur<<endl;}
								if (count == 7)
								{cout<<fri <<endl;} }

	if (current == "Saturday"){
		if (count == 1)
		{cout<<sun<<endl;}
			if (count == 2)
			{cout<<mon<<endl;}
				if (count == 3)
				{cout<<tue<<endl;}
					if (count == 4)
					{cout<<wed <<endl;}
						if (count == 5)
						{cout<<thur <<endl;}
							if (count == 6)
							{cout<<fri<<endl;}
								if (count == 7)
								{cout<<sat <<endl;} }

	if (current == "Sunday"){
		if (count == 1)
		{cout<<mon<<endl;}
			if (count == 2)
			{cout<<tue<<endl;}
				if (count == 3)
				{cout<<wed<<endl;}
					if (count == 4)
					{cout<<thur <<endl;}
						if (count == 5)
						{cout<<fri <<endl;}
							if (count == 6)
							{cout<<sat<<endl;}
								if (count == 7)
								{cout<<sun <<endl;} }

}




Is This A Good Question/Topic? 0
  • +

Replies To: Count the days of the week

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,217
  • Joined: 23-August 08

Re: Count the days of the week

Posted 16 January 2009 - 03:42 PM

Change all these:
cout<<"Five Days from today will be: ";
    dayOne.addDays("Wedsnday", 5);


to
cout<<"Five Days from today will be: " 
        << dayOne.addDays("Wedsnday", 5);


And it's "Wednesday", not "Wedsnday".
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Count the days of the week

Posted 16 January 2009 - 04:11 PM

Your code for "addDays" made my eyes bleed.

Consider two things that would make this far simpler, and smaller. One is a seven element string array for the day names. The other is to use a number (0..6) to internally represent your values.
Was This Post Helpful? 1
  • +
  • -

#4 gamejust4u  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 214
  • Joined: 19-November 08

Re: Count the days of the week

Posted 16 January 2009 - 07:54 PM

View PostJackOfAllTrades, on 16 Jan, 2009 - 02:42 PM, said:

Change all these:
cout<<"Five Days from today will be: ";
    dayOne.addDays("Wedsnday", 5);


to
cout<<"Five Days from today will be: " 
        << dayOne.addDays("Wedsnday", 5);


And it's "Wednesday", not "Wedsnday".




That still didn't work. I'm not understanding why dayTwo.addDays works and dayOne.addDays doesn't. Any other suggestions?
Was This Post Helpful? 0
  • +
  • -

#5 TheMagnitude  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 144
  • Joined: 12-January 08

Re: Count the days of the week

Posted 17 January 2009 - 02:02 AM

View Postbaavgai, on 16 Jan, 2009 - 03:11 PM, said:

Your code for "addDays" made my eyes bleed.

Consider two things that would make this far simpler, and smaller. One is a seven element string array for the day names. The other is to use a number (0..6) to internally represent your values.

My eyes bled too.
The best thing you should do is exactly what baavgai suggested, if will make your code much smaller, and much more flexible.

Code such as this:

#include <iostream>
#include <string>

using namespace std;

string days[7] = 
{
	"Monday",
	"Tuesday",
	"Wednesday",
	"Thursday",
	"Friday",
	"Saturday",
	"Sunday"
};

string nextDay(string day, int num)
{
	// SEARCH THE STRING
	for (int i = 0; i < 7; i ++)
	{
		if (days[i] == day)
			// RETURN THE DAY
			return days[(364 + i + num) % 7];
	}
}
	
int main()
{
	cout << nextDay("Wednesday", 5) << endl;
	cout << nextDay("Wednesday", -5);
	cin.ignore();
	return 0;
}


Now isn't that much more eyepleasing? And it also works with negative values of num

This post has been edited by TheMagnitude: 17 January 2009 - 02:04 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1