需要帮助修复C ++中的Unhandled Exception

时间:2014-02-17 21:37:23

标签: c++ exception unhandled

我根据我给出的UML指南编写了这个简单的程序但是我很难弄清楚为什么它在运行时抛出异常。代码本身在我运行它时会编译,如果我持续足够的次数,它会成功运行代码完成,但我猜它不应该做它做的事情。该节目显示

“[Project.exe]中[内存地址]处的未处理异常:Microsoft C ++异常:内存位置的char [不同的内存地址]”

如果我点击继续,它会使用该异常两次,但除非测试人员文件特意导致它出现,否则我不知道如何修复它。 (我真的不知道e是什么例外);

#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;
}
提供了测试人员文件:

//*******************************************************
//
// 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;
}

0 个答案:

没有答案