Tags:

How to Make a basic C++ Calculator

Homura Aug 14, 2013

  1. Homura

    Homura Creator of Avalon XPG Developer Lifetime Gold
    85/94

    Joined:
    Mar 8, 2011
    Messages:
    857
    Likes Received:
    789
    Trophy Points:
    85
    Gender:
    Male
    Location:
    Wouldn't you like to know.
    Console:
    Xbox
    Okay first off to do this you will be needing the basic idea of if statements, loops etc. They are common in pretty much all programming languages so if you can use them in one language you should get the idea of what to do in another one.. Now this is the most basic program beside a hello world program that you will ever make.

    First off make a new project. Once you have done that go in over to Source Files and right click it to add new item and add a c++ file (.cpp) and name it Main.cpp, once you have done that at the top you will want to do this:

    //These are what you need at the top of all c++ files. If you want you can also label everything so you know what they are.
    #include<iostream>
    using namespace std;

    Once you have done that you are ready to begin programming the calculator. First start by making the

    int Main()
    {

    }

    Now that you have done that you can set your Variables inside the int Main.

    int main()
    {
    //Variables
    bool done = false;
    float total = 0;
    int amount;
    char answer;

    }

    Now that you have that done you can begin coding the calculator.

    int main()
    {
    //Variables
    bool done = false;
    float total = 0;
    int amount;
    char answer;
    //While,for,switch functions for calc
    while(!done)
    {
    cout << "Enter the amount of numbers: ";
    cin >> amount;

    for(int i = 0; i < amount; i++)
    {
    char op;
    float number;

    cout << "Enter the operator: " << total << " ";
    cin >> op;
    cout << "Enter a number: ";
    cin >> number;

    switch(op)
    {
    case '+':
    total += number;
    break;
    case '-':
    total -= number;
    break;
    case '/':
    total /= number;
    break;
    case '*':
    total *= number;
    break;
    }
    }
    cout << "Total: " << total << endl;

    do
    {
    cout << "Do you want to run this program again? (y/n)" << endl;
    }
    while(answer != 'y' && answer != 'n');

    if(answer == 'n')
    done = true;
    else
    {
    total = 0;
    system ("cls");
    }
    }

    system("pause");
    return 0;
    }


    Maybe later I will make a more advanced tutorial but for now this will help with basics of c++, feel free to mess with my code and be sure to code it differently and experiment with what you can do. Anyway I hope you enjoy this little tutorial.
     
    • Like Like x 3

Share This Page

Close