Make a single use calculator!

All tutorials created in C++ to be posted in here.
3 posts Page 1 of 1
Contributors
User avatar
Duggypker
New Member
New Member
Posts: 10
Joined: Tue Mar 23, 2010 3:10 am

Make a single use calculator!
Duggypker
Why hello... Yes it's been quite awhile since I've posted.. But I have been very busy. Anyway!

today I am hear to talk about making a calculator. It can do anything.. (Besides decimal numbers and fractions)

But the basic Addition, Multiplication, subtraction, and division. Let us take a look shall we?

First off start with your basic requirements.
Code: Select all
#include <iostream>

using namespace std;

int main(){

}
Underneath int main(){ create three new integers, I named mine a, b, and c

Here we are:
Code: Select all

#include <iostream>

using namespace std;

int main(){

int a;
int b;
int c;
}
Next we will add our function that allows our user to select which type of problem.
Code: Select all
#include <iostream>

using namespace std;

int main(){

int a;
int b;
int c;

    cout << "What type of equation?\n";
    cout << " 1. Addition\n";
    cout << " 2. Subtraction\n";
    cout << " 3. Multiplication\n";
    cout << " 4. Division\n";
    cin >> a;
}
Now that our user can select the type of problem. Let's move onto the actual equations.
Remember only REAL numbers work no decimals or fractions.
Now in order to separate my different types of problems and make them accessible by command using numbers I will use a case/switch method here.
Code: Select all
#include <iostream>

using namespace std;

int main(){

int a;
int b;
int c;

    cout << "What type of equation?\n";
    cout << " 1. Addition\n";
    cout << " 2. Subtraction\n";
    cout << " 3. Multiplication\n";
    cout << " 4. Division\n";
    cin >> a;

    switch(a) {
        case 1:
         cout << "Addition problem\n";
         cout << "Enter the first number: ";
         cin >> b;
         cout << "Now enter the second number: ";
         cin >> c;
         cout << "Your answer is: " << b+c;
         return 0;

         case 2:
         cout << "Subtraction Problem\n";
         cout << "Enter the First Number: ";
         cin >> b;
         cout << "Now enter the second number: ";
         cin >> c;
         cout << "Your answer is: " << b-c;
         return 0;

         case 3:
         cout << "Multiplication Problem\n";
         cout << "Enter the First Number: ";
         cin >> b;
         cout << "Now enter the second number: ";
         cin >> c;
         cout << "Your answer is: " << b*c;
         return 0;

         case 4:
         cout << "Devision Problem\n";
         cout << "Enter the First Number: ";
         cin >> b;
         cout << "Now enter the second number: ";
         cin >> c;
         cout << "Your answer is: " << b/c;
         return 0;
    }

}
All that switch(a) { does is reads whatever the user put in as a example: 1 would go to case 1.
I made sure I labeled each equation correctly so our user knows what he is doing. Unfortunately in order to use this program again it will have to be restarted. Covering this fix I shall post later on... Below I have attached my program for anyone to use... Thanks! Post any feedback and comments!

Mini Calculator - 575 Kb

-Duggypker

EDIT: I forgot to mention in case they put a number besides 1-4. to prevent something bad from happening put this:
Code: Select all
         default:
         cout << "That is an invalid selection.. Please restart and try again 1-4.";
         return 0;
Image
Current Project: From the Ashes RTS
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Make a single use calculator!
mandai
You could either use goto or a while/for loop to run through this multiple times.
User avatar
Duggypker
New Member
New Member
Posts: 10
Joined: Tue Mar 23, 2010 3:10 am

Re: Make a single use calculator!
Duggypker
Yes I know but I was going to make an addon tutorial to implement this system when I made the working code. :3 right now Liven good and I are programming something so its kinda hard to do so many things at once :3
Image
Current Project: From the Ashes RTS
3 posts Page 1 of 1
Return to “C++ Tutorials”