Command-line parameter problem

This is a discussion on Command-line parameter problem within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> int main (void) { int Counter; for(Counter = 0; Counter < 5; Counter++) { printf("Loop process %d\n", ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,532
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    472
    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, 05:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10: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

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