ok so first of all my following code its suppose to read input from user and seperate the strings into commands and arguments.For example if i input changeDir hello , the tokenizer(which i already implemented) will split it and put changeDir into tokens[0] and hello into tokens[1]. Now what im trying to do is pass the command and arguments to another function lets say my_cd(). what kind of parameters will this function have?

//... 
vector<string> tokens;
                           
  string str;
  cout <<"please enter a command" << endl;
  getline(cin,str);
  Tokenize(str, tokens);

  if(tokens[0] == "changeDir")
{
my_cd();

}

Recommended Answers

All 4 Replies

Well I suppose you'd pass it all of the arguments except for the first one and it would expect exactly one argument (the directory), and if it got more than one, it would return an error. Or just assume they gave you the right number and pass it a string.

Personally I'd have them all take (int argc, char* argv[]) as parameters, then they'll do whatever they need to do with them.

Or in your case (int argc, vector<string> argv), or just vector<string> argv since the vector holds the count.

ok I will give you some idea, I have done it before,

and I am pretty sure it can be done way easier, but if you just like to use your head and implement it no matter what then continue to read,

There is WAY better solution the, but I am very busy to list of tell what the best.

ok here we go.

I did this in an Operating System, OS, Class like about 3 years ago,

do a for loop and find every space, and every time you encounter one, get the string saved in an array of char or String, all up to u and how fixable is ur code.

I can give you a function I made 3 years ago, but I am telling you from now, it is not perfect because I got some better experience by now

if you understand this let me know,

counter=0;
			do 
			{
				c = getc(fd);

				if( c == 32 || c == 10)//if the value was a space then do what you like, You can have indexes 
				{
					//// if we have space we can copy the string with strncpy I think, not sure which one
					//// then reset counter to 0
				}
				else if ( c == '\0')
				{
					break;// whcih break u out the while loop then you have the array set up
				}
				else
				{
					counter++; // count for the strncpy that wll copy the begining of string input up to n counts so the returned value can be save into array of i
					// then increment i++
				}

			}while(1==1);

btw its just an example, never tested it, and I am sure I need to fixt it but its a heads up for u

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.