Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The problem I'm facing with this code is it isn't printing force The output should match this exactly I beleive it has something to do
The problem I'm facing with this code is it isn't printing force The output should match this exactly I beleive it has something to do with the display function but I might be wrong. The main.cpp file is not allowed to be changed. Could you please provide help on how to fix this?
Department.cpp = #define _CRT_SECURE_NO_WARNINGS #include#include #include "Department.h" using namespace std; namespace sdds { void Department::updateName(const char* newname) { if (newname[0] != '0' && newname != nullptr) { if (departmentName != nullptr) { delete[] departmentName; } departmentName = new char[strlen(newname) + 1]; strcpy(departmentName, newname); } else { cout << "invalid name, the department's current name will not change." << endl; } } void Department::updateBudget(double change) { if (change > 0) { budget += change; } else { cout << "invalid budget, budget does not change" << endl; } } bool Department::addProject(Project& newproject) { bool check = false; if (budget - totalexpenses() > 0) { if (noOfProject == 0) { ptr_project = new Project[1]; ptr_project[0] = newproject; noOfProject = 1; check = true; } else { Project* temp_ptr_department = new Project[noOfProject + 1]; for (int i = 0; i < noOfProject; i++) { temp_ptr_department[i] = ptr_project[i]; } temp_ptr_department[noOfProject] = newproject; delete[] ptr_project; ptr_project = nullptr; noOfProject++; ptr_project = new Project[noOfProject]; for (int i = 0; i < noOfProject; i++) { ptr_project[i] = temp_ptr_department[i]; } delete[] temp_ptr_department; check = true; } } else { check = false; } return check; } // This function will use functions updateName, addProject and updateBudget to set the name of the department, add a project to it and update its budget. void Department::createDepartment(const char* newname, Project& newproject, double change) { updateName(newname); updateBudget(change); addProject(newproject); } // This function will return the list of projects a department is currently working on. Project* Department::fetchProjects() const { return ptr_project; } // This function will return the number of projects a department is currently working on. int Department::fetchNumProjects() const { return noOfProject; } // This function will return the number of projects a department is currently working on. double Department::fetchBudget() const { return budget; } // This function will return the name of a department. char* Department::fetchName() const { return departmentName; } // This function will calculate and return the expenses of a department. The total expenses of a department is the total cost of all its current projects. double Department::totalexpenses() { int i; for (i = 0; i < noOfProject; i++) { total_cost += ptr_project[i].m_cost; } return total_cost; } // This function will calculate and return the remaining budget of a department by subtracting the total expenses from the budget. double Department::remainingBudget() { return budget - total_cost; } // This function will clear all the dynamic memory allocation in a department. void Department::clearDepartment() { if (ptr_project != nullptr) { delete[] ptr_project; ptr_project = nullptr; } } //fully provided for students to display details of a project void display(const Project& project) { cout << "Project " << project.m_topic << " will cost a total of " << project.m_cost << " C$." << endl; } //fully provided for students to display details of a department void display(const Department& department) { Project* temp = department.fetchProjects(); int projects = department.fetchNumProjects(); cout << "Department " << department.fetchName() << " details:" << endl; cout << "Budget: " << department.fetchBudget() << " and the number of projects in progress is: " << projects << endl; cout << "Below are the details of the projects we are currently working on: " << endl; for (int i = 0; i < projects; i++) { display(temp[i]); } } } Department.h = #ifndef SDDS_DEPARTMENT_H_ #define SDDS_DEPARTMENT_H_ namespace sdds { const int MAX_TOPIC_LENGTH = 25; struct Project { char m_topic[MAX_TOPIC_LENGTH + 1]; double m_cost; }; //class Department does here class Department { Project* ptr_project; char* departmentName; int noOfProject = 0; double budget = 15345.99; double total_cost; // Public functions public: // This function will set the name of the department if the provided name is valid (exists and is not empty) void updateName(const char* newname); // This function will update the budget of the department by adding a new change to it. void updateBudget(double change); // This function will add a new project to the department as long as the total cost does not exceed the allocated budget. bool addProject(Project& newproject); // This function will use functions updateName, addProject and updateBudget to set the name of the department, add a project to it and update its budget. void createDepartment(const char* newname, Project& newproject, double change); // This function will return the list of projects a department is currently working on. Project* fetchProjects() const; // This function will return the number of projects a department is currently working on. int fetchNumProjects() const; // This function will return the number of projects a department is currently working on. double fetchBudget() const; // This function will return the name of a department. char* fetchName() const; // This function will calculate and return the expenses of a department. The total expenses of a department is the total cost of all its current projects. double totalexpenses(); // This function will calculate and return the remaining budget of a department by subtracting the total expenses from the budget. double remainingBudget(); // This function will clear all the dynamic memory allocation in a department. void clearDepartment(); }; //helper functions - do not belong inside the class void display(const Project& project); void display(const Department& department); } #endif // !SDDS_DEPARTMENT_H_ /*********************************************************************** // OOP244 Workshop 3 # p1: tester program // // Filemain.cpp // Version 1.0 // DateOctober 7, 2022 // AuthorMohammad Shamas // Description // // Revision History // ----------------------------------------------------------- // Name Date Reason // ///////////////////////////////////////////////////////////////// ***********************************************************************/ #include #include "Department.h" using namespace std; using namespace sdds; int main() { Project testProject = { "Base",551.55 }; Project myprojects[5] = { {"Synergy", 5041.55}, {"Mecha", 2925.99}, {"Chroma", 3995.45}, {"Force", 6988.45}, {"Oculus", 7441.22} }; Department SDDS{}; //Test1 cout << "**********Create Department TEST START**********" << endl; SDDS.createDepartment("School of Software development", testProject, 1); display(SDDS); cout << "**********Create Department TEST END**********" << endl; //Test2 cout << endl << "**********Update Name TEST Start**********" << endl; SDDS.updateName("School of Software development and design"); display(SDDS); cout << "**********Update Name TEST END**********" << endl; //Test3 cout << endl << "**********Update Budget TEST Start**********" << endl; SDDS.updateBudget(5555.99); display(SDDS); cout << "**********Update Budget TEST END**********" << endl; //Test4 cout << endl << "**********Expenses and Remaining Budget TEST Start**********" << endl; cout << "Our current total Expenses are: " << SDDS.totalexpenses() << endl; cout << "Our remaining budget is: " << SDDS.remainingBudget() << endl; cout << "**********Expenses and Remaining Budget TEST END**********" << endl; //Test5 cout << endl << "**********Add Project TEST START**********" << endl; for (int i = 0; i < 5 && SDDS.addProject(myprojects[i]); i++); display(SDDS); cout << "**********Add Project TEST END**********" << endl; SDDS.clearDepartment(); return 0; }
Script started on Fri 27 Jan 2023 02:16:02 PM EST ==137025== Memcheck, a memory error detector ==137025== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==137025== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info ==137025== Command: ws ==137025== ******Create Department TEST START********** Department School of Software development details: Budget: 15347 and the number of projects in progress is: 1 Below are the details of the projects we are currently working on: Project Base will cost a total of 551.55 C$. *******Create Department TEST END********** ******Update Name TEST Start********** Department School of Software development and design details: Budget: 15347 and the number of projects in progress is: 1 Below are the details of the projects we are currently working on: Project Base will cost a total of 551.55 C$. ******Update Name TEST END********** **********Update Budget TEST Start********** Department School of Software development and design details: Budget: 20903 and the number of projects in progress is: 1 Below are the details of the projects we are currently working on: Project Base will cost a total of 551.55 C$. ******Update Budget TEST END********** *****Expenses and Remaining Budget TEST Start********** Our current total Expenses are: 551.55 Our remaining budget is: 20351.4 *******Expenses and Remaining Budget TEST END********** *******Add Project TEST START********** Department School of Software development and design details: Budget: 20903 and the number of projects in progress is: 5 Below are the details of the projects we are currently working on: Project Base will cost a total of 551.55 C$. Project Synergy will cost a total of 5041.55 C$. Project Mecha will cost a total of 2925.99 C$. Project Chroma will cost a total of 3995.45 C$. Project Force will cost a total of 6988.45 C$. *****Add Project TEST END****** W
Step by Step Solution
There are 3 Steps involved in it
Step: 1
It seems the issue lies in the totalexpenses and remainingBudget functions in your Departmentcpp ...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