Thread: Command Line Arguments...again

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

    Command Line Arguments...again

    ok, I'm trying to get this to work but somehow can't figure it out.
    I want to distinguis between flags in a command line. These are the flags that the prog can use
    [-norand] [-ncols] [-nrows] [-n N]

    so basically a command could look like:
    user~> main -norand -ncols 5 -nrows 6
    or
    user~> main -nrows 3 -n 5

    the program goes into an infite loop on every flag except -norand...on norand it just hangs.....here is the code.
    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    #include "grid.h"
    
    void get_arguments(int arg, char *argv[], bool &random, 
    				   int &r, int &c, int &n)
    {
    	if(arg == 1){
    		return;
    	}else{
    		while( (arg -= 2) > 0 ){
    			switch (argv[ 1][ 2]) {  /* handle options */
    		        case 'o':
    					random = true;
    					cout << "rand" << endl;
    					break;
    				case 'c':
    					c = atoi( argv[ 2]);
    					cout << "cols" << endl;
    					break;
    				case 'r':
    					r = atoi(argv[ 2]);
    					cout << "rows" << endl;
    					break;
    				case ' ':
    					n = atoi(argv[ 2]);
    					cout << "N " << n << endl;
    					break;
    			}
    			arg += 2;
    		}
    	}
    }
    
    int main(int argc, char *argv[])
    {
    	bool random = true;
    	int rows = 0;
    	int cols = 0;
    	int n = 0;
    	get_arguments(argc, argv, random, rows, cols, n);
    		
    	return 0;
    }
    yhanks, 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
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    switch (argv[ 1][ 2]) {
    The problem is that every time through your loop you use the exact same point of reference for your switch. You should just do something simple like use a for loop to run through the list.
    Code:
    for( x = 1; x < argc; x++)
    {
        cout << argv[1] << endl;
    }
    This will simply print the command line args for you. With only minor modifications you can use the exact same loop to check for all your needed cases.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    I'll add that you get a infinite loop because each time you loop, you both decrement then increment your variable by 2. So it is like you hadn't done anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM