We help IT Professionals succeed at work.
Get Started

Help with the output on programm

Last Modified: 2012-05-11
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
  WeekDay(string day);
  WeekDay(int dayNum);

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

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

  void print ();

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

  // 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: ";

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

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

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


  return 0;


WeekDay::WeekDay(string day)

WeekDay::WeekDay(int 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);

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];
	// 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

Watch Question
Top Expert 2008
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE