Question: PLEASE HELP URGENT Issue with this C++ code please fix my codes and repost it. When they are compiled and run I receive the correct

PLEASE HELP URGENT

Issue with this C++ code please fix my codes and repost it. When they are compiled and run I receive the correct output. I keep getting an error please make sure it runs smoothly on matrix. Bellow is the error I keep getting.

In line number 10 of your output:

The output should be:

==========

^

But your output is:

=========

^

Unmatched character details:

The character in column 11 is supposed to be:

[=] ASCII code(61)

but you printed

[Empty Line] ASCII code(0)

//Bellow are my codes and the desired output.

//output sample

TextFile.cpp

#include  #include  #include  #include  #include "TextFile.h" #include "cstring.h" using namespace std; namespace sdds { Line::operator const char* () const { return (const char*)m_value; } Line& Line::operator=(const char* lineValue) { delete[] m_value; m_value = new char[lineValue == nullptr ? 0 : strlen(lineValue) + 1]; strcpy(m_value, lineValue); return *this; } Line::~Line() { delete[] m_value; } void TextFile::setEmpty() { delete[] m_textLines; m_textLines = nullptr; delete[] m_filename; m_filename = nullptr; m_noOfLines = 0; } void TextFile::setFilename(const char *fname, bool isCopy) { delete[] m_filename; if(!isCopy){ m_filename = new char[fname == nullptr ? 0 : strlen(fname) + 1]; strcpy(m_filename, fname); return; } m_filename = new char[fname == nullptr ? 0 : strLen(fname) + 3]; string copy_name = "C_"; for(int i = 0; i < (int)strlen(fname); i++) copy_name += fname[i]; strcpy(m_filename,copy_name.c_str()); } void TextFile::setNoOfLines() { int cnt = 0; ifstream in; in.open(m_filename); string line; while(getline(in, line)) cnt++; in.close(); if(!cnt){ delete[] m_filename; m_filename = nullptr; m_noOfLines = 0; return; } m_noOfLines = cnt; } void TextFile::loadText() { delete[] m_textLines; if(m_filename == nullptr) return; m_textLines = new Line[m_noOfLines]; ifstream in; in.open(m_filename); if(in.is_open()){ string line; for(int i = 0; i < (int)m_noOfLines; i++) { getline(in, line); m_textLines[i] = line.c_str(); } } in.close(); } void TextFile::saveAs(const char *fileName) const { if(fileName == nullptr) return; ofstream _out; _out.open(fileName); if(_out.is_open()) for(int i = 0; i < (int)m_noOfLines; i++) _out << m_textLines[i] << ' '; _out.close(); } TextFile::TextFile(unsigned int pageSize) { m_pageSize = pageSize; this->m_filename = nullptr; this->m_textLines = nullptr; this->m_noOfLines = 0; } TextFile::TextFile(const char *filename, unsigned int pageSize) { m_pageSize = pageSize; this->m_filename = nullptr; this->m_textLines = nullptr; this->m_noOfLines = 0; if(filename != nullptr) { setFilename(filename); setNoOfLines(); loadText(); } } void TextFile::safe(){ this->m_filename = nullptr; this->m_textLines = nullptr; this->m_noOfLines = 0; } TextFile::TextFile(const TextFile& _text) { m_pageSize = _text.m_pageSize; this->m_filename = nullptr; this->m_textLines = nullptr; this->m_noOfLines = 0; setFilename(_text.m_filename, true); _text.saveAs(m_filename); setNoOfLines(); loadText(); } TextFile &TextFile::operator=(const TextFile & _text) { if(this == &_text) return *this; delete[] this->m_textLines; m_textLines = nullptr; _text.saveAs(m_filename); setNoOfLines(); loadText(); return *this; } TextFile::~TextFile() { delete[] this->m_filename; delete[] this->m_textLines; } unsigned TextFile::lines() const { return m_noOfLines; } std::ostream &TextFile::view(ostream &ostr) const { if(!*this) return ostr; ostr << m_filename << endl; ostr.width(m_filename == nullptr ? 0 : strLen(m_filename) - 1); ostr.fill('='); ostr << "" << endl; for(int i = 0; i < (int)m_noOfLines; i++) { if(!(i % m_pageSize) && i) { ostr << "Hit ENTER to continue..."; cin.get(); } ostr << m_textLines[i].m_value << endl; } return ostr; } std::istream &TextFile::getFile(istream &istr) { string a; istr >> a; while(getchar() != ' '); setFilename(a.c_str()); setNoOfLines(); loadText(); return istr; } const char *TextFile::operator[](unsigned int index) const { if(m_textLines == nullptr) return nullptr; return m_textLines[index % m_noOfLines]; } TextFile::operator bool() const { return !(this->m_textLines == nullptr || this->m_filename == nullptr || !this->m_noOfLines); } const char *TextFile::name() const { return m_filename; } std::ostream &operator<<(std::ostream &ostr, const TextFile &text){ text.view(ostr); return ostr; } std::istream &operator>>(std::istream &istr, TextFile &text){ text.getFile(istr); return istr; } }

TextFile.h

#ifndef SDDS_TEXTFILE_H__ #define SDDS_TEXTFILE_H__ #include  namespace sdds { class TextFile; class Line { char *m_value{nullptr}; operator const char *() const; Line() = default; Line &operator=(const char *); ~Line(); friend class TextFile; // copy and copy assignment prevention goes here }; class TextFile { Line *m_textLines{nullptr}; char *m_filename{nullptr}; unsigned m_noOfLines{0}; unsigned m_pageSize{0}; void setFilename(const char *fname, bool isCopy = false); void setNoOfLines(); void loadText(); void saveAs(const char *fileName) const; void setEmpty(); public: explicit TextFile(unsigned pageSize = 15); explicit TextFile(const char *filename, unsigned pageSize = 15); TextFile(const TextFile &); TextFile &operator=(const TextFile &); ~TextFile(); std::ostream &view(std::ostream &ostr) const; std::istream &getFile(std::istream &istr); explicit operator bool() const; unsigned lines() const; const char *name() const; const char *operator[](unsigned index) const; void safe(); }; std::ostream &operator<<(std::ostream &ostr, const TextFile &text); std::istream &operator>>(std::istream &istr, TextFile &text); } #endif // !SDDS_TEXTFILE_H__

cstring.cpp

#include "cstring.h" namespace sdds { void strCpy(char* des, const char* src){ int i = 0; for (i = 0; src[i] != '\0'; i++){ des[i] = src[i]; } des[i] = '\0'; } int strLen(const char* s){ int len = 0, i; for(i = 0; s[i] != '\0'; i++){ len = i+1; } return len; } void strCat(char* des, const char* src){ int length, i = 0; length = strLen(des); while(src[i] != '\0'){ des[length] = src[i]; length++; i++; } des[length] = '\0'; } }

cstring.h

#ifndef SDDS_CSTRING_H_ #define SDDS_CSTRING_H_ namespace sdds { void strCpy(char* des, const char* src); int strLen(const char* s); void strCat(char* des, const char* src); } #endif // !SDDS_CSTRING_H_

w6p1_tester.cpp

#include  #include  #include  #include "TextFile.h" using namespace sdds; using namespace std; void FirstTen(TextFile T); void Copy(const string& dest, const string& source); void Dump(const string& filename); int main() { TextFile Empty; TextFile BadFilename("badFilename"); Copy("echoes.txt", "echoesOriginal.txt"); Copy("seamus.txt", "seamusOriginal.txt"); TextFile E; TextFile S("seamus.txt"); cout << "Type echoes.txt and hit " << endl; cout << "Enter the text file name: "; cin >> E; cout << E << endl; cout << S << endl; FirstTen(E); FirstTen(S); E = S; cout << E << endl; cout << "============================================================" << endl; Dump("echoes.txt"); Dump("seamus.txt"); Dump("C_echoes.txt"); Dump("C_seamus.txt"); cout << "*" << Empty << BadFilename << "*" << endl; return 0; } void FirstTen(TextFile T) { if (T) { cout << ">>> First ten lines of : " << T.name() << endl; for (unsigned i = 0; i < 10; i++) { cout << (i + 1) << ": " << T[i] << endl; } } else { cout << "Nothing to print!" << endl; } cout << endl << "-------------------------------------------------------------" << endl; } void Dump(const string& filename) { cout << "DUMP---------------------------------------------------------" << endl; cout << ">>>" << filename << "<<<" << endl ; ifstream fin(filename.c_str()); char ch; while (fin.get(ch)) cout << ch; cout << endl << "-------------------------------------------------------------" << endl; } void Copy(const string& dest, const string& source) { ifstream fin(source.c_str()); ofstream fout(dest.c_str()); char ch; while (fin.get(ch)) fout << ch; }

And here is a link to the workshop and all the details. Please fix the issue in my code. Thank you.

https://github.com/Seneca-244200/OOP-Workshops/tree/main/WS06

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!