Thursday, 14 March 2013

C++ calculator program

#include <iostream> //basic input output operators
#include <math.h> // additional features for when pausing works

//Shortcuts to avoid having to type std::cout/cin everytime

using std::cout;
using std::cin;
using namespace std;
//Beginning of actual calculator
int main ()
{
     char Done;
     char Subtraction;
     char Addition;
     char Multiplication;
     char Division;
     char answer;
     labelA:
             cout << "What basic mathematical operator shall I perform ?";
     cout << "Please answer with: Division, Multiplication, Addition, Subtraction or Done.";
     cin >> answer;
     cin.get();
     if (answer == Division); //Division section of the calculator
     {
                //Declaring the variables for use in the division function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
             
                long double value1;
                long double value2;
             
                //asking for and storing the values to be divided
               
                      cout << "Input value to be divided: ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the dividend: ";
                      cin >> value2;
                      cin.get();
             
                //dividing the values and outputting the answer
             
                value1 /= value2;
                cout << value1;
                cin.get();
                goto labelA;
     }
     if (answer == Multiplication); //Multiplication section of the calculator
     {
                //Declaring the variables for use in the multiplication function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
             
                long double value1;
                long double value2;
             
                //asking for and storing the values to be multiplied
               
                      cout << "Input value to be first number: ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the second number: ";
                      cin >> value2;
                      cin.get();
             
                //multiplying the values and outputting the answer
             
                value1 *= value2;
                cout << value1;
                cin.get();
                goto labelA;
                }
                if (answer == Addition); //Addition section of the calculator
     {
                //Declaring the variables for use in the addition function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
             
                long double value1;
                long double value2;
             
                //asking for and storing the values to be added
               
                      cout << "Input the first value : ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the second value: ";
                      cin >> value2;
                      cin.get();
             
                //subtracting the values and outputting the answer
             
                value1 += value2;
                cout << value1;
                cin.get();
                goto labelA;
                }
                if (answer == Subtraction); //Subtraction section of the calculator
     {
                //Declaring the variables for use in the subtraction function
                //They can be used again(in a different sub function)
                // because they are declared in a sub function
             
                long double value1;
                long double value2;
             
                //asking for and storing the values to be subtracted
               
                      cout << "Input value to be subtracted from: ";
                      cin >> value1;
                      cin.get();
                      cout << "Input the number to subtract from the first: ";
                      cin >> value2;
                      cin.get();
             
                //subtracted the values and outputting the answer
             
                value1 -= value2;
                cout << value1;
                cin.get();
                goto labelA;
                }
                if (answer == Done); //Function to end the program
                cout << "Good bye";
                return 0;
                }

No comments:

Post a Comment