I'm trying to write a calculator that takes command line arguments. So if someone wants to add 2 and 2 they would type:
argv[1] would be the desired operation, while argv[2] and argv[3] would be the numbers. This is my code so far:
Code:
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int addition(int a, int b){
int c;
c=a+b;
return (c);
}
int main(int argc, char *argv[]){
char x(argv[1]);
int a, b;
a = atoi(argv[2]);
b = atoi(argv[3]);
switch (x) {
case "a":
addition(a,b);
break;
default:
cerr << "Undefined operation.";
}
return 0;
}
Every time I try to compile this GCC gives me these errors:
Code:
In function 'int main(int, char**)':
error: invalid conversion from 'char*' to 'char'
error: case label does not reduce to an integer constant
make: *** [calcpp] Error 1
How can I use the value of argv[1] in a switch statement? My best attempts at deciphering the error messages point to that. Thanks in advance.