I am playing with command line arguments and implementing a very basic calculator which takes input before execution rather than at runtime. It works for 4 operations, but I am stuck at the power operation and after hours identified the fault. Here is the C++ code:
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
int main(int argc, char **argv) {
stringstream s(strcat(strcat(argv[1], " "), argv[3]));
int a, b;
s >> a >> b;
switch(argv[2][0]) {
case '+':
cout << a+b;
break;
case '-':
cout << a-b;
break;
case '*':
cout << a*b;
break;
case '/':
cout << a/b;
break;
case 'p':
cout << pow(a, b);
break;
default:
cerr << "Invalid operation";
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
The problem here is that earlier, I used '^' instead of 'p'. This character doesn't work at all, i.e., nothing is printed to console. I used debug prints, modified header files, typecasted a and b to double in pow() but the bug was this nasty little '^'. Albeit the code works well, I wish to know why this one character was exceptional in the cmd arguments? I am using Windows Command Line for the shell.
An image of the output with 'p': calc.exe command line output
And with '^': output with caret