First of all
YES, I have the motivation and will to learn these things,
NO not just for hacking.
YES, I have been learning C++ for a few weeks now.
NO, I'm not sure if this calculator is even near to proper one.
So please help me to learn more, tell me what's wrong, not
recommended or just pure failure. I can take it.
Code:
#include "iostream.h"
#include "windows.h"
using namespace std;
int main()
{
SetConsoleTitle( "Simple Calculator" );
double num1,num2,result;
char op;
cout<<"Calculator\n\n";
cout <<"+ = addition\n- = subtraction\n/ = division\n* = multiplication\n\n";
while(cin>>num1>>op>>num2)
{
switch(op)
{
case '+': result = num1 + num2;
break;
case '-': result = num1 - num2;
break;
case '*': result = num1 * num2;
break;
case '/':
if (num2!=0)
result = num1 / num2;
else
cout << "Error #1, Dividing with zero not defined\n\n";
break;
default: cout<<"Error #2, Invalid operator: '"<<op<<"'\n\n";
continue;
}
if (num2!=0)
cout<<"Answer: "<<result<<"\n\n";
}
return 0;
}
Example:
Code:
Calculator
+ = addition
- = subtraction
/ = division
* = multiplication
2+5
Answer: 7
2*298
Answer: 596
1337/0
Error #1, Dividing with Zero not defined!
524%5
Error #2, Invalid operator: '%'!