Hi guys,
I'm working on an assignment for my programming class and was wondering if I could get some help. The problem I'm having is that when I try to access the value of the map, I get a weird debug error. The program compiles fine but when I run it I get this:

"This application has requested the Runtime to terminate it in an unusual way."

I've been searching around trying to find an answer to it but am not having much luck. By doing some fiddling on my own, I've discovered that I can access the key just fine but when the program tries to grab the value, this error occurs.

The issue is occurring in the checkCurrentLoans function around line 120.

Thanks in advance for you help.

#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <vector>
#include "bookBinaryTree.h"
#include "bookType.h"
#include "date.h"
#include "memberType.h"
#include "booksBorrowed.h"

using namespace std;

void createBookList(ifstream& infile, bookBinaryTree& bookList); /// creates bookList from input file

vector<memberType> createMemberList(ifstream& infile2, vector<memberType> memberList); /// creates vector memberList from input file

vector<memberType> addNewMember(vector<memberType> memberList); /// allows user to add a new member to memberList

void checkCurrentLoans(map<int, booksBorrowed*> &memberLoans); /// allows user to check current loans

void addNewBook(bookBinaryTree& bookList); /// allows user to add a new book to bookList

map<int, booksBorrowed*> loanBook(bookBinaryTree& bookList, vector<memberType> memberList, 
										  map<int, booksBorrowed*> &memberLoans); /// allows user to check out a book

map<int, booksBorrowed*> returnBook(bookBinaryTree& bookList, vector<memberType> memberList,
											map<int, booksBorrowed*> &memberLoans); /// allows user to check in a book

map<int, booksBorrowed*> renewBook(map<int, booksBorrowed*> &memberLoans); /// allows user to renew a book (new due date)

void updateMemberFile(vector<memberType> memberList); /// print member info to file

void displayMenu(); /// displays menu

int main()
{
	bookBinaryTree bookList; /// declares binary tree for books
	vector<memberType> memberList; /// declares vector of members
	map<int, booksBorrowed*> memberLoans; // declares a map with key string (used for member name) and value 

	ifstream infile;
	ifstream infile2;

	infile.open("bookData.txt"); /// opens text file containing book information
	if(!infile)
	{
		cout << "The input file does not exist. " << endl; /// checking for existance of file
		cout << "Program terminating. " << endl;

		return 1;
	}

	infile2.open("memberData.txt"); /// opens text file containing member information
	if(!infile2) 
	{
		cout << "The input file does not exist. " << endl; /// checking for existane of file
		cout << "Program terminating. " << endl;

		return 1;
	}

	createBookList(infile, bookList); /// creates bookList
	memberList = createMemberList(infile2, memberList); /// creates memberList

	memberLoans = loanBook(bookList, memberList, memberLoans);
	checkCurrentLoans(memberLoans);

	cout << endl;
	cout << "Thanks for using the library." << endl;
	cout << "Program terminating." << endl;

	updateMemberFile(memberList);

	infile.close();
	infile2.close();

	return 0;
}

void displayMenu()
{
	char choice;

		do
		{
			cout << "Enter P for patron menu, S for staff menu." << endl;
			cin >> choice;
			choice = tolower(choice);

			if(choice == 'p')
			{
				cout << "--------------------------------------------" << endl;
				cout << "	Patron Menu" << endl;
				cout << "--------------------------------------------" << endl;
				cout << "1.	Loan a book." << endl;
				cout << "2.	Return a book." << endl;
				cout << "3.	Renew a book." << endl; 
				cout << "4.	Check on current loans." << endl;
				cout << "0.	Exit. " << endl;
				cout << "--------------------------------------------" << endl;
			}
			if(choice == 's')
			{
				cout << "--------------------------------------------" << endl;
				cout << "	Staff Menu" << endl;	
				cout << "--------------------------------------------" << endl;
				cout << "5.	Add new member." << endl;
				cout << "6.	Print list of members." << endl;
				cout << "7.	Add new book." << endl;
				cout << "0.	Exit." << endl;
				cout << "--------------------------------------------" << endl << endl;
			}
			if((choice != 'p')&&(choice != 's'))
				cout << "Incorrect selection." <<endl;

		}while((choice != 'p')&&(choice != 's'));
}

void checkCurrentLoans(map<int, booksBorrowed*> &memberLoans)
{
	map<int, booksBorrowed*>::iterator it;
	for( map<int, booksBorrowed*>::iterator it = memberLoans.begin(); it != memberLoans.end(); it++)
		cout << (*it).first << " has borrowed " << (*it).second->getBook1();

	/// enter id to locate books borrowed in memberLoans map
}

vector<memberType> createMemberList(ifstream& infile2, vector<memberType> memberList)
{
	std::string name;
	int number;

	memberType newMember; /// creates object of memberType to store info

	while(infile2)
	{
		getline(infile2, name);
		infile2 >> number;

		infile2.ignore(100, '\n');

		for(unsigned i = 0; i < name.length(); i++) /// converts all characters to lower case
			name[i] = tolower(name[i]);

		newMember.setName(name);
		newMember.setID(number);

		memberList.push_back(newMember); /// stores newMember in memberList vector
	}

	return memberList;
}

vector<memberType> addNewMember(vector<memberType> memberList)
{
	std::string name;
	int number;

	cout << "Enter details for new member." << endl;
	cout << "Name: ";
	getline(cin, name);
	cout << "ID Number: ";
	cin >> number;
	cin.get();

	memberType newMember; ///creates object of memberType for storing new info

	for(unsigned i = 0; i < name.length(); i++)
		name[i] = tolower(name[i]);

	newMember.setName(name);
	newMember.setID(number);

	memberList.push_back(newMember); /// inserts newMember into the vector memberList

	return memberList;
}

map<int, booksBorrowed*> loanBook(bookBinaryTree &bookList, vector<memberType> memberList,
										  map<int, booksBorrowed*> &memberLoans)
{
	cout << "Enter title to loan book: ";
	std::string title;
	getline(cin, title);

	for(unsigned i = 0; i < title.length(); i++) /// convert title to all lower case
		title[i] = tolower(title[i]);

	int d;
	int dueDay;
	int m;
	int dueMonth;
	int y;
	int dueYear;

	booksBorrowed books; /// create an object of books borrowed
	int id;

	if(bookList.isBookAvailable(title)) /// check if book title is available
	{
		cout << "Enter patron ID: ";
		cin >> id;
		cin.get();

		bool match = false;
	
		for(unsigned i = 0; i < memberList.size(); i++) /// searches memberList vector for memberID
		{
			if(id == memberList[i].getID())
			{
				match = true;
			
				if (bookList.bookSearch(title)) /// if title is in the search tree
				{
					bookList.bookCheckOut(title); /// checks out book, reduces quantity in stock by one

					cout << "Enter today's date (DD/MM/YYYY): " << endl;
					cin >> d;
					cin.get();
					cin >> m;
					cin.get();
					cin >> y;
					cin.get();

					if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12)) /// check if month has 31 days
					{
						if(d < 25)
						{
						dueDay = d + 7;
							dueMonth = m;/// calculating new due date
							dueYear = y;
							cout << "Item checked out." << endl;
							cout << "Due back: " << dueDay << "/" << dueMonth << "/" << dueYear << endl;
						}
						if(d > 24)
						{
							dueDay = (d+7-31);/// calculating new due date
							dueMonth = m + 1;
							dueYear = y;
							cout << "Item checked out." << endl;
							cout << "Due back: " << dueDay << "/" << dueMonth << "/" << dueYear << endl;
						}
					}
					else if(m==2) /// check if month has 28 days
					{
						if(d < 21)
						{
							dueDay = d + 7;/// calculating new due date
							dueMonth = m;
							dueYear = y;
							cout << "Item checked out." << endl;
							cout << "Due back: " << dueDay << "/" << dueMonth << "/" << dueYear << endl;
						}
						if(d > 20)
						{
							dueDay = (d+7-28); /// calculating new due date
							dueMonth = m + 1;
							dueYear = y;
							cout << "Item checked out." << endl;
							cout << "Due back: " << dueDay << "/" << dueMonth << "/" << dueYear << endl;
						}
					}
					else if ((m==4)||(m==6)||(m==9)||(m==11)) /// check if month has 30 days
					{
						if(d < 24)
						{
							dueDay = d + 7;/// calculating new due date
							dueMonth = m;
							dueYear = y;
							cout << "Item checked out." << endl;
							cout << "Due back: " << dueDay << "/" << dueMonth << "/" << dueYear << endl;
						}
						if(d > 23)
						{
							dueDay = (d+7-28);/// calculating new due date
							dueMonth = m + 1;
							dueYear = y;
							cout << "Item checked out." << endl;
							cout << "Due back: " << dueDay << "/" << dueMonth << "/" << dueYear << endl;
						}
					}

					Date dueDate; /// create a Date object with the due date
					dueDate.setDay(dueDay);
					dueDate.setMonth(dueMonth);
					dueDate.setYear(dueYear);
					books.setBook1(title);
					books.setDue1(dueDate);

				}			
			}
		}
	}
	else
		cout << "Book is unvailable at this time." << endl;

	memberLoans.insert(pair<int, booksBorrowed*>(id, &books));

	return memberLoans;
}

map<int, booksBorrowed*> returnBook(bookBinaryTree &bookList, vector<memberType> memberList,
										  map<int, booksBorrowed*> &memberLoans)
{
	cout << "Enter patron ID: ";
	int id;
	cin >> id;
	cin.get();

	bool match = false;
	
		/// finish

	return memberLoans;
}

void addNewBook(bookBinaryTree& bookList)
{
	bookType newBook; /// create a new object of bookType

	std::string title;
	std::string author;
	std::string publisher;
	long isbn;
	int year;	
	double price;
	int quantity;

	cout << "Enter details of new book: " << endl; /// prompt user for book information
	cout << "Title: ";
	getline(cin, title);
	cout << "Author: ";
	getline(cin, author);
	cout << "Publisher: ";
	getline(cin, publisher);
	cout << "ISBN: ";
	cin >> isbn;
	cin.get();
	cout << "Year of publication: ";
	cin >> year;
	cin.get();
	cout << "Price: $";
	cin >> price;
	cin.get();
	cout << "Quantity in stock: ";
	cin >> quantity;
	cin.get();
	cout << endl;
	
	for(unsigned i = 0; i < title.length(); i++) /// set all characters to loewr case
		title[i] = tolower(title[i]);

	for(unsigned i = 0; i < author.length(); i++)/// set all characters to lower case
		author[i] = tolower(author[i]);

	for(unsigned i = 0; i < publisher.length(); i++) /// set all characters to lower case
		publisher[i] = tolower(publisher[i]);

	newBook.setBookInfo(title, author, publisher, isbn, year, price, quantity); /// create new object of bookType

	bookList.insert(newBook); /// insert newBook into search tree
}

void createBookList(ifstream& infile, bookBinaryTree& bookList)
{
	std::string title;
	std::string author;
	std::string publisher;
	long isbn;
	int year;	
	double price;
	int quantity;

	bookType newBook;

	while(infile)
	{
		getline(infile, title);
		getline(infile, author);
		getline(infile, publisher);
		infile >> isbn;					/// getting information from file
		infile >> year;
		infile >> price;
		infile >> quantity;

		infile.ignore(100, '\n');

		for(unsigned i = 0; i < title.length(); i++) /// convert all characters to lower case
			title[i] = tolower(title[i]);

		for(unsigned i = 0; i < author.length(); i++) /// convert all characters to lower case
			author[i] = tolower(author[i]);

		for(unsigned i = 0; i < publisher.length(); i++) /// convert all characters to lower case
			publisher[i] = tolower(publisher[i]);
	
		newBook.setBookInfo(title, author, publisher, isbn, year, price, quantity); /// create newBook bookType

		bookList.insert(newBook); /// insert newBook into search tree
	}
}
map<int, booksBorrowed*> renewBook(map<int, booksBorrowed*> &memberLoans)
{
	cout << "Enter patron ID to renew a book: ";
	int id;
	cin >> id;
	cin.get();

	cout << memberLoans.find(id)->second->getBook1() << " is now due back: ";

	Date currentDate = memberLoans.find(id)->second->getDue1();

	int d = currentDate.GetDay();
	int m = currentDate.GetMonth();
	int y = currentDate.GetYear();
	int dueDay;
	int dueMonth;
	int dueYear;

	/// if statements to calculate new due date

	if((m==1)||(m==3)||(m==5)||(m==7)||(m==8)||(m==10)||(m==12))
	{
		if(d < 25)
		{
			dueDay = d + 7;
			dueMonth = m;
			dueYear = y;
		}
		if(d > 24)
		{
			dueDay = (d+7-31);
			dueMonth = m + 1;
			dueYear = y;
		}
	}
	else if(m==2)
	{
		if(d < 21)
		{
			dueDay = d + 7;
			dueMonth = m;
			dueYear = y;
		}
		if(d > 20)
		{
			dueDay = (d+7-28);
			dueMonth = m + 1;
			dueYear = y;
		}
	}
	else if ((m==4)||(m==6)||(m==9)||(m==11))
	{
		if(d < 24)
		{
			dueDay = d + 7;
			dueMonth = m;
			dueYear = y;
		}
		if(d > 23)
		{
			dueDay = (d+7-28);
			dueMonth = m + 1;
			dueYear = y;
		}
	}

	cout << dueDay << "/" << dueMonth << "/" << dueYear << endl;

	Date newDate(dueDay, dueMonth, dueYear); /// Date object for new due date

	/// need to set new/updated booksBorrowed object and map here

	return memberLoans;
}

void updateMemberFile(vector<memberType> memberList)
{
	/// outpust all member information to a text file

	ofstream outfile;
	outfile.open("memberData.txt");

	for(unsigned i = 0; i < memberList.size(); i++)
	{
		outfile << memberList[i].getName() << endl;
		outfile << memberList[i].getID() << endl;
	}

	outfile.close();
}

Recommended Answers

All 4 Replies

Dude no one is going to go through a 400 line code to find an error. I suggest you to post a smaller piece of code....
Just a random thought I think your issue could be a memory violation.

The error is obvious. At line 197, you use a local variable called "books" to record the booksBorrowed. Then, you insert into your std::map a value which is the address to that local variable. Then, you return from that function, which also causes that local variable to disappear (be destroyed), but you still hold a pointer to it in your std::map. Finally, you try to access it in your "checkCurrentLoans" function which throws an error because you try to access an object that no longer exists (and since that object was allocated on the stack, you get an "abnormal termination" as opposed to the classic "access violation" you normally get when accessing other memory you are not supposed to access since it was destroyed earlier).

To remedy the situation, you need to either allocate the "books" as a pointer to an object newly created on the heap (with new) and delete all the values in your map at the end of the application. Or, you can store the "bookBorrowed" objects by value inside your std::map instead of by pointer.

Line 298 of your code inserts an entry into your map that contains the address of a local variable. When function execution finishes at line 300, that local variable goes away, leaving a dangling pointer in the map.

Later, when you try to access the object to which the pointer points, it is no longer there, causing your program to crash.

@mike_2000_17: Apparently we were typing our respective responses at the same time.

>>
"This application has requested the Runtime to terminate it in an unusual way."

man there is a error code ,if you inside linux then you'll have a core dump.

[linux]
inside linux you need to enable core dump by the following command.
$ulimit -c 64000
[/linux]

and inside microsoft the error code is something like this,
those information are really valuable to track the bug later.
do not always depend on the debugger, debugger is not a developer too,
when you want to use debugger that means your design failed already.
use it to fix only.

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.