Thread: Returning zero valued variable from function doesn't work

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Returning zero valued variable from function doesn't work

    This is a console program. The user is suppose to type the name of the program at the command line, in this case a.out, followed by the tag -n. The tag -n is to be followed by an arbitrary number of arguments. If you enter two arguments like so:
    Code:
    a.out -n 1 2
    You are congratulated by the program. I you enter 1, 3, 4, 5, 6, 7, 8....etc arguments, you get an error message. There is one problem with this program. It does not work for zero arguments. Typing the following
    Code:
    a.out -n
    should also yield the error message. It does not. I have struggled with this for far too many hours. I come to you now. Do you know why this program doesn't work for zero input arguments???
    Code:
    #include <stdio.h>
    
    void main(int argc, char *argv[]) 
    {
    
    int arg_count, argVal;
    
    	for (arg_count=1 ;arg_count < argc-1; arg_count++)  
    	{
    		if (!strcmp(argv[arg_count],"-n"))
    		{
    			counterArgs(argv, arg_count, argc, &argVal);
    			if (argVal != 2)
    			{	
    				fprintf(stderr, "\n\n Error: Either you did not enter two arguments.\n");
    				fprintf(stderr, " or you lack basic programming skills.  It appears.\n");
    				fprintf(stderr,"that you have entered %d arguments.\n\n", argVal);
    				exit(0);
    			}
    			
    			fprintf(stderr,"\n\n Congratulations.  You have entered 2 = %d arguments\n\n", argVal);
    		}
    	}
    }
    
    
    
    void counterArgs(char **argv, int arg_count, int argc, int *argVal)
    {
    *argVal = 0;
    
    	while(*argVal+arg_count < argc - 1 )
    	{
    		(*argVal)++;
    	}	
    }

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    The easiest way i can suggest is to chachk the number of arguments at the begining of program. If argc==2 then print an error message and exit program.

    Also, define main as int main() (read FAQ for detaied info), and either put the prototype of your counterArgs() function before main() or move the function definition before main().

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    How about just checking that argc == 4 ? That's probably better. That would eliminate most of the code you wrote there.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >	for (arg_count=1 ;arg_count < argc-1; arg_count++)  
    >	{
    >		if (!strcmp(argv[arg_count],"-n"))
    You don't need a for-loop.
    Code:
    	arg_count = 1;
    	if (arg_count < argc)
    	{
    		if (!strcmp(argv[arg_count],"-n"))
    		{
    			counterArgs(argv, arg_count, argc, &argVal);
    			if (argVal != 2)
    			{	
    				fprintf(stderr, "\n\n Error: Either you did not enter two arguments.\n");
    				fprintf(stderr, " or you lack basic programming skills.  It appears.\n");
    				fprintf(stderr,"that you have entered %d arguments.\n\n", argVal);
    				exit(0);
    			}
    			
    			fprintf(stderr,"\n\n Congratulations.  You have entered 2 = %d arguments\n\n", argVal);
    		}
    	}
    	else
    	{
    		/* Error message here also */
    	}
    Or an easier to read version:
    Code:
    	arg_count=1;
    	if (argc > 1)
    	{
    		if (!strcmp(argv[arg_count],"-n"))
    		{
    			counterArgs(argv, arg_count, argc, &argVal);
    			if (argVal != 2)
    			{	
    				fprintf(stderr, "\n\n Error: Either you did not enter two arguments.\n");
    				fprintf(stderr, " or you lack basic programming skills.  It appears.\n");
    				fprintf(stderr,"that you have entered %d arguments.\n\n", argVal);
    				exit(0);
    			}
    			
    			fprintf(stderr,"\n\n Congratulations.  You have entered 2 = %d arguments\n\n", argVal);
    		}
    	}
    	else
    	{
    		/* Error message here also */
    	}

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't see why you need the "counterArgs" function at all. Just use argc. The first argument, if any, is the program name. The next, should be your "-n". Any after that should be your numbers. All you need is:
    Code:
    int nums = argc - 2;
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >How about just checking that argc == 4 ?
    I agree this would be the simplest and most concise.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    It was just a stupid error. I put
    Code:
    for (arg_count=1 ;arg_count < argc-1; arg_count++)
    When I should have put

    Code:
    for (arg_count=1 ;arg_count < argc; arg_count++)

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Having a for-loop there is completely pointless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Replies: 1
    Last Post: 02-03-2005, 03:33 AM