DEV Community

Muhammad Ali
Muhammad Ali

Posted on

Building a Simple Consecutive Calculator in C++

Hey everyone!

I'm a software engineering student, and I just built a console-based Consecutive Calculator in C++. It allows you to perform basic arithmetic operations continuously on your result until you choose to stop!

Features:

  1. Supports basic operators: Addition (+), Subtraction (-), Multiplication (*), Division (/), and Modulo (%).
  2. Error handling: Prevents crashing on division/modulo by zero.
  3. Continuous mode: Uses the previous result as the first number for the next calculation.

The Code:

Here is the implementation:

`#include <iostream>
using namespace std;
int main()
{
    int first, second, operate, result;
    char consent = 'y';
    cout << "Welcome to single consequetive calculator..." << endl;
    cout << "Enter the first number: ";
    cin >> first;
    do {
        cout << "Enter the second number: ";
        cin >> second;
        cout << "Tell me which computation you want?\n 1. + \n 2. -\n 3. *\n 4. / \n 5. %\n ";
        cout << "Press only numeric keys for computation \n";
        cout << "operator: ";
        cin >> operate;
        switch (operate) {
        case 1: result = first + second; break;
        case 2: result = first - second; break;
        case 3: result = first * second; break;
        case 4:
            if (second == 0) { cout << "Cannot divide by zero!\n"; continue; }
            result = first / second;
            break;
        case 5:
            if (second == 0) { cout << "Cannot modulo by zero!\n"; continue; }
            result = first % second;
            break;
        default:
            cout << "Invalid choice! Try again.\n";
            continue; 
        }

        cout << "Result: " << result << endl;
        cout << "\n Do you want to more compute with the result \n Press 'y' for YES else any key on keyboard... \n ";
        cin >> consent;
        if (consent == 'y' || consent == 'Y')
            first = result;
    } while (consent == 'y' || consent == 'Y');
    return 0;
}`
Enter fullscreen mode Exit fullscreen mode

Please give me feedback on my work or nay tips to improve it...

Top comments (1)

Collapse
 
matthew_faithfull profile image
Matthew Faithfull

Nice. Randomly I was thinking about this exact problem (In the context of testing a minimal console UI) just recently.
Apart from the minor grammar error "Do you want to more compute..." -> "Do you want to compute more..." I would suggest the next challenge is to be able to accept any key being pressed at any time and to only act on the valid ones. I'd like to be able to press + for plus, - for minus etc. as well.
This means the set of valid keys and what to do when they are pressed changes at different points in the code. You might even consider using a state machine to express this.
I once had to implement the entry of Chinese, Japanese, Korean, English and Norwegian + 12 more languages, using only a touch screen number pad, similar to a hand held calculator interface, On Windows CE. An interesting challenge.