Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Bellow is my program. so here is the problem. when I check out a book, and quit

ID: 3758804 • Letter: B

Question

Bellow is my program. so here is the problem. when I check out a book, and quit the program, it doesn't save it to the CheckedOutCommic book. I'm suppose to save all the list of checked out books to the CheckedOutCommicBook.dat file so when close the program and reopen it later I can have access to the file and either check out or check in books.

I just need help with the reading and writing the file. My program runs properly, but it doesn't read and write the file to the CheckedOutCommic.dat file. Help please. It looks like I need a forloop, but I don't know how to do it.

#include<iostream>

#include<fstream>
#include<iomanip>
#include<cstring>

//User defined header file
#include"DateClass.h"
#include"ComicBook.h"

//ComicBook object; //object to have access to the comicbook.h file.

using namespace std;

void printMenu();

int main()
{
   int count = 0; //will store number of used locations

   int tempMonth =0, tempDay=0, tempYear=0;
   char userValue;
   Date tempDate;
  
   Date tempDateDuedate;

   char FirstName[20], LastName[30], Title[50];
   int IssueNumber, checkOutDate = 0, dueDate = 0;

   ComicBook mylibrary[600] = {};


   //======================= binary to read file ============================
   fstream file;
   file.open("CheckedOutComics.dat", ios::in);
   if (!file)
   {
       cout << "Error!!!" << endl;
       exit(1);
   }


   //======================= binary to read file ============================
   file.read(reinterpret_cast<char *>(&mylibrary), sizeof(ComicBook));
   file.close();


   //=================Prints out the menu ===============================================
   cout << "Welcome to CS Comic Library Program" << endl;
   cout << "Please enter the date in integers according to the following format mm dd yyyy: ";
   cout << endl;
   cin >> tempMonth >> tempDay >> tempYear;
   cout << endl;

   tempDate.setDate(tempMonth, tempDay, tempYear);
   tempDateDuedate = tempDate;
   tempDateDuedate += 14;

   printMenu();
   cin >> userValue;   //user enters value from menue
   userValue = toupper(userValue); //Convertion from lowercase to upper case

   //while statement for each letter the user choices.
   while (userValue != 'Q')
   {
       switch (userValue)
       {
       case 'C':
       {
           //if statement for checkout option
           if ((userValue) == 'C')
           {
               cout << endl;
               cout << "Please enter one line consisting the first and last names "
                   << "of the author followed by the title of the comic book and "
                   << "the issue number." << endl;
              
               //user will enter the information
               cin >> FirstName >> LastName;
               cin.ignore();
               cin.getline(Title,50, '#');
               cin >> IssueNumber;

               //=========================== COUNT # of checked out books===============
               mylibrary[count].setFirstName(FirstName);
               mylibrary[count].setLastName(LastName);
               mylibrary[count].setTitle(Title);
               mylibrary[count].setIssueNumber(IssueNumber);

              
               //================== set check out date ===================================
              
               mylibrary[count].setCheckOutDate(tempDate);
               mylibrary[count].setDueDate(tempDateDuedate);

               count++;

              
           }
           cout << endl;
           cout << " Checking out a comic book";
           break;
       }
       case 'D':
       {
           //if statement for check in option
           if ((userValue) == 'D')
           {
               cout << endl;
               cout << "Please enter one line consisting the first and last names "
                   << "of the author followed by the title of the comic book and "
                   << "the issue number." << endl;
              
               //User will enter the information
               cin >> FirstName >> LastName;
               cin.ignore();
               cin.getline(Title, 50, '#');
               cin >> IssueNumber;

               //forloop to check list of comic books checked out
               for (int i = 0; i < count; i++)
               {      
                   //ifstatement to comare similarities of checked out book name and the information the user entered.
                   if ((strcmp(FirstName,mylibrary[i].getFirstName()) == 0) && (strcmp(LastName,mylibrary[i].getLastName()) == 0)
                       && (strcmp(Title,mylibrary[i].getTitle()) == 0) && (IssueNumber == mylibrary[i].getIssueNumber()))
                   {

                       mylibrary[i] = mylibrary[i + 1];
                       count--;

                       //ifstatmnt to compare the dates when option d is entered.
                       if (mylibrary[i].getDueDate() < tempDate)
                       {
                           cout << " The comic book is not overdue..." << endl;
                       }
                       else
                       {
                           cout << " The comic book is overdue..." << endl;
                       }

                   }
                   else
                   {
                       cout << "The data you have entered is not in the records.";
                   }
               }

               //=========================== COUNT # of checked in books left===============
           }
           cout << " Checking in a comic book";

           break;
       }
       case 'T':
       {
           //if statement to print out record of books
           if ((userValue) == 'T')
           {

               cout << "Printing all comic books currently checked out";
               cout << endl << endl;
               cout << setw(2) << "Author" << setw(35) << "Title & Issue #" << setw(25)
                   << "Chekc Out Date" << setw(15) << "Due Date" << endl;


               for (int i = 0; i < count; i++)
               {
                   cout << " " << mylibrary[i].getFirstName() << " " << mylibrary[i].getLastName() << " " << mylibrary[i].getTitle()
                       << " #" << mylibrary[i].getIssueNumber() << " "
                       << mylibrary[i].getCheckOutDate() << " " << mylibrary[i].getDueDate() << " ";
               }
           }

           break;
       }
       case 'Q':   //This will quit the program if user enters it.
       {
           cout << " Thank you for using CS Comic Library Program" << endl << "GOOD BYE!!!" << endl << endl;

           break;
       }
       default :
       {
           cout << " Error!!! You have entered an invalid value " << endl << " Please try again" << endl;
           break;
       }
       } // end of switch

       cout << endl;
       cout << " Please select another option. ";
       cin >> userValue;
       userValue = toupper(userValue); //Convertion from lowercase to upper case

   }// end of while loop


   //======================= binary to write file ============================
   fstream writefile("CheckedOutComics.dat", ios::out | ios::binary);
   if (!writefile)
   {
       cout << "Error. File couldn't open." << endl;
       exit(1);
   }

   for (int i = 0; i < count; i++)
   {
       writefile.write(reinterpret_cast<char *>(&mylibrary[i]), sizeof(ComicBook));
   }
   writefile.close();


   return 0;
}

// void function to print out the list of commics
void printMenu()
{
   cout << "____________________________________________________" << endl;
   cout << "Please enter your one letter choice as follows: " << endl;
   cout << "C:   Check out a comic book " << endl;
   cout << "D: Check in a comic book " << endl;
   cout << "T: Print all comic books currently checked out " << endl;
   cout << "Q: Quit this program " << endl;
   cout << endl;
}

//=====================comicbook.h=============================================

// Definition of class Date
#ifndef COMIC_H
#define COMIC_H
#include<iostream>
#include<string>

using namespace std;

class ComicBook
{
   friend ostream &operator << (ostream &, const ComicBook &);

public:
   ComicBook(char *FirstName = "First", char *LastName = "Last", char *Title = "Title", int IssueNumber = 'I', Date checkOutDate = { 1,1,1900 }, Date dueDate = { 1,1,1900 }); // Add Default values
   ~ComicBook();
   ComicBook & setCheckOutDate(Date);
   ComicBook & setDueDate(Date);
   ComicBook & setFirstName(const char *);
   ComicBook & setLastName(const char *);
   ComicBook & setTitle(const char *);
   ComicBook & setIssueNumber(const int);
   const char * getFirstName() const;
   const char * getLastName() const;
   const char * getTitle() const;
   const int getIssueNumber() const;
   Date getCheckOutDate() const;
   Date getDueDate() const;

private:
   char firstName[20];
   char lastName[30];
   char title[50];
   int issue;
   Date checkOut;
   Date due;


};

//comic book function
ComicBook::ComicBook(char *FirstName, char *LastName, char *Title, int IssueNumber, Date checkOutDate, Date dueDate)
{
   strcpy_s(firstName, FirstName);
   strcpy_s(lastName, LastName);
   strcpy_s(title, Title);
   issue = IssueNumber;
   checkOut = checkOutDate;
   due = dueDate;
}

ComicBook :: ~ComicBook()
{

}

//Checkout date function
ComicBook & ComicBook :: setCheckOutDate(Date checkOutDate)
{
   checkOut = checkOutDate;
   return(*this);
}

//Check in function
ComicBook & ComicBook :: setDueDate(Date dueDate)
{
   due = dueDate;
   return(*this);

}

//SetFirstName function
ComicBook & ComicBook :: setFirstName(const char *FirstName)
{
   strcpy_s(firstName,FirstName);
   return(*this);

}

//SetLastName function
ComicBook & ComicBook::setLastName(const char *LastName)
{
   strcpy_s(lastName,LastName);
   return(*this);
}

//Set title function
ComicBook & ComicBook :: setTitle(const char *Title)
{
   strcpy_s(title, Title);
   return(*this);
}


//Set user function
ComicBook & ComicBook :: setIssueNumber(const int IssueNumber)
{
   issue = IssueNumber;
   return(*this);
}


//get firstName function
const char * ComicBook :: getFirstName() const
{
   return firstName;
}


//get LastName function
const char * ComicBook :: getLastName() const
{
   return lastName;
}

//get title function
const char * ComicBook :: getTitle() const
{
   return title;
}

//================================dateclass.h==================================

// Definition of class Date
#ifndef DATE1_H
#define DATE1_H
#include <iostream>
#include <string>

using namespace std;

class Date {
   friend ostream &operator<<(ostream &, const Date &);
public:
   Date(int m = 1, int d = 1, int y = 1900); // constructor, note the default values
   void setDate( int, int, int ); // set the date
   bool operator < (Date other) const; // You need to implement this
   const Date &operator+=( int ); // add days, modify object
   bool isLeapYear(int) const; // is this a leap year?
   bool isEndOfMonth(int) const; // is this end of month?
   int getMonth() const; // You need to implement this
   int getDay() const; // You need to implement this
   int getYear() const; // You need to implement this
   string getMonthString() const; // You need to implement this

private:   
   int month;
   int day;
   int year;

   static const int days[]; // array of days per month
   static const string monthName[]; // array of month names
   void helpIncrement(); // utility function
};


// Member function definitions for Date class

// Initialize static members at file scope;
// one class-wide copy.
const int Date::days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

const string Date::monthName[] = { "", "January", "February", "March", "April", "May", "June",
"July", "August", "September", "October",
"November", "December" };


// Date constructor
Date::Date(int m, int d, int y) { setDate(m, d, y); }

// Set the date
void Date::setDate(int mm, int dd, int yy)
{
   month = (mm >= 1 && mm <= 12) ? mm : 1;
   year = (yy >= 1900 && yy <= 2100) ? yy : 1900;

   // test for a leap year
   if (month == 2 && isLeapYear(year))   
       day = (dd >= 1 && dd <= 29) ? dd : 1;   
   else
       day = (dd >= 1 && dd <= days[month]) ? dd : 1;
}


//comparing two dates.  
bool Date :: operator < (Date other) const
{
   if ((*this).year <= other.year)
       return true;
   if ((*this).month <= other.month)
       return true;
   if ((*this).day <= other.month)
       return true;
   else
       return false;
}

// Add a specific number of days to a date
const Date &Date::operator+=( int additionalDays )
{
   for (int i = 0; i < additionalDays; i++)   
       helpIncrement();

   return *this; // enables cascading
}

// If the year is a leap year, return true;
// otherwise, return false
bool Date::isLeapYear(int testYear) const
{
   if (testYear % 400 == 0 || (testYear % 100 != 0 && testYear % 4 == 0))
   return true; // a leap year
   else
   return false; // not a leap year
}


// Determine if the day is the end of the month
bool Date::isEndOfMonth(int testDay ) const
{
   if (month == 2 && isLeapYear(year))
       return (testDay == 29); // last day of Feb. in leap year else
   return (testDay == days[month]);
}


//function to get month
int Date :: getMonth() const
{
   return month;
}

//function to get day
int Date::getDay() const
{
   return day;
}

//function to get year
int Date::getYear() const
{
   return year;
}

//function to get month string
string Date::getMonthString() const
{
   return monthName[month];
}


// Function to help increment the date
void Date::helpIncrement()
{
   if (!isEndOfMonth(day))
   { // date is not at the end of the month   
       day++; }
   else if (month < 12)
   { // date is at the end of the month, but month < 12
       day = 1; ++month;
   }
   else // end of month and year: last day of the year
   {
       day = 1; month = 1;
       ++year;
   }
}

   // Overloaded output operator
   ostream &operator<<(ostream &output, const Date &d)
   {
       output << d.monthName[d.month] << ' '
           << d.day << ", " << d.year;

       return output; // enables cascading
   }

#endif ;


//get issue function
const int ComicBook :: getIssueNumber() const
{
   return issue;
}

//get chekcout date function
Date ComicBook :: getCheckOutDate() const
{
   return checkOut;
}

//get due date function
Date ComicBook :: getDueDate() const
{
   return due;
}


#endif;

//Jay helped me how to implement the ComicBook function.

Explanation / Answer

#include <fstream>
#include <string>

int main()
{
    std::ifstream file("Read.txt");
    std::string str;
    while (std::getline(file, str))
    {
        // Process str
    }
}

For Reading you can use the above code
================================
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
    while ( getline (myfile,line) )
    {
      cout << line << ' ';
    }
    myfile.close();
}

else cout << "Unable to open file";

return 0;
}

For Writing to file you can use the above code

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote