Author Topic: A Simple Calculator  (Read 3091 times)

Private Clark

  • Megas Domestikos
    Voting Member
  • Posts: 903
  • Karma: 1
    • View Profile
    • Awards
A Simple Calculator
« on: July 16, 2010, 04:59:02 AM »
Alright,
so i know ive been gone a long time mostly because i dont play rtw or mod anymore. I have on the other hand tried my hand at programming, c++ to be exact and have made a couple of simple programs already. Everything is in the console so dont expect anything fancy, also everything was written by hand, every little character is my work :).

This is the first project which i feel is useful enough to post. Its a calculator that runs from the console, it does:
-addition/subtraction
-multiplication/division
-exponents
-percentage
-factorials
-Unit conversions (the more common units, temperature, weight, and distance)

it also remembers the answer from your last operation and automatically uses that in the next operation. Anyways, im not expecting it to change your lives but give it a download, its only 31kb, and if ever you find yourself on a rainy day trying to figure out stupid math and happen to use my program then...il contact you about some sort of monetary arrangement.

So, without further ado:

download the ZIP! the cpp file is the source code, all you really need is the exe.

enjoy, or not.

Update: Thanks to Marcus's fix, the calculator should now work with windows and linux.


Attachments:
« Last Edit: July 17, 2010, 02:01:04 AM by Private Clark »

Marcus

  • Devourer of Souls
  • Megas Domestikos
    Voting Member
  • Posts: 1666
  • Karma: 10
  • I think, therefore I am.
    • View Profile
    • http://exilian.co.uk/
    • Awards
A Simple Calculator
« Reply #1 on: July 16, 2010, 06:29:40 AM »
Cool, glad you provided the source code, allows me to compile it on my linux install. I assume you didn't use any OS specific commands?
"So if you meet me, have some courtesy, have some sympathy, and some taste. Use all your well learned politesse, or I'll lay your soul to waste."

Marcus

  • Devourer of Souls
  • Megas Domestikos
    Voting Member
  • Posts: 1666
  • Karma: 10
  • I think, therefore I am.
    • View Profile
    • http://exilian.co.uk/
    • Awards
A Simple Calculator
« Reply #2 on: July 16, 2010, 06:36:44 AM »
Ok, getting two errors when attempting to compile:

Calculator.cpp: In function
« Last Edit: July 16, 2010, 06:47:37 AM by Marcus »
"So if you meet me, have some courtesy, have some sympathy, and some taste. Use all your well learned politesse, or I'll lay your soul to waste."

Jubal

  • Megadux
    Executive Officer
  • Posts: 22130
  • Karma: 99
  • Awards Awarded for oustanding services to Exilian!
    • View Profile
    • Awards
A Simple Calculator
« Reply #3 on: July 16, 2010, 01:01:23 PM »
Yays for someone using this part of the forum, and doubleyas for the return of Clark! :D

Less yays for;
"This application has failed to start because MSVCP100.dll was not found." :/
« Last Edit: July 16, 2010, 01:03:15 PM by Jubal »
The duke, the wanderer, the philosopher, the mariner, the warrior, the strategist, the storyteller, the wizard, the wayfarer...

Marcus

  • Devourer of Souls
  • Megas Domestikos
    Voting Member
  • Posts: 1666
  • Karma: 10
  • I think, therefore I am.
    • View Profile
    • http://exilian.co.uk/
    • Awards
A Simple Calculator
« Reply #4 on: July 16, 2010, 02:55:10 PM »
Ok, recompiled it using GCC, removed:

System("Pause") replaced with cin.get()
Commented out System("cls")

It now runs fine on Win 7, time for you test it Jubal. :)

Attachments:
"So if you meet me, have some courtesy, have some sympathy, and some taste. Use all your well learned politesse, or I'll lay your soul to waste."

Jubal

  • Megadux
    Executive Officer
  • Posts: 22130
  • Karma: 99
  • Awards Awarded for oustanding services to Exilian!
    • View Profile
    • Awards
A Simple Calculator
« Reply #5 on: July 16, 2010, 08:47:32 PM »
Seems to be working fine with Marc's version. How do I get back to the main calculator console after I start doing unit conversions? After I do each one I just go back to the conversion selector again.

EDIT; also, if I type a rogue value in (in this case, the value of my unit conversion was "k", everything goes tits up.  :D
« Last Edit: July 16, 2010, 08:48:50 PM by Jubal »
The duke, the wanderer, the philosopher, the mariner, the warrior, the strategist, the storyteller, the wizard, the wayfarer...

Marcus

  • Devourer of Souls
  • Megas Domestikos
    Voting Member
  • Posts: 1666
  • Karma: 10
  • I think, therefore I am.
    • View Profile
    • http://exilian.co.uk/
    • Awards
A Simple Calculator
« Reply #6 on: July 17, 2010, 12:33:58 AM »
Yah, he didn't put any numeric validation in. I might try and add that when I have time, if he doesn't already.
"So if you meet me, have some courtesy, have some sympathy, and some taste. Use all your well learned politesse, or I'll lay your soul to waste."

Private Clark

  • Megas Domestikos
    Voting Member
  • Posts: 903
  • Karma: 1
    • View Profile
    • Awards
A Simple Calculator
« Reply #7 on: July 17, 2010, 01:58:24 AM »
Hey,
thanks for pointing that out marcus, ive been using visual c++ as my compiler so i have no idea if it works with other OSs. I dont understand why the program would not work on your system jubal since i wrote this in windows 7 also, im really new to this, maybe when i installed visual c++ it added that dll i guess. As for why you cant go back: the only way i could think of going back to the previous menu would be putting everything in a for loop which was kinda confusing to me so i didnt add it, youl need to quit and restart the app. Also i dont know how to do numeric validation, if you explain marcus il add that in for the next version, im planning on doing an overhaul to the first menu cause im not really satistfied with having to type in 1 for addition etc and want it to be more natural.

EDIT: I might also upload another app that does math formulas for you (areas, perimiters, quadratic etc...) but i imagine that would bring up similar problems, what caused the dll error?

EDIT 2: I also have a really cool graphic tic tac toe game i wrote from scratch with open gl that i will definitely upload once i get it working again. Thing is i wrote it entirely on someone else's computer that i presume already had GLUT installed and so obviously when i brought the project home it wouldnt work, now i think ive installed it correctly but im getting some more compile errors. i asked around at ms's visual c++ forums, il link the article.
« Last Edit: July 17, 2010, 02:06:32 AM by Private Clark »

Marcus

  • Devourer of Souls
  • Megas Domestikos
    Voting Member
  • Posts: 1666
  • Karma: 10
  • I think, therefore I am.
    • View Profile
    • http://exilian.co.uk/
    • Awards
A Simple Calculator
« Reply #8 on: July 17, 2010, 01:35:33 PM »
To go back to the list, just put the whole lot in a while loop that says 'while(true)':

A calc program I wrote a while ago:

Code: [Select]
#include <iostream>

#include <limits> //for numeric_limits

#include <ios> //for streamsize

using namespace std;

int main(void)
{
    float option,operation,num1,num2;

    cout << "Welcome to Bal Jacques' calculator program. Select the operation you want from the list below, by typing in the corresponding number:\n";
    cout << "1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. About\n6. Quit\n";

    while(true)

    {

        cout << "Option: ";
        while(!(cin >> option))

        {

            cout << "Invalid number, try again: ";

            cin.clear();

            cin.ignore(numeric_limits<streamsize>::max(), '\n');

        }
        if (option == 1)
        {
            cout << "This operation will add two numbers.\n";
            cout << "Number 1: ";
            cin  >> num1;
            cout << "Number 2: ";
            cin >> num2;
            operation = num1 + num2;
            cout << "The answer is " << operation << "\n";
        }
        else if (option == 2)
        {
            cout << "This operation will subtract two numbers.\n";
            cout << "Number 1: ";
            cin >> num1;
            cout << "Number 2: ";
            cin >> num2;
            operation = num1 - num2;
            cout << "The answer is " << operation << "\n";
        }
        else if (option == 3)
        {
            cout << "This operation will multiply two numbers.\n";
            cout << "Number 1: ";
            cin >> num1;
            cout << "Number 2: ";
            cin >> num2;
            operation = num1 * num2;
            cout << "The answer is " << operation << "\n";
        }
        else if (option == 4)
        {
            cout << "This operation will divide two numbers.\n";
            cout << "Number 1: ";
            cin >> num1;
            cout << "Number 2: ";
            cin >> num2;
            if (num2 == 0)
            {
                cout << "You cannot divide by zero.\n";
            }
            else
            {
                operation = num1 / num2;
                cout << "The answer is " << operation << "\n";
            }
        }
        else if (option == 5)
        {
            cout << "Calculator App version 1.7 by Bal Jacques.\n";
        }
        else if (option == 6)
        {
            cout << "Thank you for using this program, Goodbye.\n";

            break;
        }
        else
        {
            cout << "Enter a number from 1 to 6.\n";
        }

    }

}

Bit more basic than yours, but shows use of the while loop and numeric validation. I've included the executable below:

Attachments:
"So if you meet me, have some courtesy, have some sympathy, and some taste. Use all your well learned politesse, or I'll lay your soul to waste."

Private Clark

  • Megas Domestikos
    Voting Member
  • Posts: 903
  • Karma: 1
    • View Profile
    • Awards
A Simple Calculator
« Reply #9 on: July 24, 2010, 08:39:47 AM »
ok i was away for the week so il try and upload some stuff this week.

Private Clark

  • Megas Domestikos
    Voting Member
  • Posts: 903
  • Karma: 1
    • View Profile
    • Awards
A Simple Calculator
« Reply #10 on: July 25, 2010, 09:01:56 AM »
Alright in an attempt to get rid of the system calls but still to get the effect of clearing the screen i thought id run this alternative by you guys. Since system calls apparently didnt work for you guys and clrscr () wont compile for me im thinking of jsut making a function that just skips alot of lines. Seems kinda heavy handed but that should not be os specific. If you guys have any other alternatives let me know.

EDIT: That solution looked really funny cause there was always a blank in between the top of the console and the text.. Jubal, your running win7 and the original program didnt work for you?
« Last Edit: July 25, 2010, 09:27:44 AM by Private Clark »

Jubal

  • Megadux
    Executive Officer
  • Posts: 22130
  • Karma: 99
  • Awards Awarded for oustanding services to Exilian!
    • View Profile
    • Awards
A Simple Calculator
« Reply #11 on: July 25, 2010, 11:28:48 AM »
No, I'm running Vista.
The duke, the wanderer, the philosopher, the mariner, the warrior, the strategist, the storyteller, the wizard, the wayfarer...

Marcus

  • Devourer of Souls
  • Megas Domestikos
    Voting Member
  • Posts: 1666
  • Karma: 10
  • I think, therefore I am.
    • View Profile
    • http://exilian.co.uk/
    • Awards
A Simple Calculator
« Reply #12 on: September 16, 2010, 06:52:29 PM »
If you can make it in a similar structure to my example above, the while(true) loop will work just fine.
"So if you meet me, have some courtesy, have some sympathy, and some taste. Use all your well learned politesse, or I'll lay your soul to waste."

Private Clark

  • Megas Domestikos
    Voting Member
  • Posts: 903
  • Karma: 1
    • View Profile
    • Awards
A Simple Calculator
« Reply #13 on: September 19, 2010, 08:31:19 AM »
hmm ive been quite busy lately what with school starting up again so unfortunately programming has taken a back seat at the moment.

Jubal

  • Megadux
    Executive Officer
  • Posts: 22130
  • Karma: 99
  • Awards Awarded for oustanding services to Exilian!
    • View Profile
    • Awards
A Simple Calculator
« Reply #14 on: September 19, 2010, 04:05:19 PM »
School as in you're in education or you're teaching? I can never remember where some of you folks are. :P
The duke, the wanderer, the philosopher, the mariner, the warrior, the strategist, the storyteller, the wizard, the wayfarer...