Gantor calculator

GANTOR 0 Tallied Votes 139 Views Share

A simple C++ console calculator that uses square roots as well.

[code]
/*
Title - C++ Console Calculator
Author - GANTOR
Version - 1
Description - A simple calculator that adds, subtracts, divides, multiplies, and uses square roots
of the user's two numbers
*/


#include <cmath>
// <cmath> is the C++ standard library that allows us
// to use operators and other mathematical
// functions. It MUST be included
#include <iostream>

using namespace std;

int main()
{
    system ("COLOR 4a");
    // most novice or even advanced programmers are unaware
    // of this lively trick. "COLOR " declares the system 
    // function of adding color. d8, is a color combination.
    // other combinations can be found by opening up the
    // comand prompt, and typing: color tree
    double firstNumber, secondNumber;
    // double is the variable we are using instead 
    // of int or string. This is because double
    // allows use of decimals and longer integers - like float
    char userChoice;
    // here we define the character variable the user will enter when choosing which operator he will use
    for (;;) {
        do {
            // these are the functions that allow us to use integers in our different cases.
            // this is very similar to if and else if, but must require a closing while statement.
    cout << "\t\t\tGANTOR's C++ Console Calculator\n\n\n";
    // \t tells the IDE to tab the content - in this case 3 times.
    // \n means newline. It is similar to endl, but to me, easier
    cout << "\tChoose the operator you would like to use\n\n";
    // they will choose from, as you know, add, subtract, divide, multiply, and even square root
    cout << "1. Addition\n";
    cout << "2. Subtraction\n";
    cout << "3. Division\n";
    cout << "4. Multiplication\n";
    cout << "5. Radifications\n";
    cout << "Press Q to quit\n\n";
    cout << "\tEnter your number here: ";
    cin >> userChoice;
} while ( userChoice < '1' || userChoice > '6' && userChoice != 'Q');
// this is our closing while loop. It basically says, that if the user's input is less than 1,
// or greater thann 6, and equals Q, then do the following
if (userChoice == 'Q') break;
// break tells the IDE or compiler to either return to the opening page, or in our case, close the program

switch (userChoice) {
       // the switch statement allows us to tell the program what the different cases are
case '1':
     // Addition
     system ("CLS");
     // this function tells the program to clear the current screen, and produce what we want
     cout << "\t\t\t\tAddition\n\n\n";
     cout << "\t\tENTER A NUMBER: ";
     cin >> firstNumber;
     // here the user will enter the first number; the second number will be added
     cout << "\n\n\t\tENTER ANOTHER NUMBER: ";
     cin >> secondNumber;
     cout << firstNumber + secondNumber;
     cout << "\n\n\n\t\t\t";
     // here it states the answer
     system ("PAUSE");
     system ("CLS");
     break;
     // this tells our program to return to the main menu
     
     case '2':
          // Subtraction
          system ("CLS");
          cout << "\t\t\t\tSubtraction\n\n\n";
          cout << "\t\tENTER A NUMBER: ";
          cin >> firstNumber;
          cout << "\n\n\t\tENTER ANOTHER NUMBER: ";
          cin >> secondNumber;
          cout << firstNumber - secondNumber;
          cout << "\n\n\n\t\t\t";
          system ("PAUSE");
          system ("CLS");
          break;
          
     case '3':
          // Division
          system ("CLS");
          cout << "\t\t\t\tDivision\n\n\n";
          cout << "\t\tENTER A NUMBER: ";
          cin >> firstNumber;
          cout << "\n\n\t\tENTER ANOTHER NUMBER: ";
          cin >> secondNumber;
          cout << firstNumber / secondNumber;
          // in the <cmath> library, division = /
          cout << "\n\n\n\t\t\t";
          system ("PAUSE");
          system ("CLS");
          break;
          
     case '4':
          // Multiplication
          system ("CLS");
          cout << "\t\t\t\tMultiplication\n\n\n";
          cout << "\t\tENTER A NUMBER: ";
          cin >> firstNumber;
          cout << "\n\n\t\tENTER ANOTHER NUMBER: ";
          cin >> secondNumber;
          cout << (firstNumber) * (secondNumber);
          cout << "\n\n\n\t\t\t";
          system ("PAUSE");
          system ("CLS");
          break;
          
          
      case '5':
           //Radifications
           system ("CLS");
           cout << "\t\t\t\tRadifications\n\n\n";
           cout << "\t\tENTER A NUMBER: ";
           cin >> firstNumber;
           cout << sqrt (firstNumber);
           cout << "\n\n\n\t\t\t";
           system ("PAUSE");
           system ("CLS");
           break;
           }
           }
           return 0;
           //  this tells the program to close
           }      
           
           
           /*
           
           To whom it may concern,
           
           Thanks for downloading!!
           
           -GANTOR
           */
[/code]
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster

My linux machine says:
- bash: color: command not found
- bash: cls: command not found
- bash: pause: command not found

In other words, avoid using system(), it makes your program non-portable...

GANTOR 0 Newbie Poster

Fair enough. I'm just surprised someone actually checked it out. Was it functional other than that?

Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster

It works nice when I replace all the system("pause"); with :

cin.ignore();
cin.get();

But you could use some extra input checking:
What if I choose 'Add' and then enter 'hello' instead of a number?

William Hemsworth 1,339 Posting Virtuoso

How many C++ console calculators do we have in the C++ snippet section? ;P but, it compiles and runs okay I guess, make sure you write portable code next time :)

mvmalderen 2,072 Postaholic

The next step should be an expression parser, which isn't just evaluating expressions of type x+y, x-y, x*y and x/y (and where you can just enter the whole expression in one time), but also accepts brackets '(' ')' and is just evaluating any type of expressions (bearing in mind that the supported operations are: +,-,*,/ :) ...

William Hemsworth 1,339 Posting Virtuoso

An expression parser is one hell of a step, i'd stay stick with the easier stuff for a while.

GANTOR 0 Newbie Poster

Yeah..I'll see how it goes.

Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster

*mumbles something about boost::regex*

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.