Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have to do error trapping for my final in introduction to programming and I got this code however there are a couple errors in

I have to do error trapping for my final in introduction to programming and I got this code however there are a couple errors in the code. I am using dev c++ for the class. I was wondering if anyone could help me with this. I have attached the code and the error codes. Any help would be appreciated. Also if you could do a screenshot of the whole code with fixed errors that would help immensely!!

#include

#include

#include

#include

using namespace std;

class FlCalc {

public:

void doCalc(double num1, double num2, char op) {

switch (op) {

case '+':

add(num1, num2);

break;

case '-':

subtract(num1, num2);

break;

case '*':

multiply(num1, num2);

break;

case '/':

divide(num1, num2);

break;

default:

throw "Invalid operator!";

}

}

private:

void add(double num1, double num2) {

cout << "Addition: " << num1 + num2 << endl;

}

void subtract(double num1, double num2) {

cout << "Subtraction: " << num1 - num2 << endl;

}

void multiply(double num1, double num2) {

cout << "Multiplication: " << num1 * num2 << endl;

}

void divide(double num1, double num2) {

if (num2 != 0)

cout << "Division: " << num1 / num2 << endl;

else

throw "Error: Division by zero!";

}

};

class FlCalc2 : public FlCalc {

public:

FlCalc allInOne(double num1, double num2) {

FlCalc result;

result.doCalc(num1, num2, '+');

result.doCalc(num1, num2, '-');

result.doCalc(num1, num2, '*');

result.doCalc(num1, num2, '/');

return result;

}

};

class Tools {

public:

void saveToFile(const string& filename, const string& content) {

ofstream myfile(filename);

if (myfile.is_open()) {

myfile << content;

myfile.close();

cout << "Data saved to file: " << filename << endl;

}

else {

throw "Error: Unable to open file: " + filename;

}

}

string readFromFile(const string& filename) {

string content;

ifstream myfile(filename);

if (myfile.is_open()) {

string line;

while (getline(myfile, line)) {

content += line + " ";

}

myfile.close();

return content;

}

else {

throw "Error: Unable to open file: " + filename;

}

}

};

int main() {

string name;

cout << "Enter name: ";

getline(cin, name);

string choice;

while (true) {

cout << " Menu: ";

cout << "1. All-in-One Calculation ";

cout << "2. Save to File ";

cout << "3. Read from File ";

cout << "4. Read Result File ";

cout << "5. Exit ";

cout << " Please choose: ";

cin >> choice;

try {

if (choice == "1") {

double num1, num2;

cout << "Enter the first number: ";

cin >> num1;

cout << "Enter the second number: ";

cin >> num2;

FlCalc2 flCalc2;

FlCalc result = flCalc2.allInOne(num1, num2);

}

else if (choice == "2") {

string filename, content;

cout << "Enter the filename: ";

cin.ignore();

getline(cin, filename);

cout << "Enter the content: ";

getline(cin, content);

Tools tools;

tools.saveToFile(filename, content);

}

else if (choice == "3") {

string filename;

cout << "Enter the filename: ";

cin.ignore();

getline(cin, filename);

Tools tools;

string content = tools.readFromFile(filename);

if (!content.empty()) {

cout << "Content of file " << filename << ": ";

cout << content;

}

}

else if (choice == "4") {

string filename;

cout << "Enter the result filename: ";

cin.ignore();

getline(cin, filename);

Tools tools;

string content = tools.readFromFile(filename);

if (!content.empty()) {

cout << "Result content of file " << filename << ": ";

cout << content;

}

}

else if (choice == "5") {

cout << name << ", Thank you for using the application. ";

break;

}

else {

throw "Invalid choice! Please try again. ";

}

}

catch (const char* msg) {

cout << msg << endl;

}

}

return 0;

}

However, when I do this code I get these error codes?

How can I fix the code?

Please give screenshot of final code please!!

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

Filing And Computer Database Projects

Authors: Jeffrey Stewart

2nd Edition

007822781X, 9780078227813

More Books

Students also viewed these Databases questions