fix the code to able to end the program when press "q". PLease Thank you so much

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

fix the code to able to end the program when press "q". PLease Thank you so much

#include <iostream>
#include <cmath>

class Currency {
protected:
    int whole;
    int fraction;
    virtual std::string get_name() = 0;

public:
    Currency() {
        whole = 0;
        fraction = 0;
    }
    Currency(double value) {
        if (value < 0)
            throw "Invalid value";
        whole = int(value);
        fraction = std::round(100 * (value - whole));
    }
    Currency(const Currency& curr) {
        whole = curr.whole;
        fraction = curr.fraction;
    }
    int get_whole() { return whole; }

    int get_fraction() { return fraction; }

    void set_whole(int w) {
        if (w >= 0)
            whole = w;
    }

    void set_fraction(int f) {
        if (f >= 0 && f < 100)
            fraction = f;
    }

    void add(const Currency* curr) {
        whole += curr->whole;
        fraction += curr->fraction;
        if (fraction > 100) {
            whole++;
            fraction %= 100;
        }
    }

    void subtract(const Currency* curr) {
        if (!isGreater(*curr))
            throw "Invalid Subtraction";
        whole -= curr->whole;
        if (fraction < curr->fraction) {
            fraction = fraction + 100 - curr->fraction;
            whole--;
        }
        else {
            fraction -= curr->fraction;
        }
    }

    bool isEqual(const Currency& curr) {
        return curr.whole == whole && curr.fraction == fraction;
    }

    bool isGreater(const Currency& curr) {
        if (whole < curr.whole)
            return false;
        if (whole == curr.whole && fraction < curr.fraction)
            return false;
        return true;
    }

    void print() {
        std::cout << whole << "." << fraction << " " << get_name() << std::endl;
    }
};

class Krone : public Currency {
protected:
    std::string name = "Krone";
    std::string get_name() {
        return name;
    }
public:
    Krone() : Currency() {  }
    Krone(double value) : Currency(value) { }
    Krone(Krone& curr) : Currency(curr) { }
};

class Soum : public Currency {
protected:
    std::string name = "Soum";
    std::string get_name() {
        return name;
    }
public:
    Soum() : Currency() {  }
    Soum(double value) : Currency(value) { }
    Soum(Krone& curr) : Currency(curr) { }
};

int main() {
    Currency* currencies[2] = { new Soum(), new Krone() };
    while (true) {
        currencies[0]->print();
        currencies[1]->print();
        char oprr;
        char oprd;
        double value;
        char curr[10];
   
        std::cin >> oprr >> oprd >> value >> curr;
        std::string currency(curr);
        if (oprr == 'q') {
            return 0;
        }
        try {
            switch (oprr) {
            case 'a':
                if (oprd == 's' && currency == "Soum")
                    currencies[0]->add(new Soum(value));
                else if (oprd == 'k' && currency == "Krone")
                    currencies[1]->add(new Krone(value));
                else
                    throw "Invalid Addition";
                break;
            case 's':
                if (oprd == 's' && currency == "Soum")
                    currencies[0]->subtract(new Soum(value));
                else if (oprd == 'k' && currency == "Krone")
                    currencies[1]->subtract(new Krone(value));
                else
                    throw "Invalid Subtraction";
                break;

            default:
                throw "Invalid Operator";
          
            }

        }
        catch (const char e[]) {
            std::cout << e << std::endl;
        }
        
    }
}

Event Sequence
Program Starts
Add a Soum object
Add a Krone object
Add a Soum object
Add a Krone object
Subtract a Krone object
Subtract a Krone object
Subtract a Soum object
End the program
Sample Input
as 1.11 Soum
a k 12.12 Krone
a k 3.45 Soum
a k 0.13 Krone
s k 10 Krone
s k 2.5 Krone
s s 0.11 Soum
q
Sample Output
0.00 Soum 0.00 Krone
1.11 Soum 0.00 Krone
1.11 Soum 12.12 Krone
Invalid addition
1.11 Soum 12.12 Krone
1.11 Soum 12.25 Krone
1.11 Soum 2.25 Krone
Invalid subtraction
1.11 Soum 2.25 Krone
1.00 Soum 2.25 Krone
Transcribed Image Text:Event Sequence Program Starts Add a Soum object Add a Krone object Add a Soum object Add a Krone object Subtract a Krone object Subtract a Krone object Subtract a Soum object End the program Sample Input as 1.11 Soum a k 12.12 Krone a k 3.45 Soum a k 0.13 Krone s k 10 Krone s k 2.5 Krone s s 0.11 Soum q Sample Output 0.00 Soum 0.00 Krone 1.11 Soum 0.00 Krone 1.11 Soum 12.12 Krone Invalid addition 1.11 Soum 12.12 Krone 1.11 Soum 12.25 Krone 1.11 Soum 2.25 Krone Invalid subtraction 1.11 Soum 2.25 Krone 1.00 Soum 2.25 Krone
Expert Solution
steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Basics of loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education