Alright. I've been following the tutorials offered by this site, and I've done a whole slew of lessons. So I'm trying to combine most of them into one program, and I've chosen to make a basic calculator. I completed it, and it works nicely. It first began with integers, now I've allowed it to use decimals as well. So, here's the code for the calculator.
Code:
#include <iostream>
using namespace std;
int main()
{
int input;
float inputadd1;
float inputadd2;
float addinganswer;
float divideanswer;
float function;
cout<<"Awesome Calculator is Awesome.\n\n";
do
{
cout<<"1 = Add\n";
cout<<"2 = Subtract\n";
cout<<"3 = Multiply\n";
cout<<"4 = Divide\n";
cout<<"5 = Quit\n";
cout<<"Selection: ";
cin>> input;
switch ( input ){
case 1:
cout<<"\nFirst number: ";
cin>> inputadd1;
cout<<"\nAdd what to it?: ";
cin>> inputadd2;
addinganswer = (inputadd1 + inputadd2);
cout<<"--------------------------------------\n";
cout<<""<< inputadd1 <<" + "<< inputadd2 <<" = "<< addinganswer <<"\n";
cout<<"--------------------------------------\n\n";
cin.get();
break;
case 2:
cout<<"\nFirst number: ";
cin>> inputadd1;
cout<<"\nSubtracted by?: ";
cin>> inputadd2;
addinganswer = (inputadd1 - inputadd2);
cout<<"--------------------------------------\n";
cout<<""<< inputadd1 <<" - "<< inputadd2 <<" = "<< addinganswer <<"\n";
cout<<"--------------------------------------\n";
cin.get();
break;
case 3:
cout<<"\nFirst number: ";
cin>> inputadd1;
cout<<"\nMultiplied by?: ";
cin>> inputadd2;
addinganswer = (inputadd1 * inputadd2);
cout<<"--------------------------------------\n";
cout<<""<< inputadd1 <<" x "<< inputadd2 <<" = "<< addinganswer <<"\n";
cout<<"--------------------------------------\n";
cin.get();
break;
case 4:
cout<<"\nFirst number: ";
cin>> inputadd1;
cout<<"\nDivided by?: ";
cin>> inputadd2;
divideanswer = (inputadd1 / inputadd2);
cout<<"--------------------------------------\n";
cout<<""<< inputadd1 <<" / "<< inputadd2 <<" = "<< divideanswer <<"\n";
cout<<"--------------------------------------\n";
cin.get();
break;
case 5:
cout<<"Quitting...";
break;
default:
cout<<"--------------------------------------\n";
cout<<"\nBad input..\n";
cout<<"--------------------------------------\n";
break;
}
}while (input != 5);
}
Now, there is nothing (that I know), that is wrong with this program. I've compiled it many times, and it works perfectly. I don't need help with fixing it, or anything like that. What I want to do, is to shrink the program down. To optimize it. I see a clear way that I can do it, but I need to know one thing: If it's possible to store math operations in a variable. For example:
A = +
B = -
C = /
D = *
Though in my program, it'll all be stored in one variable, but you get the idea. I've tried using the 'float' variable, though it doesn't work...Or I'm just using it incorrectly. So, here's a little snippit of what I tried: (I'll just use case 1, you can see the original above.)
Code:
case 1:
cout<<"\nFirst number: ";
cin>> inputadd1;
cout<<"\nAdd what to it?: ";
cin>> inputadd2;
function = (+);
addinganswer = (inputadd1 "<< function <<" inputadd2);
cout<<"--------------------------------------\n";
cout<<""<< inputadd1 <<" + "<< inputadd2 <<" = "<< addinganswer <<"\n";
cout<<"--------------------------------------\n\n";
cin.get();
break;
So I bolded the part that I'm really trying to get at. Basically, if I haven't managed to get it across, I want the +,-,*,/ in the variable "function". So in this example, function will be an addition operation, and in essence, it should add inputadd1 and inputadd2 together. But as I've done this wrong, it doesn't. So, is their a specific type of variable I must use, or am I just approaching this the wrong way?
Quick Edit: As I mentioned above, I have been reading the tutorials, and I just may have missed it. But I did do a quick search for it, and didn't stumble across anything. So thanks for any help. :]