Calculator

Posts 1–10 of 10 · Page 1 of 1
Calculator
I'm building a four function calculator and need to know what loop function is the best to use.
Any one got anything?
I've created four separate programs that do one function individually, but I want to put them all together.
Was thinking of "break"...but idk.
Yes, I know I'm new to C++...even though I'm really not, I used to program a lot, stopped for a year or so, then forgot a good amount...
I only remember the basics.

Addition:

Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

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

{
    //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;
    
    //calculate answer
    double answer;
    answer = num1 + num2;
    
    //declare answer
    cout << "Answer is:";
    cout << answer << endl;
    
    //wait for user to end program to see result
    system("PAUSE");
    return 0;
    
}
Subtraction:

Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

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

{
    //enter first number
    double num1;
    cout << "Enter number to be subtracted from:";
    cin >> num1;
    
    //enter second number
    double num2;
    cout << "Enter second number to be subtracted from first:";
    cin >> num2;
    
    //calculate answer
    double answer;
    answer = num1 - num2;
    
    //declare answer
    cout << "Answer is:";
    cout << answer << endl;
    
    //wait for user to end program to see result
    system("PAUSE");
    return 0;
    
}
Multiplication:

Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

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

{
    //enter first number
    double num1;
    cout << "Enter first number to be multiplied:";
    cin >> num1;
    
    //enter second number
    double num2;
    cout << "Enter second number to be multiplied:";
    cin >> num2;
    
    //calculate answer
    double answer;
    answer = num1 * num2;
    
    //declare answer
    cout << "Answer is:";
    cout << answer << endl;
    
    //wait for user to end program to see result
    system("PAUSE");
    return 0;
    
}
Division:

Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

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

{
    //enter first number
    double num1;
    cout << "Enter number to be divided:";
    cin >> num1;
    
    //enter second number
    double num2;
    cout << "Enter second number to divide by:";
    cin >> num2;
    
    //calculate answer
    double answer;
    answer = num1 / num2;
    
    //declare answer
    cout << "Answer is:";
    cout << answer << endl;
    
    //wait for user to end program to see result
    system("PAUSE");
    return 0;
    
}
You could create a function for each case, from there I guess you could use the switch/case statement and ask for what the user wants to do. ( subtract,add,multiply,divide )

[php]
double Addition(double a,double b)
{
return a+b;
}

int main()
{
double result = Addition(5,10);
cout << result;
cin.get();
}
[/php]

That's just the addition function.

Not sure if this is what you're looking for.
cin>>stringvarhere;
check if it contains +-/* then:
sscanf(stringvarhere, "%d + %d", num1, num2); or something in that direction(dont know exact params)
strtok(string, "*/+-"); // will break statement into two parts for each number

or use scanf like retox

The try ch = strpbrk(string, "*/-+");

switch(ch)

case '+':

etc...

something like that. =/
I'm going to use:

[php]
switch
{
case1;
//etc...
break;

case2;
//etc...
break;

}[/php]

And so on.
Will that work?
Quote Originally Posted by Ryan View Post
I'm going to use:

[php]
switch
{
case1;
//etc...
break;

case2;
//etc...
break;

}[/php]

And so on.
Will that work?
Yes. It will.
Quote Originally Posted by Ryan View Post
Well here's what I finished with.
If anyone could give me some tips, that would be great.

Virustotal. MD5: ff294b9dbd51a460a48802c3f7817069 Suspicious.Insight Win-Trojan/Killfiles.449994
fouropcalc.zip MD5:ff294b9dbd51a460a48802c3f7817069 - VirSCAN.org 3% Scanner(s) (1/36) found malware!

Idk why the fuck it detected a trojan...
Oh well.
Trust a minion.
Tested the addition part of the code. It works fine. Now you have to make it so you can type any equation, without having to choose the options ( add sub,divide,multiply ).

Nice job.
Quote Originally Posted by Void View Post
Tested the addition part of the code. It works fine. Now you have to make it so you can type any equation, without having to choose the options ( add sub,divide,multiply ).

Nice job.
I'll be working on that next.
This calculator's gonna go as far as I can get it to.
I'm relearning functions, so that should help.
I would have thought a use of macros and templates would be the best combination of speed efficiency and usefulness and simplicity.
Code:
#include <iostream>
#define cube(x) ((x)*(x)*(x))
template<class x> x add(x aValue, x bValue)
{
x result;
aValue + bValue;
return(result);
}

// we have to declare the type use in replace of 
// x template for our function(forgot the proper term, overloading might be it even 
// though I think in the case of templates it's different)

int add(int, int);
double add(double, double);
int main()
using namespace std;
{
char input[];
cout << " 5.5 plus 6.8 is... " << add(5.5, 6.8) << endl;
cout << "6 plus 8 is" << add(6, 8) << endl;
cout << "9 plus 12 cubed is" << cube(add(9,12)) << endl;
cout << "Input a number\n";
cout << "Bye!~\n";
// or maybe a cin.get for a pause I don't encourage the use of
// SYSTEM("PAUSE") since it's specific to the VC++ compiler
return 0;
something like that, take advantage of some OOP instead of having to make completely separate functions and etc. You can add the cin stuff to it and all that. char input[] can be an effective way to get values since there's some library functions (I forget, look in the generic <string> C++ styled header) that can convert char input into int and etc.
C has string.h with support for things like
Code:
atof(char*)
// short for ansi to float, but actually is to double
atoi(char*)
// short for ansi to int
// For accurate error testing remember to implement something to keep 
// char constants from input
but I'm not sure about them since their might be a more up to date counter-part in the C++ string header (it lacks the .h extension) Nevertheless use your own methods if you think they're better (that wasn't sass, really if you think they're better then...).
I might make some calculator source myself, it should be professional not only taking two arguments at a time =d the only thing I'm thinking of right now is something with operator overloading for some reason.
Posts 1–10 of 10 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us