A simple C++ console calculator that uses square roots as well.
Gantor calculator
[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
GANTOR 0 Newbie Poster
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster
William Hemsworth 1,339 Posting Virtuoso
tux4life 2,072 Postaholic
William Hemsworth 1,339 Posting Virtuoso
GANTOR 0 Newbie Poster
Nick Evan 4,005 Industrious Poster Team Colleague Featured Poster
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.