This is how I would do it only there would be more operations and a try catch exception handler
// geo121's simple calculator!
#include <iostream.h>
int main()
{
cout << "\n\n\n\n\n\nWelcome to Geo121's Calculator\n\nThis Calculator is used to calculate various math operations\n\nWhen Prompted enter a value for the first number\n\nThen the symbol for the operation\n\nThen another number for the second value\n\nThe symbols for the operations are as followed : \n\n+ is used for addition\n\n- is used for subtraction\n\n* is used for multiplication\n\n/ is used for division\n\n\nMORE OPERATIONS TO COME\n\n";
float num2;
float num1;
char op;
// this documentation it isn't required
// char op in ASCII
// + = 43
// - = 45
// * = 42
// / = 47
float ans;
cout << "Please Enter the First Number: ";
cin >> num1;
cout << "Please Enter the Operation Symbol: ";
cin >> op;
cout << "Please Enter the Second Number: ";
cin >> num2;
switch (op)
{
case 43: // operation symbol was + in ASCII is 43
ans=num1+num2;
break;
case 45: // operation symbol was - in ASCII is 45
ans=num1-num2;
break;
case 42: // operation symbol was * in ASCII is 42
ans=num1*num2;
break;
case 47: // operation symbol was / in ASCII is 47
ans=num1/num2;
break;
default: // operation symbol was invalid
cout << "\n\n\nIncorrect Operation\n\n\n";
break;
}
cout << num1 << " " << op << " " << num2 << " = " << ans << endl;
return 0;
}
If you are new to programming in C++ I would suggest reading
learn C++ in 21 days
It actually takes less if you try and it also teaches some key computer programming concepts
such as code organization and memory management
~ Geo
~ You are unique, just like everyone else
|