View Single Post
  #2 (permalink)  
Old December 7th, 2006, 08:51 AM
Geo121 Geo121 is offline
Friend of Wrox
 
Join Date: Jan 2006
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

Hey there. Your only problem is that you are declaring it right just forgetting to follow up.

Basically you declare char *argv in your main function

which was right but when you declared char x = argv[1] you forgot to say it was a pointer

so all you do is add an asterisk in front of argv[1] and you have it as a char =P

now as far as the switch statement . . .

it will accept characters as long as you put them in as characters

so this step depends on your compiler but the best practice is to put characters in single quotes

mainly becouse this is the most common standard and most compilers dont like chars in double quotes

so if you say 'a' instead of "a" it works

I did it myself heres the 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':
               cout << addition(a,b);
               break;
          default:
               cerr << "Undefined operation.";
     }

     return 0;
}


~ Geo

 ~ You are unique, just like everyone else
Reply With Quote