Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old December 7th, 2006, 05:08 AM
Registered User
 
Join Date: Dec 2006
Location: , , .
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
  #2 (permalink)  
Old December 7th, 2006, 08:51 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Fort Wayne, Indiana, USA.
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
  #3 (permalink)  
Old December 7th, 2006, 01:25 PM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot Geo! That did the trick.

Reply With Quote
  #4 (permalink)  
Old December 8th, 2006, 07:56 AM
Friend of Wrox
 
Join Date: Jan 2006
Location: Fort Wayne, Indiana, USA.
Posts: 103
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Geo121
Default

No problemo! That's what I'm here for =P

Keep up the good work!

~ Geo

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Big challenge here! How to convert char* to char^? samiswt Visual C++ 2005 1 November 30th, 2007 09:09 PM
unwanted conversion from char to ascii mor3bane C++ Programming 0 July 5th, 2007 03:07 AM
Invalid char in encoding-new asp .net webservice ajcis55 C# 1 April 12th, 2007 04:42 AM
conversion of ascii to char abhi_web C# 1 August 23rd, 2006 09:41 AM
getting the last CHAR of a string kondapally Crystal Reports 0 December 13th, 2004 03:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.