assigning a string to a char *argv[]

This is a discussion on assigning a string to a char *argv[] within the C++ Programming forums, part of the General Programming Boards category; I have a program that USED to take command line arguments and now I am trying to add a user ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    assigning a string to a char *argv[]

    I have a program that USED to take command line arguments and now I am trying to add a user menu instead.
    Code:
        int menuChoice;
        string start;
        string end;
        string fileName;
        ifstream in;
    
     switch (menuChoice) {
            case 1:
                cout<<"Enter the starting city: ";
                cin>>start;
                cout<<"\nEnter the ending city: ";
                cin>>end;
                cout<<"\nEnter the name of the file: ";
                cin>> fileName;
    
            break;
    In the old code I had this:
    Code:
    char * StartCity = argv[1];
    char * EndCity = argv[2];
    in.open(argv[3],ios::in)
    I know this:
    Code:
    char * StartCity = start //start is a string in the menu
    char * EndCity = end //end is a string in the menu
    Does not work.

    I think this does
    Code:
    file.open (fileName.c_str(), ios::in);
    The reason is throughout the program I test the argv's like:
    Code:
    bool ArgCheck = TestArgs(argv[1],cities,st);
          if(!ArgCheck) {
             throw MyException("Start city not found in map file.");
          }
    and I also have functions that use char * like:
    Code:
    void StackCities(char * StartCity, char * EndCity,...); //<---part of a function
    Can someone show me how I can assign those string names in the menu to the argv[1], argv[2] and argv[3]?
    Last edited by csharp100; 05-06-2012 at 09:34 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    You are mixing C-strings with std::strings, I recommend you stick with the std::strings. Convert the StartCity and EndCity to std::strings instead of the C-strings. If you don't want to change your other functions then use the std::string.c_str() member function when you need to pass a C-string into your function. However I suggest that you also consider changing the other functions to use std::strings as well.
    Code:
    bool ArgCheck = TestArgs(StartCity.c_str(), cities, st);
    Jim

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    You are mixing C-strings with std::strings, I recommend you stick with the std::strings. Convert the StartCity and EndCity to std::strings instead of the C-strings. If you don't want to change your other functions then use the std::string.c_str() member function when you need to pass a C-string into your function. However I suggest that you also consider changing the other functions to use std::strings as well.
    Code:
    bool ArgCheck = TestArgs(StartCity.c_str(), cities, st);
    Jim
    Can you show me an example of std::string.c_str()? I have a bunch of functions and that would really be a pain.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    Look at the code I posted, that is an example of using the std::string.c_str().

    It may be a pain to convert your functions to std::string but the pain will probably be worth the effort.


    Jim

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    Look at the code I posted, that is an example of using the std::string.c_str().

    It may be a pain to convert your functions to std::string but the pain will probably be worth the effort.


    Jim
    So anywhere I have a char * I am to replace that with the example yo ushowed me with the exception of the variable name?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    So anywhere I have a char * I am to replace that with the example
    Only if the char* is pointing to a C-string. See this link for std::string.c_str().


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need solution for assigning char string to malloc
    By killua1234 in forum C Programming
    Replies: 13
    Last Post: 04-21-2010, 01:07 AM
  2. Replies: 8
    Last Post: 12-08-2009, 01:47 AM
  3. Replies: 26
    Last Post: 11-30-2007, 02:51 AM
  4. Replies: 9
    Last Post: 11-23-2007, 11:28 AM
  5. Assigning a String object to char*
    By The Dog in forum C++ Programming
    Replies: 13
    Last Post: 07-14-2002, 04:41 PM

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