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 &current)
{
	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;
}

Recommended Answers

All 13 Replies

You'll have better luck with some explanation, not just posting code and asking us to figure out what it does. Pinpoint as close as you can where the problem seems to be, and what the crash circumstances are.

Basically, what you have here is the same as taking your car to the mechanic, saying "Fix", and walking away.

In minus one day you can get negative day, and in function toname you only treat properly positive idx. If you give to this function negative number this value is not handled by switch, and you return nothing, and this nothing is treated as string, and after statement in line 112 it is being destructed. But you can't free unallocated memory, so the program craches. Also if you want to know where and probably why your program crashed use debugger, for example gdb (for gcc).

In minus one day you can get negative day, and in function toname you only treat properly positive idx. If you give to this function negative number this value is not handled by switch, and you return nothing, and this nothing is treated as string, and after statement in line 112 it is being destructed. But you can't free unallocated memory, so the program craches. Also if you want to know where and probably why your program crashed use debugger, for example gdb (for gcc).

When run, the utility thing pops up and says

#if _ITERATOR_DEBUG_LEVEL == 2
	if (_Myproxy != 0)
		{	// proxy allocated, drain it
		_Lockit _Lock(_LOCK_DEBUG);

		for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
			[B]*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)[/B]
			(*_Pnext)->_Myproxy = 0;
		_Myproxy->_Myfirstiter = 0;
		}
 #endif /* _ITERATOR_DEBUG_LEVEL == 2 */

where bolded is where the breakpoint is, i have no idea what im looking at though

Also this is my first time in months of coding where this issue was ever presented to me, a "xutility," thing pops up and says something which can be found above this post.

In minus one day you can get negative day, and in function toname you only treat properly positive idx.

He's exactly right. What happens when idx is negative, your method toName runs right off of the end. At the end of toName, put something like return "junk"; . You'll hit there when you try to find the day before Monday. You'll have to catch that possibility.

He's exactly right. What happens when idx is negative, your method toName runs right off of the end. At the end of toName, put something like return "junk"; . You'll hit there when you try to find the day before Monday. You'll have to catch that possibility.

well that was actually the problem, but now when i run it I do not get the proper results. This is the problem, but it won't display the proper results under the previous day print out.

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

So check if your number is negative, and if it is, add 7 to it.

So check if your number is negative, and if it is, add 7 to it.

already did that above though...

Yes, I see that you did it in the other function. Post up your current code that you are working with.

This is still it...

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

class DayOfTheWeek
{
public:
	DayOfTheWeek();
	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);
};

DayOfTheWeek::DayOfTheWeek()
{
	day = 0;
}

int DayOfTheWeek::toNumber(string dayName)
{
	if (dayName == "Monday") return 0;
	if (dayName == "Tuesday") return 1;
	if (dayName == "Wednesday") return 2;
	if (dayName == "Thursday") return 3;
	if (dayName == "Friday") return 4;
	if (dayName == "Saturday") return 5;
	if (dayName == "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 << toName(day);
}

string DayOfTheWeek::getDay(string &current)
{
	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;
	int number = 3;

	//deletable
	string theNextDay;
	string theDayBefore;
	string RecentDay;

	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;
	cout << currentDay << " + " << number << " = " << monday.addDays(number) << endl;
	cout <<"The value of the monday object is still " << currentDay << "." << endl;
	cout << currentDay << " - " << number << " = " << monday.addDays(number * -1) << endl;
	getch();
	return 0;
}

You don't check if the day is negative in minusOneDay at all. You do it in addDays, but that's it, and those two methods don't interact. So when day = 0 (Monday), in minusOneDay you subtract 1 (= -1) and pass it into toName which can't handle the -1. So, do some range checking on idx in toName.

fixed it thanks!

No prob, lol. I had to look through it once more to trace it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.