Invalid Operands to Binary Error

This is a discussion on Invalid Operands to Binary Error within the C Programming forums, part of the General Programming Boards category; Okay, so I'm still learning about C programming but I think I know what I've done wrong but I can't ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    10

    Invalid Operands to Binary Error

    Okay, so I'm still learning about C programming but I think I know what I've done wrong but I can't figure out how to fix it.

    In my code, I'm trying to compare elements of an array (which is holding a string taken from a file via command line) in a for loop.
    Code:
    for( j = 0; inptr[ j ] != '\0'; j++ ) {
            --j;
    }
    
    if( inptr[j] == '\n' ) {
           --j;
    }
    However, I keep getting two invalid operands to binary errors. I'm pretty certain that you can compare elements of an array like this but I can't figure out how.

    Thanks for your help!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Post the smallest and simplest program that demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    My god...

    The simplest suggestion showed me the light. I was using the wrong variable names.

    In my code inptr is a pointer, not an array. *doh*

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by MikeyVeeDubb
    In my code inptr is a pointer, not an array.
    hmm... but in this context that does not matter, other than possible runtime errors if the pointer does not point to the first element of the desired array.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    Well, I'm not sure why changing it fixed it but now my second function is giving me a problem. I keep running into an "syntax error at end of input" error and I'm not sure why. Here's the function.

    Code:
    void getLine ( char *src, FILE *fswitch )
    {
    	while( fgets( src, 81, fswitch ) != NULL ) {
    		if ( src[0] == '\n' ) {
                		return ( getLine( src, fswitch ) );
          		}
    } // getLine
    Any ideas?

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Refer to post #2

    And what exactly are you trying to do?

    Also, you:

    - should be passing the array size to the function. Otherwise, it's basically unusable for the general case.
    - should not be calling getLine recursively. There's no need for it.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    My assignment is to:
    Write a C program named rev to open a text file and display its contents line by
    line so that the text of each line is reversed.
    Here's the code I have currently (it's gonna look rough, please remember that I'm still learning):
    Code:
    /* Libraries */
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    
    int getLine ( char *src, FILE *fswitch );
    
    int main ( int argc, char * argv[ ] ) 
    {
    
    	char instring [ 120 ];		// holds string input from files
    	char numberLines [ 10 ];	// used to number lines (option)
    	char fileName [ 50 ];		// used to hold the name of the file
    	FILE * inptr;			// FILE ptr as defined in stdio.h
    	int i, j;			// counters for loops
    	int lineNum = 1;		// lineNum counts the number of lines
    	int end = 0;			// end quits the loop when file is scanned
    
    	if( argc == 1 ) {
    		printf( "Please enter a filename. Syntax: ./rev \"filename\" \"number of lines\"\n" );
    		return 0;
        	}
        	else if( argc == 2 )
        	{
    		strcpy( fileName, argv[1] );
        	}
        	else if ( argc == 3 )
        	{
    		strcpy( numberLines, argv[ 1 ] );        
    		strcpy( fileName, argv[ 2 ] );
                    if ( strcmp( numberLines, "-n" ) != 0 ) {
    			return 0;
    		}
        	}
        	else
        	{ 
    		printf( "Too many arguments. Syntax: ./rev \"filename\" \"number of lines\"\n" );
          		return 0;
        	}
    
        	if ( ( inptr = fopen( fileName, "r" ) ) == NULL ) {
     		printf( "File could not be opened.\n" );
              	return 0;
        	}
    
    	while( end != 1 ) {
            	end = getLine ( instring, inptr );
            	if ( feof( inptr ) != 0 ) {
            		end = 1;
    		}
    
    		if( strcmp( numberLines, "-n" ) == 0 ) {
                    	printf("%d ", lineNum );
    		}
    
    		for( j = 0; instring[ j ] != '\0'; j++ ) {
            		--j;
    		}
    
            	if( instring[j] == '\n' ) {
            		--j;
    		}
    
    		while( j >= 0 ) {
    			printf( "%c", inptr[j] );
            		--j;
    		}
    
            	lineNum = lineNum + 1;
            	printf( "\n" );
    
            	for( i = 0; i < 119; ) {
    			instring[i++] = '\0';
    		}
    
    	fclose( inptr );
    	return 0;
    }
    
    
    int getLine ( char *src, FILE *fswitch )
    {
    	if( fgets( src, 81, fswitch ) != NULL ) {
    		
       		if ( src[0] == '\n' )
                		return ( getLine( src, fswitch ) );
             	else
             	return 0;
         	}
         	else
         	return 1;
    		
    } // getLine
    I've tried to get rid of my Syntax error at end of input error several different ways but it doesn't go away. I don't know what's causing it.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    For extra credit we were asked to code in a "-n" command line argument that would number the lines (from 1 - number of lines). I'm using an array so I can use strcmp to see if the command line argument is in fact "-n"

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm not really sure why you have getLine:
    1. recursive
    2. read 81 characters into 120 character buffer

    What syntax error are you getting?


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

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I didn't read all of your code, but:

    Code:
    for( j = 0; instring[ j ] != '\0'; j++ ) {
    	--j;
    }
    Maybe you should decide which direction to go and stick with it? You also have a buffer underflow there.

    Also, *never* use strcpy or *any* other functions that read/write to memory oblivious to the boundaries of the buffers that they process. On a side note, if you do decide to use strncpy, just keep in mind that it doesn't guarantee the null terminator, so you may need to do that yourself.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    I'm looking at it now too and I'm not really sure if it's necessary. I wrote it in the other day and it doesn't really do anything.

    I'm getting a "error: syntax error at end of input" error.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    On a hunch, change // getLine to /* getLine */. The // comments are part of C99 (and C++).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    :-\ Still no luck. We're using the gcc compiler so I think either comment type will work but I changed all of my comments just in case, to no avail. Here's what my code looks like now:


    Code:
    /* Libraries */
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    
    int main ( int argc, char * argv[ ] ) 
    {
    
    	char instring [ 120 ];		/* holds string input from files 	   */
    	char numberLines [ 10 ];	/* used to number lines (option) 	   */
    	char fileName [ 50 ];		/* used to hold the name of the file 	   */	
    	FILE * inptr;			/* FILE ptr as defined in stdio.h	   */
    	int i, j;			/* counters for loops			   */
    	int lineNum = 1;		/* lineNum counts the number of lines	   */
    	int end = 0;			/* end quits the loop when file is scanned */	
    	
    	/* The following checks the command line arguments */
    	/* to make sure they are valid and check for the   */
    	/* 		"-n" argument.			   */
    			
    	if( argc == 1 ) {
    		printf( "Please enter a filename. Syntax: ./rev \"filename\" \"number of lines\"\n" );
    		return 0;
        	}
        	else if( argc == 2 )
        	{
    		strcpy( fileName, argv[1] );
        	}
        	else if ( argc == 3 )
        	{
    		strcpy( fileName, argv[ 1 ] );			/* Copy the name of the file from the cmd line	*/
    		strcpy( numberLines, argv[ 2 ] );       	/* Copy the second argument (better be "-n")	*/
                    if ( strcmp( numberLines, "-n" ) != 0 ) {	/* Use strcmp() to see if cmd arg is "-n"	*/
    			return 0;
    		}
        	}
        	else
        	{ 
    		printf( "Too many arguments. Syntax: ./rev \"filename\" \"number of lines\"\n" );
          		return 0;
        	}
    
    	/* Check to see if the file can be opened */
        	if ( ( inptr = fopen( fileName, "r" ) ) == NULL ) {
     		printf( "File could not be opened.\n" );
              	return 0;
        	}
    
    	/* This while statement reverses the lines taken from the file */
    	/* The loop will continue to scan every line in the file until */
    	/* 		there are no more lines to be read.	       */
    
    	while( end != 1 ) {
    		/* Checks for end of file */
    		if ( feof( inptr ) != 0 ) {
            		end = 1;
    		}
    
    		/* If "-n" option is in use, print lineNum */
    		if( strcmp( numberLines, "-n" ) == 0 ) {
                    	printf("%d ", lineNum );
    		}
    		
    		
    		for( j = 0; instring[ j ] != '\0'; j++ ) {
            		--j;
    		}
    
            	if( instring[j] == '\n' ) {
            		--j;
    		}
    
    		while( j >= 0 ) {
    			printf( "%c", inptr[j] );
            		--j;
    		}
    
            	lineNum = lineNum + 1; // Increase lineNum to match line number
            	printf( "\n" );
    
    		/* Resets the instring array */
            	for( i = 0; i < 119; i++ ) {
    			instring[i] = '\0';
    		}
    
    	fclose( inptr ); /* Close the file when finished   */
    	return 0;	 /* returns int to make main happy */
    } /* End main() */
    These are the exact things the compiler's telling me:
    rev.c: In function `main':
    rev.c:87: warning: int format, __FILE arg (arg 2)
    rev.c:101: error: syntax error at end of input
    I'm still confused.

  14. #14
    Registered User
    Join Date
    Sep 2009
    Posts
    10
    Correction: I just got rid of the warning but the error still exists.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You forgot a closing brace:
    Code:
    /* Libraries */
    # include <stdio.h>
    # include <string.h>
    # include <stdlib.h>
    
    int main ( int argc, char * argv[ ] ) 
    {
    
      char instring [ 120 ];		/* holds string input from files 	   */
      char numberLines [ 10 ];	/* used to number lines (option) 	   */
      char fileName [ 50 ];		/* used to hold the name of the file 	   */	
      FILE * inptr;			/* FILE ptr as defined in stdio.h	   */
      int i, j;			/* counters for loops			   */
      int lineNum = 1;		/* lineNum counts the number of lines	   */
      int end = 0;			/* end quits the loop when file is scanned */	
    	
      /* The following checks the command line arguments */
      /* to make sure they are valid and check for the   */
      /* 		"-n" argument.			   */
    			
      if( argc == 1 ) {
        printf( "Please enter a filename. Syntax: ./rev \"filename\" \"number of lines\"\n" );
      return 0;
      }
      else if( argc == 2 )
      {
        strcpy( fileName, argv[1] );
      }
      else if ( argc == 3 )
      {
        strcpy( fileName, argv[ 1 ] );			/* Copy the name of the file from the cmd line	*/
        strcpy( numberLines, argv[ 2 ] );       	/* Copy the second argument (better be "-n")	*/
        if ( strcmp( numberLines, "-n" ) != 0 ) {	/* Use strcmp() to see if cmd arg is "-n"	*/
          return 0;
        }
      }
      else
      { 
        printf( "Too many arguments. Syntax: ./rev \"filename\" \"number of lines\"\n" );
        return 0;
      }
    
      /* Check to see if the file can be opened */
      if ( ( inptr = fopen( fileName, "r" ) ) == NULL ) {
         printf( "File could not be opened.\n" );
         return 0;
      }
    
      /* This while statement reverses the lines taken from the file */
      /* The loop will continue to scan every line in the file until */
      /* 		there are no more lines to be read.	       */
    
      while( end != 1 ) {
        /* Checks for end of file */
        if ( feof( inptr ) != 0 ) {
          end = 1;
        }
        /* If "-n" option is in use, print lineNum */
        if( strcmp( numberLines, "-n" ) == 0 ) {
          printf("%d ", lineNum );
        }
        for( j = 0; instring[ j ] != '\0'; j++ ) { //Error with j
          --j;
        }
        if( instring[j] == '\n' ) {
         --j;
        }
        while( j >= 0 ) {
          printf( "%c", inptr[j] );
          --j;
        }
        lineNum = lineNum + 1; // Increase lineNum to match line number
        printf( "\n" );
        /* Resets the instring array */
        for( i = 0; i < 119; i++ ) {
    	instring[i] = '\0';
        }
        fclose( inptr ); /* Close the file when finished   */
      }
      return 0;	 /* returns int to make main happy */
    
    } /* End main() */
    Last edited by Adak; 09-23-2009 at 03:11 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

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