troubleshooting Question

Help with the output on programm

Avatar of IbanezRG
IbanezRG asked on
Programming Languages-OtherC++
1 Comment1 Solution375 ViewsLast Modified:
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

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros