I wrote this simple program according to the UML guidelines I was given but I'm having a tough time figuring out why it's throwing an exception when I run it. The code itself compiles when I run it and if I hit continue enough times, it does successfully run the code to completion, but I'm guessing it shouldn't do what it does. The program shows
"Unhandled exception at at [memory address] in [Project.exe]: Microsoft C++ exception: char at memory location [different memory address]"
It uses that exception twice if I hit continue, but unless the tester file is specifically causing it to appear, I'm at a loss as to how to fix it. (I really don't know what exception e is either);
#include <string>
#include <iostream>
using namespace std;
class dayType
{
private:
string day;
public:
dayType();
string addDays(int _num);
dayType(string _day);
string getDay();
void setDay(string _day);
string nextDay();
string prevDay();
void print();
};
Functions.cpp
#include <string>
#include <iostream>
#include <exception>
#include "dayType.h"
using namespace std;
string arr_day[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
dayType::dayType()
{
day = "--";
}
string dayType::addDays(int _num)
{
int num = distance(arr_day, find(arr_day, arr_day + 7, day));
num = num + _num;
try
{
while (num > 7)
num -= 7;
return arr_day[num];
}
catch(...)
{
cout << "Negative input value. Compiler did not follow simple instructions...";
}
return "Poopsy";
}
dayType::dayType(string _day)
{
day = "--";
setDay(_day);
}
string dayType::getDay()
{
const string day1 = dayType::day;
return day1;
}
void dayType::setDay(string _day)
{
dayType::day = _day;
if ((day != "Sunday") || (day != "Monday") || (day != "Tuesday") || (day != "Wednesday") || (day != "Thursday") || (day != "Friday") || (day != "Saturday"))
throw "Not a valid day of the week. Seriously... There's only 7";
}
string dayType::nextDay()
{
int num = distance(arr_day, find(arr_day, arr_day + 7, day));
num++;
return arr_day[num];
}
string dayType::prevDay()
{
int num = distance(arr_day, find(arr_day, arr_day + 7, day));
num--;
return arr_day[num];
}
void dayType::print()
{
cout << day << endl;
}
Tester File provided:
//*******************************************************
//
// Chapter 1: Programming Exercise 3
//*******************************************************
#include <iostream>
#include <string>
#include "dayType.h";
using namespace std;
int main()
{
dayType myDay("Monday");
cout << "myDay Details: " ;
myDay.print();
cout << endl;
cout << "myDay=" << myDay.getDay()<< ", prevDay=" << myDay.prevDay() << endl;
cout << "myDay=" << myDay.getDay()<< ", nextDay=" <<myDay.nextDay() << endl;
cout << "myDay after adding 8 : " << myDay.addDays(8) << endl;
try
{
myDay.setDay("ZZZZ");
}
catch(exception e)
{
cout << e.what() << endl;
}
system("pause");
return 0;
}