this is my first program,simple calculator but has helped me with a fair bit of understanding. Is there anyway to optimize it to make it smaller, the release size is 12kb compiled, seems kinda large for just a bunch of text play.
yes there are some apparent ones,
1. if you always ask for two numbers, may it be + or / , so why not ask it only once. like
cout << "Enter first digit: ";
cin >> firstNum;
cout << "Enter second digit: ";
cin >> secondNum;
and then pass them to your code. and let it return the value and print it once only.
like
1 2
double add(double firstAdd, secondAdd);
so in your main
something like this can compact stuffs:
it does addition, subtractio, division and multiplication(*, x, and X) and if you try to divide by zero it'll come up with a error message.
#include <iostream.h>
using namespace std;
int main(void)
{
system ("TITLE calculator");
system ("COLOR 5E");
char cChar;
double dfirstnumber;
double dsecondnumber;
char cDoagain;
do
{
system ("CLS");
cout << "Please enter the first number that you want to use " << endl;
cin >> dfirstnumber;
cout << "Please enter the operation that you would like to perform" << "(+,-,* or/)" << endl;
cin >> cChar;
cout << "Please enter the second number that you want to use" << endl;
cin >> dsecondnumber;
switch (cChar)
{
case '+':
cout << "The answer is " << dfirstnumber << " + " << dsecondnumber << " = " << (dfirstnumber + dsecondnumber) << endl;
break;
case '-':
cout << "The answer is " << dfirstnumber << " - " << dsecondnumber << " = " << (dfirstnumber - dsecondnumber) << endl;
break;
case '*':
cout << "The answer is " << dfirstnumber << " * " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
break;
case 'x':
cout << "The answer is " << dfirstnumber << " x " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
break;
case 'X':
cout << "The answer is " << dfirstnumber << " X " << dsecondnumber << " = " << (dfirstnumber * dsecondnumber) << endl;
break;
case '/':
if(dsecondnumber == 0){
cout << "This is an invalid operation" << endl;
}else{
cout << "The answer is " << dfirstnumber << " / " << dsecondnumber << " = " << (dfirstnumber / dsecondnumber) << endl;
break;
}
default:
cout << "That is an invalid operation " << endl;
break;
}
cout << "Would you like to start again? (y or n)" << endl;
cin >> cDoagain;
}while (cDoagain == 'y' || cDoagain == 'Y');