casting command line arguments

This is a discussion on casting command line arguments within the C++ Programming forums, part of the General Programming Boards category; I'm new to c++ and new to this forum, so hello everybody the problem is this: I'm using a function ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    5

    casting command line arguments

    I'm new to c++ and new to this forum, so hello everybody

    the problem is this:

    I'm using a function that has the command line arguments as its input values:

    bool CalcUnit::validate(int argc, char* argv[]) ect...

    now, I found out that I can get the arguments values like this: *argv[1] ect...

    In my function I need to have the value of *argv[1] in the string datatype, because a child function { bool isUnit( std::string input); } that needs to access this argument demands a string as the input value.

    so I've tryied to cast that argument like this: isUnit( (string)*argv[1] )


    but somehow this won't work :/

    any clues why

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
      #include <iostream>
      #include <string>
      
      using namespace std;
      
      int main(int argc, char **argv)
      {
        string s;
        
        if (argc == 2)
        {
          s = argv[1];
          cout <<s;
        }
      }
    or
    Code:
     #include <iostream>
     #include <string>
     
     using namespace std;
     
     void foo(string s)
     {
       cout <<"hello " <<s <<endl;
     }
     
     int main(int argc, char **argv)
     {
       string s;
       
       if (argc == 2)
       {
     	foo(argv[1]);
       }
     }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    thanx for the reply, but the problem still exists :/

    the error that shows up is "error C2440", saying that I cannot convert char to string.

    but I mean, the argv[1] or in my case *argv[1] returns a char array, right? and I mean, char arrays should be easely converted to strings :/

    any further suggestions?

    thanks alot

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>*argv[1]
    This is a char, not a char array. To get to the array, use argv[1], like I showed you.

    To see what I mean, try this code:
    Code:
    #include <iostream>
    
    int main(int argc, char **argv)
    {
      std::cout <<*argv[1];
    }
    /* Output:
    
    
    E:>prog aaa
    a
    See, only one "a" printed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    5
    now it worked

    thanks alot,

    and sorry for the stupid questions

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 04:27 PM
  2. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 07:46 AM
  3. Arguments and type casting
    By great_gonzo in forum C Programming
    Replies: 3
    Last Post: 05-03-2006, 06:48 PM
  4. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 11:48 AM
  5. registry, services & command line arguments.. ?
    By BrianK in forum Windows Programming
    Replies: 3
    Last Post: 03-04-2003, 02:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21