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?
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
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