Tuesday, July 11, 2023

DOCTOR! The Calculator is Terminal!! The C++ variety ;)

 

 Hello, and Welcome to my Blog! 

If you're a returning visitor, ...Hello Again!




Quote of the Week:

..but fear itself isn't worthy of our concern. It is who we become while in its clutches.

-RWBY

This week I wanted to jump back into C++ with a relatively straightforward program. There are no pointers or anything fancy. I just needed a breather as I am currently studying the Django and React frameworks.

The calculator has five main capabilities. The regular addition, subtraction, multiplication, and division are included. However, the fifth is called "E," meaning "end." It allows you to smoothly end the application. I have also used cin methods to prevent garbage data from crashing the terminal calculator.

Here's the code below:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    // Defining Varriables
    string operation;
    double number1, number2;
    bool runapp = true;

    // get user input
    while (runapp == true)
    {
        cout << "Enter Operation (+,-,*, /, E): ";
        cin >> operation;

        // if else statement checking entered operation
        if (operation == "+")
        {
            cout << "Enter First Number: ";
            cin >> number1;
            if (cin.fail())
            {
                cin.clear();
            }
            cout << "Enter Second Number: ";
            cin >> number2;
            if (cin.fail())
            {
                cin.clear();
            }

            cout << endl;
            cout << "The Value is: " << number1 + number2 << endl;
        }
        else if (operation == "-")
        {
            cout << "Enter First Number: ";
            cin >> number1;
            if (cin.fail())
            {
                cin.clear();
            }
            cout << "Enter Second Number: ";
            cin >> number2;
            if (cin.fail())
            {
                cin.clear();
            }
            cout << endl;
            cout << "The Value is: " << number1 - number2 << endl;
        }
        else if (operation == "*")
        {
            cout << "Enter First Number: ";
            cin >> number1;
            if (cin.fail())
            {
                cin.clear();
            }
            cout << "Enter Second Number: ";
            cin >> number2;
            if (cin.fail())
            {
                cin.clear();
            }
            cout << endl;
            cout << "The Value is: " << number1 * number2 << endl;
        }
        else if (operation == "/")
        {
            cout << "Enter First Number: ";
            cin >> number1;
            if (cin.fail())
            {
                cin.clear();
            }
            cout << "Enter Second Number: ";
            cin >> number2;
            if (cin.fail())
            {
                cin.clear();
            }
            cout << endl;
            cout << "The Value is: " << number1 / number2 << endl;
        }
        else if (operation == "E")
        {
            cout << "Closing Application" << endl;
            runapp == false;
            break;
        }
        else
        {
            cout << "Invalid Response, Try Again" << endl;
        }
    }

    return 0;
}



That's all for this time. Peace 💣✌

DOCTOR! The Calculator is Terminal!! The C++ variety ;)

    Hello, and Welcome to my Blog!  If you're a returning visitor, ...Hello Again! Quote of the Week: ..but fear itself isn't worthy...