hello all, Im trying to make a command line parser and honestly am having trouble starting it. I need a function called parse which reads one line of user input and creates an array of the strings found in the input. the function should be passed the array and reference variable that is to be the length of the array. Prompt the user and read the input from within the parse function.

example if the user types " cp this that" the resulting array will have length 3 and contain strings "cpp", "this", and "that"

then i have to creat a main function that will continueously loop until the user enters quit as an input.


heres what I have so far.

/************************************************************************
	Command Line Parser
	Version 1.0
	
	This program parses a user input and places it into arrays in C++
		 
	Source File:  cmdParser.cpp

*************************************************************************/
#include <iostream>

using namespace std;

int parse( char array[], int length)
{
	char ch;	
	
	cout << " Please enter a command " << endl;
	cin.get(ch);
	while ( !cin.eof() )
		if ( isspace(ch) )
			
	
}

int main()
{
	int length;
	int array[length];

	if  ( ??? != quit)
		{
			parse(array, length);
		}
}

Your main() will need to received those arguments, first.

Your main() will need to received those arguments, first.

bare with me because Im fairly new with c++. So how do i let main recieve the arguments?

Actually, I think that thines01 misunderstood the project requirements; what you describe is what is called a 'shell' in the Unix/Linux world. You won't necessarily be getting command-line arguments for this program, but will be reading in a command line and executing it, then reading another command line and executing that, and so on until someone types in 'exit'.

The tricky part of a shell is not the parser, but the ability to execute another program without using the existing system() command. The functions for spawning an executing a process are system-dependent, thus how you would do it would be different under Windows than under, say, Linux. What sort of system is this meant to run under?

that is correct i just need to read the command line and place the strings of input into the array. this is suppose to run on spock a unix system, thanks for any help in advance

Seems like the Unix command-line can serve this as well.

int main(int argc, char** argv)

I'm not following the tips you are giving. so do I need to write it as "int main (int argc, char** argv)? Also would like more help on how to get the characters into the string form and into the array. My professor said something about making sure that there is a null character at the end of each string( /0)

Test this :

#include <iostream>
using namespace std;

int main(int argc, char * argv[]){
  for(int i = 0; i < argc; ++i){
    cout << "Argument[" << i < "] = " << argv[i] << endl;;
 }
}

You can call it like ./ExecutableName arg1 arg2 arg3

firstperson your code give me errors saying invalid operand types. I still dont understand why the arguments have to be passed to the main?

You can do this one of two ways:
1) You can have the user type their input on the Unix or DOS command line
2) You can ask the user for the input from within your parse function.

If you choose option 1, your program will need to retrieve those parameters FROM the command line. In order to get those parameters you reference argc and argv.
argc is the count of command-line parameters and argv is a pointer to an array of parameters (starting with the program name itself). The user can type:
your_prog_name cpp this that
Your program will take the contents of argc and argv and pass them to your parse function.

If you choose option 2 as Schoil-R-LEA suggests, your program ACTS as the shell on the operating system and will pass those values entered to to the parse function. You've sort-of started this method by asking the user for information. You are only asking for one character, though AND you're doing it inside the parse function.
You might consider asking for the input from within main and then passing those values as you call parse(char array[], int length).

...but you might need a string, a string array or a more complex type to hold what the user types (depending on your requirements).

I apologize for not being very clear. I need to have the input taken in the parse function at least thats my understanding. here is the assignment

write a C++ function called parse that reads one line of user input from the keyboard and creates an array of the strings found in the input. Your function should be passed the array and a reference variable that is to be assigned the length of the array. prompt the user and read the input from within the function.

for example: if the user enters "cp this that", the resulting array would have length of 3 and contain strings cp, "this", and "that".

create a main() function that has a loop that will continuously call the function until the user enters "quit". After the function has returned control to main, print the contents of the array and its length.

I was thinking that i could use main and do a simple word count by using "isspace" to find the correct length of the array and pass that. however, the assignment says that the input is read in the parse function, so now im totally lost.

You should pass an array of strings (or string pointers) to the parse function with the count being the number of parameters in the array of strings.

You should pass an array of strings (or string pointers) to the parse function with the count being the number of parameters in the array of strings.

I understand passing the array of strings and how to do that, but if the input is suppose to be given in the parse function how will i get the number of parameters in the array of strings?

You can read that requirement either way:
1) prompt the user and read the input from within the function.
2) prompt the user, (and read the input from within the function).

Logically, you should choose option 2.
Prompt the user, get the input, pass the input to the parse function.

You can read that requirement either way:
1) prompt the user and read the input from within the function.
2) prompt the user, (and read the input from within the function).

Logically, you should choose option 2.
Prompt the user, get the input, pass the input to the parse function.

could you give an example of how to pass the input to to the parse function? would this mean i am passing three things from main to the parse function now? (the array, the length, and the input?)

Well, you will first need to decide what values are going to your parse function.
Two options:

void parse1(vector<string> vec)
{
   for(int i=0; i< vec.size(); i++)
   {
      cout << vec[i] << endl;
   }
}

void parse2(int argc, char* argv[])
{
   for(int i=0; i< argc; i++)
   {
      cout << argv[i] << endl;
   }
}

Then you can put the user input in a structure that fits the function signature.

...if you're using a vector, or if you're using a char* [] with a size indicator.

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.