1

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;
}
user2989065
  • 13
  • 1
  • 6
  • 2
    The check in `setDay()` does not do what you think it does. – pmr Feb 17 '14 at 21:42
  • What does it do then? It looks pretty straightforward to me, but I'm positive i'm doing something stupid in that function now. – user2989065 Feb 17 '14 at 21:48
  • ((day != "Sunday") || (day != "Monday") ...) – Ross Feb 17 '14 at 21:49
  • Also beware of indexing your array (statements like "return arr_day[num]") without checking whether num will be in the bounds of said array... – Ross Feb 17 '14 at 21:49
  • yeah, it was the last step of the assignment to include a try-catch block to make sure the number isn't negative. Which isn't necessary in this particular case since the number is defined at before compiling as a positive 8. – user2989065 Feb 17 '14 at 21:56
  • i didn't see this at first but it also says that it's a first chance exception, then it throws the above stated unhandled exception. – user2989065 Feb 17 '14 at 21:57

0 Answers0