Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am getting this error while running these files. All the required files are give below just need help with solving the error. Exception thrown

I am getting this error while running these files. All the required files are give below just need help with solving the error. Exception thrown at 0x00007FFCAD0EF551 (ucrtbased.dll) in OOP244_Milestone3.exe: 0xC0000005: Access violation reading location 0x0000000000000000. PLZ help. #ifndef SDDS_LIB_H #define SDDS_LIB_H namespace sdds { const int SDDS_MAX_LOAN_DAYS = 15; // maximum number of day a publication can be borrowed with no penalty const int SDDS_TITLE_WIDTH = 30; // The width in which the title of a publication should be printed on the console const int SDDS_AUTHOR_WIDTH = 15; // The width in which the author name of a book should be printed on the console const int SDDS_SHELF_ID_LEN = 4; // The width in which the shelf id of a publication should be printed on the console const int SDDS_LIBRARY_CAPACITY = 5000; // Maximum number of publications the library can hold. } #endif

Date.h

#ifndef SDDS_DATE_H__ #define SDDS_DATE_H__ #include  namespace sdds { extern bool sdds_test; extern int sdds_year; extern int sdds_mon; extern int sdds_day; const int NO_ERROR = 0; const int CIN_FAILED = 1; const int YEAR_ERROR = 2; const int MON_ERROR = 3; const int DAY_ERROR = 4; const char DATE_ERROR[5][16] = { "No Error", "cin Failed", "Bad Year Value", "Bad Month Value", "Bad Day Value" }; const int MIN_YEAR = 1500; class Date { private: int m_year; int m_mon; int m_day; int m_ErrorCode; int m_CUR_YEAR; int daysSince0001_1_1()const; // returns number of days passed since the date 0001/1/1 bool validate(); /* validates the date setting the error code and then returning the result true, if valid, and false if invalid.*/ void errCode(int); // sets the error code int systemYear()const; // returns the current system year bool bad()const; // return true if int mdays()const; // returns the number of days in current month void setToToday(); // sets the date to the current date (system date) friend class Publication; public: Date(); // creates date with current date Date(int year, int mon, int day); /* create date with assigned values then validates the date and sets the error code accordingly */ int errCode()const; // returns the error code or zero if date is valid const char* dateStatus()const; // returns a string corresponding the current status of the date int currentYear()const; // returns the m_CUR_YEAR value; // functions i need to do: std::istream& read(std::istream& is = std::cin); std::ostream& write(std::ostream& os = std::cout)const; // Comparison operator overload methods bool operator==(const Date& right) const; bool operator!=(const Date& right) const; bool operator>=(const Date& right) const; bool operator<=(const Date& right) const; bool operator<(const Date& right) const; bool operator>(const Date& right) const; // Operator- method int operator-(const Date& right)const; // bool type conversion operator operator bool() const; }; std::ostream& operator<<(std::ostream& os, const Date& RO); std::istream& operator>>(std::istream& is, Date& RO); } #endif

Date.cpp

#define _CRT_SECURE_NO_WARNINGS #include  #include  #include  #include  using namespace std; #include "Date.h" namespace sdds { bool sdds_test = false; int sdds_year = 2021; int sdds_mon = 12; int sdds_day = 25; bool Date::validate() { errCode(NO_ERROR); if (m_year < MIN_YEAR || m_year > m_CUR_YEAR + 1) { errCode(YEAR_ERROR); } else if (m_mon < 1 || m_mon > 12) { errCode(MON_ERROR); } else if (m_day < 1 || m_day > mdays()) { errCode(DAY_ERROR); } return !bad(); } int Date::mdays()const { int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, -1 }; int mon = m_mon >= 1 && m_mon <= 12 ? m_mon : 13; mon--; return days[mon] + int((mon == 1) * ((m_year % 4 == 0) && (m_year % 100 != 0)) || (m_year % 400 == 0)); } /*int Date::systemYear()const { time_t t = time(NULL); tm lt = *localtime(&t); return lt.tm_year + 1900; }*/ int Date::systemYear()const { int theYear = sdds_year; if (!sdds_test) { time_t t = time(NULL); tm lt = *localtime(&t); theYear = lt.tm_year + 1900; } return theYear; } /*void Date::setToToday() { time_t t = time(NULL); tm lt = *localtime(&t); m_day = lt.tm_mday; m_mon = lt.tm_mon + 1; m_year = lt.tm_year + 1900; errCode(NO_ERROR); }*/ void Date::setToToday() { if (sdds_test) { m_day = sdds_day; m_mon = sdds_mon; m_year = sdds_year; } else { time_t t = time(NULL); tm lt = *localtime(&t); m_day = lt.tm_mday; m_mon = lt.tm_mon + 1; m_year = lt.tm_year + 1900; } errCode(NO_ERROR); } int Date::daysSince0001_1_1()const { // Rata Die day since 0001/01/01 int ty = m_year; int tm = m_mon; if (tm < 3) { ty--; tm += 12; } return 365 * ty + ty / 4 - ty / 100 + ty / 400 + (153 * tm - 457) / 5 + m_day - 306; } Date::Date() :m_CUR_YEAR(systemYear()) { setToToday(); } Date::Date(int year, int mon, int day) : m_CUR_YEAR(systemYear()) { m_year = year; m_mon = mon; m_day = day; validate(); } const char* Date::dateStatus()const { return DATE_ERROR[errCode()]; } int Date::currentYear()const { return m_CUR_YEAR; } void Date::errCode(int readErrorCode) { m_ErrorCode = readErrorCode; } int Date::errCode()const { return m_ErrorCode; } bool Date::bad()const { return m_ErrorCode != 0; } ostream& operator<<(ostream& os, const Date& RO) { return RO.write(os); } istream& operator>>(istream& is, Date& RO) { return RO.read(is); } // functions i need to do: std::istream& Date::read(std::istream& is) { errCode(NO_ERROR); is >> m_year; is.ignore(); is >> m_mon; is.ignore(); is >> m_day; if (is.fail()) { errCode(CIN_FAILED); is.clear(); } else { if (m_year >= MIN_YEAR && m_year <= currentYear()) { if (m_mon >= 1 && m_mon <= 12) { if (m_day <= mdays() && m_day >= 1) errCode(NO_ERROR); else errCode(DAY_ERROR); } else errCode(MON_ERROR); } else errCode(YEAR_ERROR); } // is.ignore(1000, 'n'); return is; } std::ostream& Date::write(std::ostream& os)const { // If the Date object is in a "bad?state (it is invalid) print the "dateStatus()? if (this->bad()) { os << dateStatus(); } else { // Otherwise, the function should write the date in the following format using the ostream object: // Prints the year os << m_year; // Prints a Slash ?/ ? os << "/"; // Prints the month in two spaces, padding the left digit with zero if the month is a single - digit number os << left << setw(2) << setfill('0') << m_mon; // Prints a Slash ?/ ? os << "/"; // Prints the day in two spaces, padding the left digit with zero if the day is a single - digit number os << left << setw(2) << setfill('0') << m_day; // Makes sure the padding is set back to spaces from zero os << setfill(' '); // Returns the ostream object. } return os; } // Comparison operator overload methods bool Date::operator==(const Date& right) const { if (this->m_day == right.m_day) if (this->m_mon == right.m_mon) if (this->m_year == right.m_year) return true; return false; } bool Date::operator!=(const Date& right) const { return (this->m_year != right.m_year || this->m_mon != right.m_mon || this->m_day != right.m_day); } bool Date::operator>=(const Date& right) const { if (this->m_day >= right.m_day) if (this->m_mon >= right.m_mon) if (this->m_year >= right.m_year) return true; return false; } bool Date::operator<=(const Date& right) const { if (this->m_day <= right.m_day) if (this->m_mon <= right.m_mon) if (this->m_year <= right.m_year) return true; return false; } bool Date::operator<(const Date& right) const { return (this->m_year < right.m_year || this->m_mon < right.m_mon || this->m_day < right.m_day); } bool Date::operator>(const Date& right) const { return (this->m_year > right.m_year || this->m_mon > right.m_mon || this->m_day > right.m_day); } // Operator- method int Date::operator-(const Date& right)const { return (daysSince0001_1_1() - right.daysSince0001_1_1()); } // bool type conversion operator Date::operator bool() const { return (m_year >= 1500 && m_year <= m_CUR_YEAR && m_mon >= 1 && m_mon <= 12 && m_day >= 1 && m_day <= mdays()); } }

Streamable.h

#ifndef SDDS_STREAMABLE_H #define SDDS_STREAMABLE_H namespace sdds { class Streamable { public: virtual std::ostream& write(std::ostream& os)const = 0; // pure virtual std::ostream& read(std::istream& is) = 0; // pure virtual bool conIO(std::ios& io)const = 0; // pure virtual operator bool()const = 0; // pure virtual ~Streamable(); }; std::ostream& operator<<(std::ostream& os, const Streamable& obj); std::istream& operator>>(std::istream& is, Streamable& obj); } #endif

Streamable.cpp

#include #include"Streamable.h" using namespace std; namespace sdds { ostream& operator<<(ostream& os, const Streamable& obj) { obj.write(os); return os; } istream& operator>>(istream& is, Streamable& obj) { obj.read(is); return is; } }

Publication.h

#ifndef SDDS_PUBLICATION_H #define SDDS_PUBLICATION_H #include"Date.h" #include"Lib.h" namespace sdds { class Publication { char* m_title{}; char m_shelfId[SDDS_SHELF_ID_LEN + 1]{}; int m_membership = 0; int m_libRef = -1; Date m_date; void extractChar(std::istream& istr, char ch)const; void set(const char* title, const char* shelfId, int membership, int libRef, Date date); public: Publication(); // Modifiers virtual void set(int member_id); // Sets the membership attribute to either zero or a five-digit integer. void setRef(int value); // Sets the **libRef** attribute value void resetDate(); // Sets the date to the current date of the system. virtual char type()const;//Returns the character 'P' to identify this object as a "Publication object" bool onLoan()const;//Returns true is the publication is checkout (membership is non-zero) Date checkoutDate()const;//Returns the date attribute bool operator==(const char* title)const;//Returns true if the argument title appears anywhere in the title of the //publication. Otherwise, it returns false; (use strstr() function in ) operator const char* ()const;//Returns the title attribute int getRef()const;//Returns the libRef attribute. // Override bool conIO(std::ios& io)const; std::ostream& write(std::ostream& os) const; std::istream& read(std::istream& istr); operator bool() const; // RULE OF THREE : Copy Constructor, User Defined Copy Assignment Operator, Destructor Publication(const Publication& cnt); Publication& operator=(const Publication& cnt); ~Publication(); }; std::ostream& operator<<(std::ostream& os, const Publication& obj); std::istream& operator>>(std::istream& is, Publication& obj); } #endif

Publication.cpp

#define _CRT_SECURE_NO_WARNINGS #include #include #include  #include  #include"Publication.h" #include"Streamable.h" #include"Lib.h" #include"Date.h" using namespace std; namespace sdds { void Publication::extractChar(std::istream& istr, char ch)const { // First, it will peek and see if the next character in the keyboard buffer is the same as the ch argument // If yes, it will read and remove it from the keyboard. (it throws it away!) if (istr.peek() == ch) { istr.get(); } else { // If not, it will set the istream into a fail state. istr.setstate(ios::failbit); } } // Modifiers void Publication::set(int member_id)// Sets the membership attribute to either zero or a five-digit integer. { m_membership = member_id > 9999 && member_id <= 99999 ? member_id : 0; } void Publication::setRef(int value) // Sets the **libRef** attribute value { m_libRef = value; } void Publication::resetDate()// Sets the date to the current date of the system. { m_date.setToToday(); } char Publication::type()const//Returns the character 'P' to identify this object as a "Publication object" { return 'P'; } bool Publication::onLoan()const//Returns true is the publication is checkout (membership is non-zero) { return m_membership != 0; } Date Publication::checkoutDate()const //Returns the date attribute { return m_date; } bool Publication::operator==(const char* title)const//Returns true if the argument title appears anywhere in the title of the publication. Otherwise, it returns false; (use strstr() function in ) { if (strstr(m_title, title) != nullptr) { return true; } return false; } Publication::operator const char* () const//Returns the title attribute { return m_title; } int Publication::getRef()const//Returns the libRef attirbute. { return m_libRef; } bool Publication::conIO(ios& ios) const { // This method is not capable of modifying the Streamable object. conIo receives a reference of an ios object and returns a Boolean. // Functions overriding this function will determine if the incoming ios object is a console IO object or not. return &ios == &cin || &ios == &cout; } ostream& Publication::write(ostream& os) const { if (conIO(os)) { if (bool(*this)) { os << "| " << setw(SDDS_SHELF_ID_LEN) << left << setfill(' ') << m_shelfId << " | " << setw(SDDS_TITLE_WIDTH) << left << setfill('.') << m_title << " | "; m_membership == 0 ? os << " N/A " : os << m_membership; os << " | " << m_date << " |"; } } else { if (bool(*this)) { os << type() << "t" << m_libRef << "t" << m_shelfId << "t" << m_title << "t" << m_membership << "t" << m_date; } // P 2001 P001 The Toronto Star 34037 2021/11/17 } return os; } istream& Publication::read(istream& istr) { // First, clear all the attributes by freeing the memory // and setting everything to their default values. if (m_title != nullptr) { delete[] m_title; m_title = nullptr; } m_shelfId[0] = ''; m_membership = 0; m_libRef = -1; m_date.setToToday(); char* title = nullptr; title = new char[SDDS_TITLE_WIDTH + 1]; char shelfId[SDDS_SHELF_ID_LEN + 1]{}; int membership = 0; int libRef = -1; Date date; if (conIO(istr)) { cout << "Shelf No: "; istr.getline(shelfId, SDDS_SHELF_ID_LEN + 1, 'n'); if (strlen(shelfId) != SDDS_SHELF_ID_LEN) { istr.setstate(ios::failbit); } cout << "Title: "; istr.getline(title, SDDS_TITLE_WIDTH + 1, 'n'); cout << "Date: "; istr >> date; if (date.bad()) { istr.setstate(ios::failbit); } } else { // Otherwise, assume reading begins with libRef attribute as if the type 'P' is not in the file. istr >> libRef; extractChar(istr, 't'); istr >> shelfId; extractChar(istr, 't'); if (shelfId[0] == '') { istr.setstate(ios::failbit); } istr.getline(title, SDDS_TITLE_WIDTH + 1, 't'); if (title == nullptr || title[0] == '') { istr.setstate(ios::failbit); } if (istr.good()) { istr >> membership; istr >> date; } } if (istr.rdstate() == 0) { m_title = new char[strlen(title) + 1]; strcpy(m_title, title); strcpy(m_shelfId, shelfId); m_membership = membership; m_date = date; m_libRef = libRef; } delete[] title; title = nullptr; return istr; } Publication::Publication() { m_title = nullptr; m_shelfId[0] = ''; m_membership = 0; m_libRef = -1; m_date.setToToday(); } Publication::operator bool() const { return m_title != nullptr || m_shelfId[0] != ''; } // HELPER ostream& operator<<(ostream& os, const Publication& obj) { obj.write(os); return os; } istream& operator>>(istream& is, Publication& obj) { obj.read(is); return is; } // RULE OF THREE : Copy Constructor, User Defined Copy Assignment Operator, Destructor Publication::Publication(const Publication& pub) { if (pub.operator bool()) { this->set(pub.m_title, pub.m_shelfId, pub.m_membership, pub.m_libRef, pub.m_date); } } Publication& Publication::operator=(const Publication& pub) { if (this != &pub) { this->set(pub.m_title, pub.m_shelfId, pub.m_membership, pub.m_libRef, pub.m_date); } return (*this); } Publication::~Publication() { delete[] m_title; } void Publication::set(const char* title, const char* shelfId, int membership, int libRef, Date date) { if (m_title != nullptr) { delete[] m_title; m_title = nullptr; } if (title != nullptr) { m_title = new char[strlen(title) + 1]; strcpy(m_title, title); strcpy(m_shelfId, shelfId); m_membership = membership; m_libRef = libRef; m_date = date; } else { m_shelfId[0] = ''; m_membership = 0; m_libRef = -1; m_date.setToToday(); } } }

ms3_tester.cpp

#include #include #include "Publication.h" #include "Utils.h" #include "Date.h"

using namespace std; using namespace sdds; Publication readPublication(istream& istr) { Publication P; cin >> P; return P; } Publication getNextRec(ifstream& ifstr) { Publication P; ifstr >> P; ifstr.ignore(1000, ' '); return P; }

int main() { sdds::sdds_test = true; Publication pd; cout << "An Invalid publication printout:" << endl; cout << ">" << pd << "<" << endl; cout << "Enter the following: " << endl << "P1234" << endl << "------------------------------" << endl; pd = readPublication(cin); if (!cin) { cin.clear(); cin.ignore(1000, ' '); } else { cout << "This is not supposed to be printed!" << endl; } cout << "You entered:" << endl; cout << ">" << pd << "<" << endl; cout << "Enter the following: " << endl << "P123" << endl << "Seneca Weekly" << endl << "2021/13/17" << endl << "------------------------------" << endl; pd = readPublication(cin); if (!cin) { cin.clear(); cin.ignore(1000, ' '); } else { cout << "This is not supposed to be printed!" << endl; } cout << "You entered:" << endl; cout << ">" << pd << "<" << endl; cout << "Enter the following: " << endl << "P123" << endl << "Seneca Weekly" << endl << "2021/11/17" << endl << "------------------------------" << endl; pd = readPublication(cin); cout << "You entered:" << endl; cout << pd << endl; cout << "And the title is agian: \"" << (const char*)pd << "\"" << endl; pd.set(12345); if (pd.onLoan()) { cout << "Now this publication is on loan to a member with the id: 12345" << endl; pd.resetDate(); cout << "The checkout date is: " << pd.checkoutDate() << endl; pd.setRef(9999); cout << "The library unique reference id is: " << pd.getRef() << endl; cout << pd << endl; cout << "----------------------------------------------------------------" << endl; } cout << "Adding the periodical publication to the end of the data file:" << endl; ofstream fileout("Periodicals.txt", ios::app); if (pd) { cout << "appeneded to the file" << endl; fileout << pd << endl; } fileout.close(); cout << endl << "Contents of the file:" << endl; ifstream filein("Periodicals.txt"); char pType{}; for (int row = 1; filein; row++) { filein >> pType; if (pType != 'P') { cout << "The Record type signature is supposed to be B, but it is: " << pType << endl; filein.setstate(ios::failbit); } filein.ignore(); pd = getNextRec(filein); if (filein) { cout << (pd.onLoan() ? "|*" : "| "); cout.width(4); cout.fill(' '); cout.setf(ios::right); cout << row << (pd.onLoan() ? "*" : " "); cout.unsetf(ios::right); cout << pd << (pd == "Star" ? "<<<" : "") << endl; } } return 0; }

Lib.h

#ifndef SDDS_LIB_H #define SDDS_LIB_H

namespace sdds { const int SDDS_MAX_LOAN_DAYS = 15; // maximum number of day a publication can be borrowed with no penalty const int SDDS_TITLE_WIDTH = 30; // The width in which the title of a publication should be printed on the console const int SDDS_AUTHOR_WIDTH = 15; // The width in which the author name of a book should be printed on the console const int SDDS_SHELF_ID_LEN = 4; // The width in which the shelf id of a publication should be printed on the console const int SDDS_LIBRARY_CAPACITY = 5000; // Maximum number of publications the library can hold. }

#endif

Utils.h

#ifndef SDDS_UTILS_H #define SDDS_UTILS_H

namespace sdds {

}

#endif

Utils.cpp

#include #include"Utils.h"

using namespace std;

namespace sdds {

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions