Thread: Command-line parameter problem

  1. #1
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765

    Command-line parameter problem

    Code:
    #include <stdio.h> 
    
    int main (void)  
    { 
        int Counter; 
    
        for(Counter = 0; Counter < 5; Counter++) 
        { 
            printf("Loop process %d\n", Counter); 
            RunBatch(Counter); 
            printf("\n"); 
        } 
        return 0; 
    } 
    
    int RunBatch(int Counter) 
    { 
        int LoopCount = 0; 
        
        while(LoopCount < Counter) 
        { 
            LoopCount = LoopCount+1; 
            printf("This loop is on iteration %d.\n", LoopCount); 
        } 
        return 0; 
    }
    How would I change the second parameter in the above code located in the first for loop so that I may input what is currently 5, as any number from the command-line instead, hence filtering through the rest of the program and furthermore allowing control by means of one command line parameter.

    Code:
    int main ( int argc, char * argv[] )  
    {
    	if (argc == 2)
    	{
    		if(argv[1][0] == '?')
    		{
    			printf("Help stuff would go here.\n");
    		}
    		else
    		{
    			printf("%s \n", argv[1]); /* This is the line I'm trying to replace */
    		}
    	}
    	else if ( argc >= 3 )
    	{
    		printf("Invalid input:\n");
    		printf("Only one command-line parameter allowed.\n");
    	}
    	else
    	{
    		DefaultMessage();
    	}
    	return 0;
    }
    
    void DefaultMessage ( void )
    {
    	printf("CmdLine.exe:\n");
    	printf("The purpose of this program is to demonstrate command line arguments.\n");
    	printf("This program will print whatever you type on the screen.\n\n");
    	printf("Example:\n");
    	printf("C:\\>CmdLine.exe Hmmm\n");
    	printf("Hmmm\n");
    }
    I tried culturing the first program into this one by means of a function call, and passing the 2nd command-line parameter straight to my function, but it doesn't register. My loop just spins out of control. I think that's because this command-line code I'm using captures text from the line, not raw integers - which is what I need for my idea, yes?

    Any ideas?
    The world is waiting. I must leave you now.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I think you've been here far too long to have not read the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    yes, you have to convert argv[1] into an integer, like so: int i = atoi(argv[1]);

  4. #4
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Sorry Salem, I let the atoi slip by me. That's what I needed. Thanks for pointing that out nonpuz.
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  3. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM