Hello, I'm getting the following error with this code, also i would like to know how to add the path and filespec together with a and be able to split the 2 so it would be 3 args not 4.

ERROR:

error C2664: 'std::vector<_Ty>::vector(const std::allocator<_Ty> &)' : cannot convert parameter 1 from 'char *' to 'const std::allocator<_Ty> &'

This is my current code:

#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace boost::filesystem;

#include <boost/regex.hpp>

void show_files( const path & directory, bool recurse_into_subdirs = false )
{
  if( exists( directory ) )
  {
    directory_iterator end ;
    for( directory_iterator iter(directory) ; iter != end ; ++iter )
      if ( is_directory( *iter ) )
      {
        cout << "\nd " << iter->path() ;
        if( recurse_into_subdirs ) show_files(*iter) ;
      }
      else 
        cout << "\n- " << iter->path() ;
  }
}

int main(int argc, char *argv[])
{
	if (argc != 4)
	{
		cout << "Usage: program.exe [switches] [path] [filespec]" << endl;
	}
	else
	{
		vector<string> s_switches(argv[1]);
		string s_path(argv[2]);
		string s_filespec(argv[3]);

		cout << s_switches[1] << endl;

		bool aSwitch = false;
		bool rSwitch = false;
		bool RSwitch = false;
		bool lSwitch = false;

		//set switches
		if (s_switches[1] == "-a")
		{
			bool aSwitch = true;
		}
		if (s_switches[1] == "-r")
		{
			bool rSwitch = true;
		}
		if (s_switches[1] == "-R")
		{
			bool RSwitch = true;
		}
		if (s_switches[1] == "-l")
		{
			bool lSwitch = true;
		}

		//boost::regex expr(s_filespec);
		//fs::path currentPath(s_path);
		//cout << currentPath << endl;
		//fs::basic_directory_iterator(s_path);

		show_files(s_path) ;

	}	

	cin.ignore();
	return 0;
}

Instead of:

vector<string> s_switches(argv[1]);

try:

vector<string> s_switches(string(argv[1]));

ok that works, but now i get errors for things like, s_switches[1]

error C2109: subscript requires array or pointer type

I have no idea how to use that library, this is for a school project and we haven't used that library before. I tried to use getline() but can't get that to work either. there are a max of 4 switches (-a, -r, -R, -l)

Any ideas?

I would encourage you to look into it for the future but I can understand you probably just want to get this done on schedule.

One solution would be to not assume that there are 4 arguments, because you have 4 optional switches and 2 other arguments, in addition to the program name.

Since the last two arguments are mandatory, you access them as the last argument in the argv and the second to last argument in argv . The switch arguments are then everything from argv[1] to argv[argc-3]

SO you saying to have more then 4 args? my prof only wants 3, the switches should be -r-R-l for example and the next arg should be C:\program files (x86)\.... (then a character that would separate the 2, i chose |) followed by the regular expression, eg. (B|b)ill.*

All i need to know for now is how to separate them, once i can get that done i can work on using what has been specified.

I see, they made it a little easier for you.

Just treat the switches as a string like you do with the other arguments. Forget the vector.

You then just parse the string of switches. One solution would be to use a for loop and the substr method of string .

Your loop could look like this:

for(int x=0; x < s_switches.size(); x+=2)

I'll leave the body of the loop for you since I probably shouldn't do your hw. But what you would want to do is access the element at x and x+1 to get a single switch (e.g. -r). You can get parts of the string with substr. A reference is here: http://www.cplusplus.com/reference/string/string/

Also note that if the size of the switch isn't divisible by 2, it's not valid. You might want to check for this before the loop.

This article has been dead for over six months. Start a new discussion instead.