C++: Building a mulitfunctional calculator

Chayenne van der Erf

I am trying to build a calculator in C++. I'm new to the program and have to do this for a school assignment, so sorry for my ignorance. English is also my second language so excuse me if I don't make much sense.

Let's say I have two integers A and B for which a user has to assign a value to either add, subtract, etc. How would I then be able add a third integer (let's say X) without all three showing up when I run the program? So instead of having to type a value for A, B, AND X, it only asks to type a value for X?

For example 4 + 5 = 9, but the calculator can also square numbers, so how do I get the option of a user just filling in 4 squared = 16, while still keeping the former code that lets me add and subtract two numbers?

Maybe seeing the code would help understand what I mean? Sorry if I'm confusing.

#include <iostream.h>
#include <conio.h>

int main ()
{
    cout << "Calculator [v.1.0]" << endl;
    cout << "(c) 2021 <Chayenne van der Erf>" << endl << endl;

    cout << "Kies een bewerking en druk op Enter:" << endl;
    cout << "1. Optellen              2. Aftrekken" << endl;
    cout << "3. Vermenigvuldigen      4. Delen" <<endl;
    cout << "5. Kwadraat              6. Worteltrekken" <<endl;
    cout << "7. Reciproke             8. Logarithme" <<endl;
    cout << "0. Exit" << endl << endl;

    int Bewerking;
    cout << "Bewerking: ";
    cin >> Bewerking;

    cout << "" << endl;

    switch (Bewerking) {
    case 1:
        cout << "+";
        break;
    case 2:
        cout << "-";
        break;
    case 3:
        cout << "*";
        break;
    case 4:
        cout << "/";
        break;

    default: "Invalid Number";
    }

    cout << "" << endl << endl;
    double A, B;

    cout << "Enter een waarde: ";
    cin >> A;
    cout << "Enter een waarde: ";
    cin >> B;

    int antwoord;
    if (Bewerking == 1) {antwoord = A + B;}
    else if (Bewerking == 2 ) {antwoord = A - B;}
    else if (Bewerking == 3) {antwoord = A * B;}
    else if (Bewerking == 4) {antwoord = A / B;}

    cout << "" << endl;
    cout << "= " << antwoord << endl;

    getch();
    return 0;
}
molbdnilo

Make the variables, and the reading, conditional on the operation.

Example outline:

if (operation takes one input)
{
    double x;
    cin >> x;
    Calculate result...
}
else if (operation takes two inputs)
{
    double x, y;
    cin >> x >> y;
    Calculate result...
}
else if (operation takes three inputs)
{
    double x, y, z;
    cin >> x >> y >> z;
    Calculate result...
}

Print result...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TOP Ranking

HotTag

Archive