0

I'm writing a program that makes a word ladder. A word latter is a path of getting from word A to word B. For example

head -> heal -> teal -> tell -> tall -> tail

In my main file, I'm supposed to pass in these command line arguments: file name of the .txt file that has the dictionary, a start word, a end word, and the number of steps allowed to get from a to b. I assumed you would just put the parameters on main, but its not working. I have

int main(int argc, char* argv[], string start, string end, int steps)
{
     return 0;
}

When I try to compile, it gives me two warnings. The first one wants my strings to be char**. The second one says it takes only zero to two arguments.

How would I pass in a file name, two strings, and an integer from the command line?

mhemmy
  • 297
  • 1
  • 4
  • 9
  • 4
    Have you tried [googling](https://www.google.com/search?q=c%2B%2B+command+line+arguments&oq=c%2B%2B+command+line+arguments&aqs=chrome..69i57j69i60l5.3199j0j7&sourceid=chrome&ie=UTF-8) to figure out how command line arguments work in c++? – scohe001 Sep 22 '17 at 17:52
  • related/dupe: https://stackoverflow.com/questions/6361606/save-argv-to-vector-or-string – NathanOliver Sep 22 '17 at 17:53

1 Answers1

1

The first parameter to main is the number of parameters on the command line.

The second parameter is an array of pointers to the parameter texts (C-Style character arrays).

If you execute your program as follows:

homework1.exe dictionary.txt alpha eclipse 25

The argument count variable will contain 5 (includes the program name). Here is how the array of pointers will look like:

[0] "homework1.exe"  
[1] "dictionary.txt"
[2] "alpha"
[3] "eclipse"
[4] "25"
[5] nullptr

Note that the step count will be in text format and you will need to convert from text format to internal format. One method of conversion is to use std::istringstream.

Steve
  • 6,334
  • 4
  • 39
  • 67
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154