command line parser

This is a discussion on command line parser within the C++ Programming forums, part of the General Programming Boards category; hello all, new semester, new problems...ahhh I love it! anyhow, I'm supposed to write a simple command line argument parser, ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572

    command line parser

    hello all, new semester, new problems...ahhh I love it!

    anyhow, I'm supposed to write a simple command line argument parser, and for some odd reason it does not want to work. The requirments are the following:

    if the command line is:
    Code:
    parse 123ds24
    then the prog should display
    Code:
    123
    24
    if the command line is
    Code:
    parse 123ddd36 356m7
    then
    Code:
    123 
    36
    356
    7
    I think you know what I mean...anyhow, below is my code...the problem arises when there is a letter in the string, it just skipps it and goes to the next argument, any suggestions?
    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	int intVal = 0;
    	char c;
    
    	if ( argc == 1 ) {
    		cout << "\nThere is nothing to parse!\n";
    		cout << endl;
    	} else {
    		
    		for ( int i = 1; i < argc; i++ ) {
    			c = argv[i][i-1];
    			
    			if ( isalpha(c) ) continue;
    			else{
    				intVal =  atoi( argv[i] );
    				cout << intVal << endl;
    			}
    		}
    	}
    
    	return 0;
    }
    thanks,

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You're going to need two loops, one which increments each time you check a char in an argument, and one which increments each time you finish checking a whole argument...to make it simple you wont even need variables, just check whether the char in argv[arg][chr] isnt a letter (with isalpha() )

    Edit:

    To get you started:
    Code:
    #include <iostream>
    #include <cctype>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
       for (int arg=1;arg<argc;arg++)
       {
               for (int chr=0;argv[arg][chr]!='\0';chr++)
              {
                 ...
              }
              ...
        }
    
    
    
    	return 0;
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    thanks for the reply JaWiB, but I have figured it out before...this is what summer does to me...going from 30 hours of coding a week to maybe 2! ahh...but oh well time to get back into the nic of things. Anyways, below is the solution for anyone interested.

    Yes I did need a double loop to go through each element of the array, and atoi(c) only displays digits unil none digit so
    Code:
    int x = atoi( "324dd345");
    would result in x being 324 only. This is just for completeness. Yes this was just the warm up to this semester...I will visit in a day or two about a doubly linked grid world prog that has some runtime requirements...what fun!

    solution:
    Code:
    #include <iostream>
    #include <cctype>
    #include <cstring>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	int a=0; //counter for amount of letters
    	char c;
    
    	if ( argc == 1 ) {	//check for amount of arguments
    		cout << "\nThere is nothing to parse!\n";
    		cout << endl;
    	} else {
    		for ( int i = 1; i < argc; i++ ) {
    			for ( int j = 0; j < strlen(argv[i]); j++ ){
    				c = argv[i][j];	//step through each string and
    				if ( isdigit(c) ) { //check if value is a digit
    					if ( a > 0 ) {
    						cout << endl;
    						a=0;
    					}
    					cout << argv[i][j];// << endl;
    				}else { a++; } //if value is not digit increment a
    			}
    			cout << endl;
    		}
    	}
    
    	return 0;
    }
    this could be probably made much nicer, esspecially the a++ thing, but oh well...I still have some days before the due date...

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Registered User newbie_grg's Avatar
    Join Date
    Jul 2002
    Posts
    77

    strtok

    axon,
    I suggest you to use strtok(). Read it about it here. There are also other docs which may matter to you.
    http://<br /> <a href="http://www.g...html</a><br />

    cheers
    "If knowledge can create problems, it is not through ignorance that we can solve them. "
    -Isaac Asimov(1920-1992)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with Parser Generators - Functions
    By jason_m in forum C Programming
    Replies: 1
    Last Post: 09-09-2008, 10:38 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Parser Help
    By Barnzey in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2005, 01:10 PM
  4. Problem with a file parser.
    By Hulag in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2005, 09:54 AM
  5. Parser - need help!
    By thelma in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 09:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21