Help with the output on programm

IbanezRG
IbanezRG used Ask the Experts™
on
Finally I got some code that works, but it isn't returning the day before.
It is supposed to output:
The value of monday object is monday.
tomorrow is tuesday.
The day before monday is Sunday.
Three days from Monday is thursday
The value of the monday object is still monday
three days before monday is friday.

The program isn't subtractiong the day.

#include <iostream>
#include <string>


using namespace std;

class WeekDay
{
public:
  //constructors
  WeekDay();
  WeekDay(string day);
  WeekDay(int dayNum);

  //Functions
  void setDay (string);
  void setDay(int dayNum);
  string getDay();

  WeekDay getNextDay ();
  WeekDay getPreviousDay ();
  WeekDay addDays (int dayNum);

  void print ();

private:
  // private functions
  void init();
  int dayToNum(const string dayName);
  string numToDay(const int day);

private:
  // data
  int day_;

  int dayMin;
  int dayMax;
  int numberOfDays;
  string weekDayNames[7];
};

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

  //Displays for Day 1
  WeekDay dayOne("Monday");

  cout<<"The value of monday is: ";
  dayOne.print();

  cout<<"Yesterday was: ";
  WeekDay yesterday = dayOne.getPreviousDay();
  yesterday.print();

  cout<<"Tomorrow will be: ";
  WeekDay tomorrow = dayOne.getNextDay();
  tomorrow.print();

  cout<<"Three Days from today will be: ";
  WeekDay threeFurther = dayOne.addDays(3);
  threeFurther.print();


  cout<<endl;
  

  return 0;
}


WeekDay::WeekDay()
{
  init();
  setDay(0);
}

WeekDay::WeekDay(string day)
{
  init();
  setDay(day);
}

WeekDay::WeekDay(int dayNum)
{
  init();
  setDay(dayNum);
}


void WeekDay::init()
{
  // initialize the number of days and the day names
  dayMin = 0;
  dayMax = 6;
  numberOfDays = 7;
  weekDayNames[0] = "Monday";
  weekDayNames[1] = "Tuesday";
  weekDayNames[2] = "Wednesday";
  weekDayNames[3] = "Thursday";
  weekDayNames[4] = "Friday";
  weekDayNames[5] = "Saturday";
  weekDayNames[6] = "Sunday";
}

void WeekDay::setDay(string day)
{
  int dayNum = dayToNum(day);
  setDay(dayNum);
}

void WeekDay::setDay(int dayNum)
{
  // use modulus (%) to reduce the day number to the range of
  // dayMin to dayMax
  day_ = dayNum % numberOfDays;
}


string WeekDay::getDay()
{
  string dayName = numToDay(day_);
  return dayName;
}

void WeekDay::print()
{
  cout << getDay() << endl;
}


int WeekDay::dayToNum(const string dayName)
{
  // convert a day name to a day number
  // note: dayName is case sensitive

  // find the dayName in the array
  for (int i = dayMin; dayMax; i++)
  {
	if (dayName == weekDayNames[i])
	{
	  return i;
	}
  }

  // if not found, return -1
  return -1;
}

string WeekDay::numToDay(const int day)
{
  // convert a day number to a day name
  if (day >= dayMin && day <= dayMax)
  {
	return weekDayNames[day];
  }
  else
  {
	// if out of range, return empty string
	return "";
  }
}


WeekDay WeekDay::getNextDay()
{
  return WeekDay(day_ + 1);
}

WeekDay WeekDay::getPreviousDay()
{
  return WeekDay(day_ - 1);
}


WeekDay WeekDay::addDays(int count)
{
  return WeekDay(day_ + count);
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2008
Commented:
Hey there,

I think you need to modify at least the following two functions to make them properly wrap around in the array of your defined weekdays:
WeekDay WeekDay::getNextDay()
{
  return WeekDay((day_ < 6) ? day_ + 1 : 0);
}

WeekDay WeekDay::getPreviousDay()
{
  return WeekDay((day_ > 0) ? day_ - 1 : 6);
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial