1

I did a program that took command line arguements to run it. I am now trying to do a menu drive program as part of its "improvement." I used,

int main(int argc, char * argv[])

in the original where the arguements were:

char * startCity = argv[1];
char * endCity = argv[2];
in.open(argv[3],ios::in); //<----file name went here

Here is what I did now and I know it is incorrect:

int main(int argc, char * argv[]){

int menuChoice;
string startCity;
string endCity;
string fileName;
ifstream in;

cout<<"Welcome to J.A.C. P2\n"
  "\n"
  "This program will find the shortest path\n"
  "from One city to all other cities if there\n"
  "is a connecting node, find the shortest path\n"
  "between two cities or find the shortest\n"
  "between three or more cities.\n"<<endl;

cout<<"Please make a choice of what you would like to do:\n"<<endl;

cout<<"  1------> Shortest Path between 2 cities.\n"
      "  2------> Shortest Path between 3 or more cities.\n"
      "  3------> Shortest Path from 1 city to all.\n"
      "  9------> Take your ball and go home!\n"<<endl;
cout<<"Waiting on you: "; cin>>menuChoice;

switch (menuChoice) {
    case 1:
        cout<<"Enter the starting city: ";
        cin>>StartCity;
        cout<<"\nEnter the ending city: ";
        cin>>EndCity;
        cout<<"\nEnter the name of the file: ";
        cin>> fileName;

    break;

Since all of my program is based on char * argv[] How can I convert those into strings OR how can I assign variables to the arguements in order to read them in?

I appreciate all the answers but they seem to be going in the direction I am trying to get away from. The OLD program used command line arguements. How can I do this:

string StartCity = char * argv[1];
string EndCity = char * agrv[2];
string filename = in.open(argv[3],ios::in);

That is what I am trying to do. I am sorry if I did not make myself clear.

c_sharp
  • 281
  • 1
  • 5
  • 13

3 Answers3

4

This might help.

int main (int argc, char ** argv)
{
          std::vector<std::string> params(argv, argv + argc);       
          //Now you can use the command line arguments params[0], params[1] ... 

}
bisarch
  • 1,388
  • 15
  • 31
0

to convert the command line arguments to strings:

std::vector<std::string> args(argc);
for (int i=1; i<argc; ++i)
   args[i] = argv[i];

I start at '1' because '0' is the program name:


to get them into vars, maybe:

// Make sure we're not accessing past the end of the array.
if (argc != 4) {
    std::cout << "Please enter three command line arguments" << std::endl;
    return 1;
}

string startCity = argv[1];
string endCity = argv[2];
string fileName = argv[3];      
matiu
  • 7,469
  • 4
  • 44
  • 48
0

To get a const char * from a std::string, use std::string::c_str().

fstream file;
string s = "path\\file.txt";
file.open (s.c_str(), ios::in);
chris
  • 60,560
  • 13
  • 143
  • 205
  • Need it the other way. Edited the original post. – c_sharp May 07 '12 at 01:34
  • Not sure what you mean, the only other way is like `string s = some_char_array;`, which I mentioned in the comments. – chris May 07 '12 at 01:39
  • `cin` or `getline` should work for inputting strings. Passing them to `open` requires `c_str()` for the C-style string. – chris May 07 '12 at 01:54
  • How would I do the startCity or endCity? `std::startCity::c_str()` where `string startCity;` has been declared earlier? – c_sharp May 07 '12 at 02:07
  • @user1318371, `string startCity = "some city"; const char *c_style_startCity = startCity.c_str();` – chris May 07 '12 at 02:17
  • Something doesn't seem right to me, where is the argv[1] supposed to go for startCity and argv[2] for endCity? like I had up there earlier, `string startCity = char * argv[1]` which I know is incorrect but that is what I am trying to do. Thank You for your comments and help. I have tests in the program for the argv[1] and argv[2], i.e. `bool ArgCheck = TestArgs(argv[1],cities,st); if(!ArgCheck) { throw MyException("Start city not found in map file."); }` – c_sharp May 07 '12 at 02:34
  • All you need to do to assign a `char *` to a `string` is just say `char *c = "hello"; string s = c;` You don't need any extra `char *`s in there or anything. Saying that fits the `string` constructor that takes a `char *`, so no problems. In this case `argv [x]` is a `char[]`, which works just as well. – chris May 07 '12 at 02:38
  • Will this work? `string s = "hello"; char *c = s;` That seems to be what I need. Sorry @chris, not tryin got be a pain. – c_sharp May 07 '12 at 03:45
  • You could try `char *c = s.c_str();`. There's one problem with that though. `c_str()` returns a `const char *`. If you **need** the `char *`, you can copy the result into a non-const array and use that. – chris May 07 '12 at 03:58