Thread: CLA

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    CLA

    Hi again, My program works but once i include if and else statesments i get programs. Basically I have a main (Image.c) that passes a picture based on CLA'S to another file called (Image_lib.c) which has the function to edit the image. Heres the main (Image.c) which i think the problem lies:

    Code:
    #include <stdio.h>
    #include <ep100lib.h>
    
    int main(int argc, char **argv)
    {
    ep100_lib_init();
    
    FILE * output = stdout;
    FILE * input = stdin;
    
    char *filename=NULL;
    int image_data[EP100_LIB_MAX_X][EP100_LIB_MAX_Y];
    
    filename = argv[argc-1];
    
    int count;
    
    int width;
    int height;
    ep100_lib_init();
    
    for(count=0; count<=argc-1; count++)
    	{
    	
    if (argc <= 1)
    	{
    		fprintf(output,"ERROR: Not enough arguments entered\n");
    		return(0);
    	}
    
    else if (strcmp(argv[1],"-i") == 0)
    	{		
    
    		ep100_lib_open_image(filename);
    
    		width = ep100_lib_get_image_width();
    		height = ep100_lib_get_image_height();
    	
    		ep100_lib_get_data(image_data);
    		//ep100_lib_print_data(image_data);
    		
    		invert_image(image_data, width, height);
    
    		ep100_lib_set_data(image_data);
    		
    		sleep(2);
    		
    		ep100_lib_display_image();
    		
    		ep100_lib_close();
    		
    		return(0);
    	}
    }
    }
    Now when i remove the if...else statesments it works fine. But once i add them it doesnt.

    What is wrong?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    What happens when you include the IF-ELSE statment does the program compiles, do u get an error messages and warnings. Mean time include the string.h file.

    And check whether it entere the else part of the code by adding some printf in your if else.

    ssharish

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    270
    when i get rid of the if and else it works

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Step 1 is learn some indentation skills.
    Consistent layout of the control flow will tell you where you've messed up.

    Code:
    #include <stdio.h>
    #include <ep100lib.h>
    
    int main(int argc, char **argv)
    {
        ep100_lib_init();
    
        FILE *output = stdout;
        FILE *input = stdin;
        char *filename = NULL;
        int image_data[EP100_LIB_MAX_X][EP100_LIB_MAX_Y];
    
        filename = argv[argc - 1];
    
        int count;
        int width;
        int height;
    
        ep100_lib_init();
    
        for (count = 0; count <= argc - 1; count++)
        {
            if (argc <= 1)
            {
                fprintf(output, "ERROR: Not enough arguments entered\n");
                return (0);
            }
            else if (strcmp(argv[1], "-i") == 0)
            {
                ep100_lib_open_image(filename);
    
                width = ep100_lib_get_image_width();
                height = ep100_lib_get_image_height();
    
                ep100_lib_get_data(image_data);
    
                //ep100_lib_print_data(image_data);
    
                invert_image(image_data, width, height);
    
                ep100_lib_set_data(image_data);
    
                sleep(2);
    
                ep100_lib_display_image();
                ep100_lib_close();
    
                return (0);
            }
        }
    }
    Other questions are:
    1. Why are you calling ep100_lib_init(); twice?
    2. Why are you mixing declarations and statements (which isn't valid C)
    3. if (argc <= 1) is going to be true/false tested just once outside the loop just as much as being tested every time inside the loop.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 20
    Last Post: 01-26-2009, 01:33 AM
  2. Unix programming & CLA
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 03-06-2008, 10:22 AM