Tuesday + 3 = Friday? C++ Programming Problem
<p>Looking at the main function, we can see that I've Hard Coded the "Monday" into my setDay public function. It is easy to grab a day of the week from the user using a c-string (as I did in setDay), but how would I ask the user to add n to the day that is set, "Monday" and come up with "Thursday"? It is hard because typdef enum { INVALID, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY} doesn't interpret 9 is 0 and/or 10 as 1.</p>

<pre><code>#include &lt;iostream&gt;

using std::cout;
using std::endl;

class DayOfTheWeek //class is encapsulation of functions and members that manipulate the data.
{
public:

 DayOfTheWeek(); // Constructor
 virtual ~DayOfTheWeek(); // Destructor

 void setDay(const char * day); // Function to set the day
 void printDay() const;      // Function to Print the day.
 const char * getDay() const;   // Function to get the day.

 const char * plusOneDay();  // Next day function
 const char * minusOneDay(); // Previous day function
 const char * addDays(int addValue);  // function that adds days based on parameter value

private:
 char * day; // variable for the days of the week.

};

DayOfTheWeek::DayOfTheWeek() : day(0)
{
// Usually I would allocate pointer member variables
// Here in the construction of the Object
}

const char * DayOfTheWeek::getDay() const
{
 return day; // we can get the day simply by returning it.
}

const char * DayOfTheWeek::minusOneDay()
{
 if ( strcmp( day, "Monday" ) == 0)
 {
  cout &lt;&lt; "The day before " &lt;&lt; day &lt;&lt; " is ";
  return "Sunday";
 }
 else if ( strcmp( day, "Tuesday" ) == 0 )
 {
  cout &lt;&lt; "The day before " &lt;&lt; day &lt;&lt; " is ";
  return "Monday";
 }
 else if ( strcmp( day, "Wednesday" ) == 0 )
 {
  cout &lt;&lt; "The day before " &lt;&lt; day &lt;&lt; " is ";
  return "Tuesday";
 }
 else if ( strcmp( day, "Thursday" ) == 0 )
 {
  cout &lt;&lt; "The day before " &lt;&lt; day &lt;&lt; " is ";
  return "Wednesday";
 }
 else if ( strcmp( day, "Friday" ) == 0 )
 {
  cout &lt;&lt; "The day before " &lt;&lt; day &lt;&lt; " is ";
  return "Thursday";
 }
 else if ( strcmp( day, "Saturday" ) == 0 )
 {
  cout &lt;&lt; "The day before " &lt;&lt; day &lt;&lt; " is ";
  return "Friday";
 }
 else if ( strcmp( day, "Sunday" ) == 0 )
 {
  cout &lt;&lt; "The day before " &lt;&lt; day &lt;&lt; " is ";
  return "Saturday";
 }
 else
 {
  cout &lt;&lt; "'" &lt;&lt; day &lt;&lt; "'";
  return "is an invalid day of the week!";
 } 
}

const char * DayOfTheWeek::plusOneDay()
{
 if ( strcmp( day, "Monday" ) == 0)
 {
  cout &lt;&lt; "The day after " &lt;&lt; day &lt;&lt; " is ";
  return "Tuesday";
 }
 else if ( strcmp( day, "Tuesday" ) == 0 )
 {
  cout &lt;&lt; "The day after " &lt;&lt; day &lt;&lt; " is ";
  return "Wednesday";
 }
 else if ( strcmp( day, "Wednesday" ) == 0 )
 {
  cout &lt;&lt; "The day after " &lt;&lt; day &lt;&lt; " is ";
  return "Thursday";
 }
 else if ( strcmp( day, "Thursday" ) == 0 )
 {
  cout &lt;&lt; "The day after " &lt;&lt; day &lt;&lt; " is ";
  return "Friday";
 }
 else if ( strcmp( day, "Friday" ) == 0 )
 {
  cout &lt;&lt; "The day after " &lt;&lt; day &lt;&lt; " is ";
  return "Saturday";
 }
 else if ( strcmp( day, "Saturday" ) == 0 )
 {
  cout &lt;&lt; "The day after " &lt;&lt; day &lt;&lt; " is ";
  return "Sunday";
 }
 else if ( strcmp( day, "Sunday" ) == 0 )
 {
  cout &lt;&lt; "The day after " &lt;&lt; day &lt;&lt; " is ";
  return "Monday";
 }
 else
 {
  cout &lt;&lt; "'" &lt;&lt; day &lt;&lt; "'";
  return " is an invalid day of the week!";
 }
}

const char * DayOfTheWeek::addDays(int addValue)
{
 if ( addValue &lt; 0 )
 {
  if ( strcmp( day, "Monday" ) == 0)
  {
   cout &lt;&lt; day &lt;&lt; " - " &lt;&lt; -addValue &lt;&lt; " = ";
   return "Friday";
  }
  else if ( strcmp( day, "Tuesday" ) == 0 )
  { 
   cout &lt;&lt; day &lt;&lt; " - " &lt;&lt; -addValue &lt;&lt; " = ";
   return "Saturday";
  }
  else if ( strcmp( day, "Wednesday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " - " &lt;&lt; -addValue &lt;&lt; " = ";
   return "Sunday";
  }
  else if ( strcmp( day, "Thursday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " - " &lt;&lt; -addValue &lt;&lt; " = ";
   return "Monday";
  }
  else if ( strcmp( day, "Friday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " - " &lt;&lt; -addValue &lt;&lt; " = ";
    return "Tuesday";
  }
  else if ( strcmp( day, "Saturday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " - " &lt;&lt; -addValue &lt;&lt; " = ";
   return "Wednesday";
  }
  else if ( strcmp( day, "Sunday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " - " &lt;&lt; -addValue &lt;&lt; " = ";
   return "Thursday";
  }
   else
  {
   cout &lt;&lt; "'" &lt;&lt; day &lt;&lt; "' ";
   return "is an invalid day of the week! ";
  }
 }
 else // if our parameter is greater than 0 (positive)
 {
  if ( strcmp( day, "Monday" ) == 0)
  {
   cout &lt;&lt; day &lt;&lt; " + " &lt;&lt; addValue &lt;&lt; " = ";
   return "Thursday";
  }
  else if ( strcmp( day, "Tuesday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " + " &lt;&lt; addValue &lt;&lt; " = ";
   return "Friday";
  }
  else if ( strcmp( day, "Wednesday" ) == 0 )
  { 
   cout &lt;&lt; day &lt;&lt; " + " &lt;&lt; addValue &lt;&lt; " = ";
   return "Saturday";
  }
   else if ( strcmp( day, "Thursday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " + " &lt;&lt; addValue &lt;&lt; " = ";
   return "Sunday";
  }
  else if ( strcmp( day, "Friday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " + " &lt;&lt; addValue &lt;&lt; " = ";
    return "Monday";
  }
  else if ( strcmp( day, "Saturday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " + " &lt;&lt; addValue &lt;&lt; " = ";
   return "Tuesday";
  }
  else if ( strcmp( day, "Sunday" ) == 0 )
  {
   cout &lt;&lt; day &lt;&lt; " + " &lt;&lt; addValue &lt;&lt; " = ";
   return "Wednesday";
  }
  else
  {
   cout &lt;&lt; "'" &lt;&lt; day &lt;&lt; "' ";
   return "is an invalid day of the week! ";
  }
 }
}

void DayOfTheWeek::printDay() const
{
 cout &lt;&lt; "The Value of the " &lt;&lt; day;
}

void DayOfTheWeek::setDay(const char * day)
{
 if (day)
 {// Here I am allocating the object member char day pointer
  this-&gt;day = new char[strlen(day)+1];
  size_t length = strlen(day)+1; // +1 for trailing null char
  strcpy_s(this-&gt;day , length , day); // copying c-strings
 }
 else day = NULL; // If their was a problem with the parameter 'day'
}

DayOfTheWeek::~DayOfTheWeek()  
{
 delete day; // Free the memory allocated in SetDay
}

int main()
{ 
 DayOfTheWeek MondayObject; // declare an object

 MondayObject.setDay("Monday"); // Call our public function 'setDay' to set a day of the week
 MondayObject.printDay();        // Call our public function 'printDay' to print the day we set

 cout &lt;&lt; " object is " &lt;&lt; MondayObject.getDay() &lt;&lt; endl; // Print the value of the object

 cout &lt;&lt; MondayObject.plusOneDay()  &lt;&lt; endl;
 cout &lt;&lt; MondayObject.minusOneDay() &lt;&lt; endl;
 cout &lt;&lt; MondayObject.addDays(3)    &lt;&lt; endl;

   MondayObject.printDay();
 cout &lt;&lt; " object is still " &lt;&lt; MondayObject.getDay() &lt;&lt; endl; // Print the value of the object

 cout &lt;&lt; MondayObject.addDays(-3)  &lt;&lt; endl;

 return 0;
}
</code></pre>
