# Thread: C++ Calculator

1. ## C++ Calculator

I decided to make a math calculator because the math courses at my college had some geometry (Yes, this calculator also has geometry in it) in them.
Of course, this saves so much time.
I'm planning on adding formulas for more 3-d shapes soon.

I'm not sure if this will work for you windows users since I programmed this with a mac.

Souce code:

/* C++ Calculator made
by Hyperion */

#include <iostream>
using namespace std;

int main ()

{
cout << "This calculator was programmed by Hyperion with c++.";
cout << "\nPlease do not redistribute the program without the authorized";
cout << "\nconsent of the programmer!";

int a, j;
double b, c, d, e, f, g, h, i, k, l, m, n, o, p, q;

cout << "\nWelcome!!!\n";

do
{

cout << "\nWould you like basic arithmetic calculator or geometry calculator?\n";
cout << "\n(Basic Arithmetic = 1 | Geometry = 5):";

cin >> a;

if (a == 1)
{

cout << "\nWould you like to add, subtract, multiply, or divide?";
cout << "\n(Add = 1 | Subtract = 2 | Multiply = 3 | Divide = 4):";

cin >> a;

if (a == 1)
{
cout << "Addition it is. Please enter your first number:";
cin >> b;
cout << "\nPlease enter a number to add with:";
cin >> c;
cout << "\nYour number is\n";
cout << b + c;
}

if (a == 2)
{
cout << "Subtraction it is. Please enter a number:";
cin >> d;
cout << "Please enter a number to subtract with:";
cin >> e;
cout << "\nYour number is\n";
cout << d - e;
}

if (a == 3)
{
cout << "Multiply it is. Please enter a number:";
cin >> f;
cout << "\nPlease enter a number to multiply with:\n";
cin >> g;
cout << "Your number is\n";
cout << f * g;
}

if (a == 4)
{
cout << "Divide it is. Please enter a number:";
cin >>h;
cout << "Please enter a number to divide with:";
cin >> i;
cout << "\nYour number is\n";
cout << h / i;
}

if (a > 4)
{
cout << "Program error!!!";
}

if (a < 1)
{
cout << "Program error!!!";
}
cout << "\nThanks for using the C++ Calculator!\n";
}

if (a == 5)
{
double r, s, t, u;

cout << "\nWhat shape are you trying to find the area/volume of?\n";
cout << "(Square = 1 | Triangle = 2 | Circle = 3 | Rectangle = 4 | Sphere = 5):";
cin >> j;
if (j == 1)
{
cout << "\nPlease enter the side length of the square:";
cin >> k;
cout << "\nThe area of the sqare is\n";
cout << k * k;
}

if (j == 2)
{
cout << "\nPlease enter the base of the triangle:";
cin >> l;
cout << "\nPlease enter the height of the triangle:";
cin >> m;
n = l * m * .5;
cout << "\nThe area of the triangle is\n";
cout << n;
}

if (j == 3)
{
cout << "\nPlease enter the radius of the circle:";
cin >> o;
p = o * o;
q = p * 3.14;
cout << "\nThe area of the circle is\n";
cout << q;
}

if (j == 4)
{
cout << "\nPlease enter the the width of the rectangle:";
cin >> r;
cout << "\nPlease enter the length of the rectange:";
cin >> s;
cout << "\nThe area if the rectangle is\n";
cout << r * s;
}

if (j == 5)
{
cout << "\nPlease enter the radius of the sphere:";
cin >> t;
u = 1.33333333 * 3.14 * t * t * t;
cout << "\nThe voluma of the sphere is\n";
cout << u;
}
cout << "\nThanks for using the C++ Calculator!";
}

} while (a != 0);
return 0;
}

2. ## The Following User Says Thank You to Hyperion For This Useful Post:

[MPGH]Wyo (09-05-2010)

3. nice try
to use a switch.

4. Originally Posted by Toxic Waltz
nice try
to use a switch.
Umm, what?

5. Couldn't you have compiled it into a .exe or a Windows Compatible Format!

6. Originally Posted by Hyperion
Umm, what?
I think he's saying you should try to use a switch. And try making a function plotter (opengl), my attempt failed

7. Originally Posted by Hyperion
Umm, what?
The C switch Statement (C)

and indent your code so it is easier to read.
try to use as less variables as possible...

there also is a C++ section on mpgh

8. Here didnt work in Windows Vista Ultimate but I got a Mac too so i will try there

9. Originally Posted by Xion7200
Couldn't you have compiled it into a .exe or a Windows Compatible Format!
Yeah... I dunno how to do that.

I did lay out the source code for you all so you can just copy and paste the source onto your compiler.

Originally Posted by Toxic Waltz
The C switch Statement (C)

and indent your code so it is easier to read.
try to use as less variables as possible...
Ahh, I didn't get to that lesson yet.

And it's the forum that won't show the indentations.

10. ## The Following User Says Thank You to Hyperion For This Useful Post:

Justin (09-05-2010)

11. Originally Posted by Hyperion
Yeah... I dunno how to do that.

I did lay out the source code for you all so you can just copy and paste the source onto your compiler.
Awww.. I see, Thanks for Explaining!

12. i actually expected a troll hyperion

13. Way too high for me.

14. well good job i guess

15. Originally Posted by Hyperion
And it's the forum that won't show the indentations.
You should be able to wrap code tags around it...

Code:
```#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs, char* pszargs[])

{

cout << "Welcome to Ryan's four function calculator." << endl;
cout << "Functions are defined as follows: 1 - Add. 2 - Sub. 3 - Mult.";
cout << " and 4 - Div." << endl;
cout << "Please enter the function you desire by number." << endl;

int choice;
cout << "Enter 1, 2, 3, or 4:";
cin >> choice;

switch(choice)
{
case 1:         //addition case

//enter first number
double num1;
cout << "Enter first number to be added:";
cin >> num1;

//enter second number
double num2;
cout << "Enter second number to be added:";
cin >> num2;

answer1 = num1 + num2;

cout << "Answer is:";
cout << answer1 << endl;

break;

case 2:             //subtraction case
//enter first number
double num3;
cout << "Enter number to be subtracted from:";
cin >> num3;

//enter second number
double num4;
cout << "Enter second number to be subtracted from first:";
cin >> num4;

answer2 = num3 - num4;

cout << "Answer is:";
cout << answer2 << endl;

break;

case 3:             //multiply case
//enter first number
double num5;
cout << "Enter first number to be multiplied:";
cin >> num5;

//enter second number
double num6;
cout << "Enter second number to be multiplied:";
cin >> num6;

answer3 = num5 * num6;

cout << "Answer is:";
cout << answer3 << endl;

break;

case 4:        //division case

//enter first number
double num7;
cout << "Enter number to be divided:";
cin >> num7;

//enter second number
double num8;
cout << "Enter second number to divide by:";
cin >> num8;

answer4 = num7 / num8;

cout << "Answer is:";
cout << answer4 << endl;

break;

default:
cout << "None of the four operations were chosen." << endl;

}

cout << "Thank you for using my quad operation calculator." << endl;

system("PAUSE");

return 0;

}```
There's my first calculator. /
I used a switch method in this one.
It's not hard at all, you just state cases.
It saves a lot of time as opposed to writing if statement after if statement.

16. Originally Posted by Void
Noice Ryan. |:
Lol. Definitely not.
I'm doing more Java now.
Getting into GUI's.