Regardless of what I do, the program won't run without crashing... any luck on how to fix it?
#include <iostream>
#include <string>
using namespace std;
class DayOfTheWeek
{
public:
void setDay(string);
void printDay();
string getDay(string&);
string plusOneDay(int);
string minusOneDay(int);
string addDays(int);
private:
int day;
int toNumber(string);
string toName(int);
};
int DayOfTheWeek::toNumber(string name)
{
if (name == "Monday") return 0;
if (name == "Tuesday") return 1;
if (name == "Wednesday") return 2;
if (name == "Thursday") return 3;
if (name == "Friday") return 4;
if (name == "Saturday") return 5;
if (name == "Sunday") return 6;
else return -1;
}
string DayOfTheWeek::toName(int idx)
{
idx = idx % 7;
switch (idx)
{
case 0: return "Monday"; break;
case 1: return "Tuesday"; break;
case 2: return "Wednesday"; break;
case 3: return "Thursday"; break;
case 4: return "Friday"; break;
case 5: return "Saturday"; break;
case 6: return "Sunday"; break;
}
}
void DayOfTheWeek::setDay(string currentDay)
{
day = toNumber(currentDay);
}
void DayOfTheWeek::printDay()
{
cout << day;
}
string DayOfTheWeek::getDay(string ¤t)
{
current = toName(day);
return current;
}
string DayOfTheWeek::plusOneDay(int newDay)
{
int nextDay = day + 1;
string theNextDay = toName(nextDay);
return theNextDay;
}
string DayOfTheWeek::minusOneDay(int newDay)
{
int previousDay = day - 1;
string thePreviousDay = toName(previousDay);
return thePreviousDay;
}
string DayOfTheWeek::addDays(int number)
{
int idx = day + number;
if (idx < 0)
{
idx = idx + 7;
}
string recentDay = toName(idx);
return recentDay;
}
//main function which prints the requirements
int main()
{
string current;
int newDay = 0;
string theNextDay;
string theDayBefore;
string RecentDay;
int number = 3;
DayOfTheWeek monday;
monday.setDay("Monday");
string currentDay = monday.getDay(current);
monday.plusOneDay(newDay);
monday.minusOneDay(newDay);
monday.addDays(number);
cout <<"The value of the monday object is ";
monday.printDay();
cout << endl;
cout <<"The day after" << currentDay << " is " << monday.plusOneDay(newDay) << "." << endl;
cout <<"The day before" << currentDay << " is " << monday.minusOneDay(newDay) << "." << endl;
return 0;
}