+ 1
Programming a calculator?
so I'm new to coding I have a few rudiments under my belt but coding a calculator illudes me
3 Respostas
+ 9
Check out my simple calculator if you want to look for some example. And I will help you if you need any help and if you get stuck
+ 1
check this out:
#include <iostream>
using namespace std;
int main()
{
int a,b;
char symb;
cin >> a;
cout << "first number:"<< a << endl;
cin >> symb;
cout << "symbol:" << symb << endl;
cin >> b;
cout << "second number:"<< b << endl;
switch (symb) {
case '+':
cout << "sum:" << a+b << endl;
break ;
case '-':
cout << "diff:" << a-b << endl;
break;
case '*':
cout << "incr:" << a*b << endl;
break;
case '/':
cout << "div:" << a/b << endl;
break;
}
return 0;
}
0
Try a command driven calculator for start. Use endless loop, inside on the beginning use cin to read a command to string. Then you can use if else if block to identify the command. Inside each if block do the operation. Like cin x cin y z=x*y cout z. If you want to evaluate a longer sequence from string, then you might want to create a simple parser with precedence, but that might be a bit difficult for start.