Answered step by step
Verified Expert Solution
Question
1 Approved Answer
DO NOT MODIFY MAIN.CPP DO NOT MODIFY MAIN.CPP DO NOT MODIFY MAIN.CPP NameTag.cpp = #include #include #include NameTag.h using namespace std; namespace sdds { void
DO NOT MODIFY MAIN.CPP
DO NOT MODIFY MAIN.CPP
DO NOT MODIFY MAIN.CPP
NameTag.cpp = #include #include #include "NameTag.h" using namespace std; namespace sdds { void NameTag::setEmpty() { m_name = nullptr; m_extension = 0; } bool NameTag::isValid() const { return m_name != nullptr; } void NameTag::allocateAndCopy(const char* name) { int len = 0; for (int i = 0; name[i] != '\0'; i++) { len++; } if (len > 40) { len = 40; } m_name = new char[len + 1]; for (int i = 0; i < len; i++) { m_name[i] = name[i]; } m_name[len] = '\0'; } NameTag::NameTag() { setEmpty(); } NameTag::NameTag(const char* name, int extension) { setEmpty(); // do not set the name if the extension is invalid if (name != nullptr && extension >= 10000 && extension <= 99999) { allocateAndCopy(name); m_extension = extension; } } NameTag::~NameTag() { delete[] m_name; } NameTag& NameTag::read() { setEmpty(); char input[41] = { 0 }; cout << "Please enter the name: "; cin.getline(input, 41); allocateAndCopy(input); char yesNo = 'x'; while (yesNo != 'Y' && yesNo != 'N') { cout << "Would you like to enter an extension? (Y)es/(N)o: "; cin >> yesNo; cin.ignore(10000, ' '); while (yesNo != 'Y' && yesNo != 'N') { cout << "Only (Y) or (N) are acceptable, try again: "; cin >> yesNo; cin.ignore(10000, ' '); } if (yesNo == 'Y') { cout << "Please enter a 5 digit phone extension: "; cin >> m_extension; while (cin.fail() || m_extension < 10000 || m_extension > 99999) { if (cin.fail()) { cout << "Bad integer value, try again: "; cin.clear(); cin.ignore(1000, ' '); cin >> m_extension; } else { cout << "Invalid value, 5-digit phone extension required: "; cin.ignore(1000, ' '); cin >> m_extension; } } cin.ignore(10000, ' '); // add this to clear any leftover characters } else { m_extension = 0; } } return *this; } std::ostream& NameTag::print() const { if (isValid()) { std::ios oldState(nullptr); oldState.copyfmt(std::cout); int box_width = max(20, (int)strlen(m_name)); cout << "+-" << setfill('-') << setw(box_width) << "" << " +" << endl; cout << "| " << setfill(' ') << setw(box_width) << "" << " |" << endl; cout << "| " << left << setw(box_width) << m_name << " |" << endl; cout << "| " << setfill(' ') << setw(box_width) << "" << " |" << endl; cout << "| " << "Extension: " << left << setw(box_width - 11); if (m_extension != 0) { cout << m_extension; } else { cout << "N/A"; } cout << " |" << endl; cout << "| " << setfill(' ') << setw(box_width) << "" << " |" << endl; cout << "+-" << setfill('-') << setw(box_width) << "" << " +" << endl; std::cout.copyfmt(oldState); } else { cout << "EMPTY NAMETAG!" << endl; } return cout; } }
NameTag.h = #ifndef SDDS_NAMETAG_H__ #define SDDS_NAMETAG_H__ #include #include namespace sdds { class NameTag { char* m_name; int m_extension; void setEmpty(); bool isValid()const; void allocateAndCopy(const char* name); public: NameTag(); NameTag(const char* name, int extension = 0); ~NameTag(); NameTag(const NameTag&) = delete; NameTag& operator=(const NameTag&) = delete; NameTag& read(); std::ostream& print()const; }; } #endif // !SDDS_NAMETAG_H__
#include using namespace std; #include "NameTag.h" using namespace sdds; int main() { NameTag nt[6] = { "A very very very long name to be cut short!!!", // one argument constructor {"Fred Soley", 12345} , // two argument constructor with good data {nullptr, 23456}, // two argument constructor with bad name {"Bad number", 1234}, // two argument constructor with bad number {"Bad number", 123456} // two argument constructor with bad number // default constructor (sixth element) }; int i; for (i = 0; i < 6; i++) { nt[i].print(); cout << endl; } cout << "Enter the following:" << endl << "Hubert Blaine Wolfeschlegelsteinhausenbergerdorff" << endl << "x" << endl << "n" << endl; nt[1].read().print(); cout << "Enter the following:" << endl << "Lisa Simpson" << endl << "Y" << endl << "five" << endl << "9999" << endl << "100000" << endl << "12345" << endl; nt[3].read().print(); cout << "Enter the following:" << endl << "Bart Simpson" << endl << "y" << endl << "11111" << endl; nt[5].read().print(); return 0; }
DO NOT MODIFY MAIN.CPP
DO NOT MODIFY MAIN.CPP
DO NOT MODIFY MAIN.CPP
g++ -Wall -std=c++11 -g -o ws main.cpp NameTag .h NameTag.cpp main.cpp: In function 'int main()': main.cpp:13:4: error: use of deleted function 'sdds::NameTag::NameTag(const sdds ::NameTag&)' }; ^ In file included from main.cpp:3:0: NameTag.h:18:9: error: declared here NameTag(const NameTag&) = delete;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
The provided code is a C implementation of a NameTag class It includes the nec...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started