Calculator

So this is what I have for a calculator so far...

#include<iostream>
using namespace std;

int main(void)
{

system("TITLE Calculator");
system("COLOR 17");
char cChar;
double a;
double b;
char cDoagain;

do
{
system("CLS");
cout << "Enter your first number:" << endl;
cin >> a;
cout << "Enter the operation you want to carry out:" << "(+,-,*, or /)" << endl;
cin >> cChar;
cout << "Enter your second number:" << endl;
cin >> b;

switch (cChar)
{
case '+':
cout << "Your answer is: " << a << "+" << b <<
"=" << (a+b) << endl;
break;
case '-':
cout << "Your answer is: " << a << "-" << b <<
"=" << (a-b) << endl;
break;
case '*':
cout << "Your answer is: " << a << "*" << b <<
"=" << (a*b) << endl;
break;
case 'x':
cout << "Your answer is: " << a << "x" << b <<
"=" << (a*b) << endl;
break;
case 'X':
cout << "Your answer is: " << a << "X" << b <<
"=" << (a*b) << endl;
break;
case '/':
if(b == 0){
cout << "You can't divide by zero" << endl;
}else{
cout << "Your answer is: " << a << "/" << b <<
"=" << (a/b) << endl;
}
break;
default:
cout << "That is an invalid operation" << endl;
break;
}
cout << "Do you have another calculation? (y or n)" << endl;
cin >> cDoagain;
}while (cDoagain == 'y' || cDoagain == 'Y');
system("PAUSE");
return 0;
}

However, I want to know how to be able to add more functionality, like being able to do stuff with more than just two numbers. Also, is it possible to have the functions in ints and allow the functions to overload to allow floats?

Any help would be greatly appreciated.
I want to know how to be able to add more functionality, like being able to do stuff with more than just two numbers
Code needs to be more structured

For example, if you want to be able to write
2
+
3
*
5
you could do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
enum STATE{ENTER_NUMBER1, ENTER_OPERATION, ENTER_NUMBER2};
int main(){
    STATE state = ENTER_NUMBER1;
    //other declarations

    while(...){
        switch(state){
            case ENTER_NUMBER1:
                //enter first number
                //You don't need a break in any of these
            case ENTER_OPERATION:
                //enter +-*/
            case ENTER_NUMBER2:
                //enter second number
                //do your switch{} to calculate the result.
                //store result in the first number
                state = ENTER_OPERATION
        }
    }
}

You could do so, that when user enters C, state = ENTER_NUMBER1.

is it possible to have the functions in ints and allow the functions to overload to allow floats
if you have a function int add(int, int); then you can have an overload float add(float, float);. You cannot however have float add(int, int);
If you're lazy, you could use templates: template<typename T> T add(T, T);
The only problem with that is when user jumps between operation types, such as
2
*
2
+
2
*
2

This would output 12, while the correct result is 8. You could solve this by creating a vector. * and / operations get assigned to current place in the vector, while numbers following + and - operations get assigned to the next value in the vector. For example:

//i=0, j=0, values get assigned to "temp" first (cin >> temp;), +/- operations are assigned to "op"
//if + then op[j]=1, if - then op[j]=2

2 //number[i] = temp
*
3 //number[i] = temp * number[i]
+ //i++; op[j]=1; j++
5 //number[i] = temp
- //i++, op[j]=2; j++
4 //number[i] = temp
*
1 //number[i] = temp * number[i]
0 //calculation ends, i=1, j=0, result = number[0], program loops through "number" and "op":

for(;number[i];i++,j++) {
if (op[j]==1) result = result + number[i];
else if (op[j]==2) result = result - number[i];
else cout << "Error! Improper operations!";
}


I have one question though: How do you create null-terminated vectors? I've read that empty places in a vector get assigned '\0', but when I used vectors the last time, I had to use separate global variable for counting loops, because compiler was telling me that there are no values assigned to that particular place in my vector. I'm using Visual Studio 2010.
Sorry for off-topic question.
okay, so what i originally had is really not even close to what i need lol. according to the directions, i need to meet these criteria:
write a calculator program.
Allow the user to enter a simple math function to evaluate
(Note: the '>' designates input for cin)

>7
>+
>8
>=
result is 15

or:

>6
>+
>8
>-
>3
>=

result is 11


the program will need to evaluate as many numbers and operations until the user enters a '=', at which time the result will be printed
Your program will need a seperate function to evaluate each math operation ( +, -, *, / )
Your program will also need a function to determine which math function to call
The functions you have written for the project have been setup to use read in ints.
You will need to *overload* these functions to also allow for floats

Any help would be great because I dont know where to start
I hope I'm not spoiling it for you, but I've written simple calculator code myself yesterday to see if my way would work. Here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <vector>

using namespace std;

int main() {
	double temp;
	int i=0, j=0, counter=1; //counters
	double result=0;
	char opr='*'; //temporary value for first run
	vector<double> num(20); //up to 20 different numbers
	vector<int> pluMin(19); //up to 19 different +/- operations
	num[0]=1; //temporary value for first run
	bool loop=1; //green light for loop

	cout << "Use +-*/, enter '=' to display result.\n\n";

//Putting numbers into vector, calculating "on the way" if '*' or '/'

	//Number
	do {
		cout << "Number:   ";
		cin >> temp;
		switch (opr) {
			case '+':
				pluMin[j]=1; j++; i++; counter++; num[i]=temp;
				break;
			case '-':
				pluMin[j]=2; j++; i++; counter++; num[i]=temp;
				break;
			case '*':
				num[i]=num[i]*temp;
				break;
			case '/':
				if(temp == 0){
				cout << "You can't divide by zero" << endl;
				}else{
				num[i]=num[i]/temp;
				}
				break;
			default:
				cout << "That is an invalid operation" << endl;
				break;
		}

	//Operation
		cout << "Operator: ";
		cin >> opr;
		if (opr=='=') loop=0;

	} while (loop);

//Calculating '+' and '-' operations
	result=num[0]; i=1; j=0;
	for(;counter;i++,j++,counter--) {
		if (pluMin[j]==1) result = result + num[i];
		else if (pluMin[j]==2) result = result - num[i];
		else if (pluMin[j]==0) break;
		else cout << "Improper operations!"; //Just in case ;)
	}
	cout << "\nResult:   " << result;
	cin >> result;
	return 0;
}


It will calculate * and / operations first, then + and -
Haha, not spoiling it all for me.
Thanks very much
Topic archived. No new replies allowed.