View Single Post
  #1 (permalink)  
Old December 7th, 2006, 05:08 AM
deuxmio deuxmio is offline
Registered User
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Invalid conversion from 'char*' to 'char'

I'm trying to write a calculator that takes command line arguments. So if someone wants to add 2 and 2 they would type:
Code:
$./prog a 2 2
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.


Reply With Quote