Thread: Need of help with a few errors

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    15

    Need of help with a few errors

    Hey, been working on some code and just keep receiving errors, been at this for hours so pretty sure I am just not thinking straight and it is very simple repair.
    Errors:
    1>c:\users\ryan\documents\work\programming\program ming task 2 final\task 2\task 2\functions.c(293) : error C2065: 'flood_colour' : undeclared identifier
    1>c:\users\ryan\documents\work\programming\program ming task 2 final\task 2\task 2\functions.c(293) : error C2440: 'function' : cannot convert from 'int' to 'pixel_colour'
    1>c:\users\ryan\documents\work\programming\program ming task 2 final\task 2\task 2\functions.c(293) : warning C4024: 'FloodImage' : different types for formal and actual parameter 1
    1>c:\users\ryan\documents\work\programming\program ming task 2 final\task 2\task 2\functions.c(473) : error C2065: 'flood_colour' : undeclared identifier
    1>c:\users\ryan\documents\work\programming\program ming task 2 final\task 2\task 2\functions.c(473) : error C2440: 'function' : cannot convert from 'int' to 'pixel_colour'
    1>c:\users\ryan\documents\work\programming\program ming task 2 final\task 2\task 2\functions.c(473) : warning C4024: 'FloodImage' : different types for formal and actual parameter 1


    prototypes.h
    Code:
    typedef struct 
    {
    	int red;
    	int green;
    	int blue;
    } pixel_colour;
    
    int FloodImage(pixel_colour flood_colour);
    
    /* Fills entire array with pixels of the given colour          */
    /* always returns 0                                            */
    
    int DrawLine(int start_x, int start_y, int finish_x, int finish_y,
    			 pixel_colour line_colour);
    
    /* draws a straight line between points (x_start, y_start) and */
    /* (x_finish, y_finish) in colour line_colour                  */
    /* return codes as follows                                     */
    /*                                        Return Value         */
    /* All coordinates within image                 0              */
    /* start point outside image                    1              */
    /* finish point outside image                   2              */
    /* both points outdside image                   3              */
    /* all line points outside image                4              */
    
    int RefreshImage( );
    
    /* writes new copy of image file to disk                       */
    /* File name should be imagexxxx.ppm where xxxx is current     */
    /* generation, starting from 0000 and incremented each time    */
    /* CheckPointImage() is called                                 */
    
    int CheckpointImage( );
    
    /* writes new copy of image file to disk and increments        */
    /* generation number so that next RefreshImage() call does not */
    /* overwrite but writes a new file                             */
    /* returns new generation number                               */
    prototype2h.h
    Code:
    int Input( );					//user input function, for reading in user commands and drawing, saving and refreshing images 
    
    int Menu();							//Help function, listing commands and program information
    
    typedef struct
    {
    	char code[30];					//declares structure for 'user'
    } user;
    task.c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include "prototypes.h"
    #include "prototype2h.h"
    
    #define Layer 3
    int ***image ;
    int High, Wide;
    pixel_colour flood_colour;
    int x, y, z;
    int xs, xe, ys, ye;
    float m;
    pixel_colour line_colour;
    
    
    int main(void)
    {
    	Menu( );
    	Input( );
    }
    functions.c
    Code:
    #include <stdio.h>										//includes all header files, both from source code and our own files
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include "prototypes.h"
    #include "prototype2h.h"
    
    pixel_colour floor_colour;
    pixel_colour line_colour;
    
    #define Layer 3
    extern int x,y;
    extern int xs, xe, ys, ye;
    extern int debug=1;
    extern int ***image ;
    extern int High, Wide;
    extern float m;
    static number=-1;
    int counter, hold, z, k=1;
    
    long size;
    int *pnum = NULL;
    char filename[120], file[20], command[]={"ab"};
    user input[1000000];
    
    char opt[]  ={"SZ"};									
    char opt1[] ={"FL"};
    char opt2[] ={"FI"};
    char opt3[] ={"LI"};
    char opt4[] ={"CH"};
    char opt5[] ={"RE"};
    char opt6[] ={"EX"};
    char opt7[] ={"HP"};
    
    void swap(char *opt1, char *opt2)
    {
    	char *temp = (char *)malloc((strlen(opt1)+1)*sizeof(char));
    	strcpy(temp, opt1);
    	strcpy(opt1, opt2);
    	strcpy(opt2, temp);
    	free(temp);
    }
    
    int FloodImage(pixel_colour flood_colour)
    {
    	flood_colour.red=*(pnum);
    	flood_colour.green=*(pnum+1);
    	flood_colour.blue=*(pnum+2);
    
    	for (y = 0; y < High; y++)
    	{
    		for(x = 0; x < Wide; x++)
    		{
    			image[y][x][0]=flood_colour.red;
    			image[y][x][1]=flood_colour.green;
    			image[y][x][2]=flood_colour.blue;
    		}
    	}
    }
    int Draw_Line_pen(int xs,int xe, int ys, int ye, pixel_colour line_colour)
    {
    	xs=*(pnum);
    	xe=*(pnum+1);
    	ys=*(pnum+2);
    	ye=*(pnum+3);
    
    	line_colour.red=*(pnum+4);
    	line_colour.green=*(pnum+5);
    	line_colour.blue=*(pnum+6);
    
    	if (xs>=0 && xe<= Wide && ys>=0 && ye<=High)
    	{
    		goto drawline;
    		return(0);
    	}
    	else if (xs<0 && ys<0 && xe<=Wide && ye<=High)
    	{
    		printf("The starting point of the line is not within the image, please type in new co-ordinates for the line\n");
    			return(1);
    	}
    	else if (xe>Wide && ye>High && xs>=0 && ys>=0)
    	{
    		printf("The finishing point of the line is not within the image, please type in new co-ordinates for the line\n");
    			return(2);
    	}
    	else if((xs<0 && xe>Wide && ys>=0 && ye<=High) || (xs>=0 && xe<=Wide && ys<0 && ye>High))
    	{
    		printf("A line co-ordinate is not within the image, please type in new co-ordinates for the line\n");
    			return(3);
    	}
    	else if(xs<0 && ys<0 && xe>Wide && ye>High)
    	{
    		printf("None of the line co-ordinates are within the image, pleae type in new co-ordinates for the line\n");
    			return(4);
    	}
    	else
    	{
    		printf("Incorrect line point try again");
    			return(5);
    	}
    
    drawline:
    
    
    
    	if (abs(xe - xs) > abs(ye - ys))		//check gradient less than one
    	{
    		m = (ye - ys)/(float)(xe - xs);		//Calculate gradient
    		if (xs > xe) 
    		{
    		m = (xe - xs)/(float)(ye - ys);		//Calculate gradient
    		if (ys > ye)
    		{
    			int temp;									//swap xs and xe and ys and ye
    			temp = xs; xs = xe; xe = temp; 
    			temp = ys; ys = ye; ye = temp; 
    		}	
    		for (y = ys; y < ye; y++)
    		{
    			x = (int)(xs + (m * (y - ys)));		//changes array elements to draw line
    			image[y][x][0] = line_colour.red;
    			image[y][x][1] = line_colour.green;
    			image[y][x][2] = line_colour.blue;
    		}
    	}
    	else	{
    			int temp;									//swap xs and xe and ys and ye, similar to swap function above
    			temp = xs; xs = xe; xe = temp; 
    			temp = ys; ys = ye; ye = temp; 
    		}	
    		for (x = xs; x < xs; x++)
    		{
    			y = (int)(ys + (m * (x - xs)));
    			image[y][x][0] = line_colour.red;			//changes array elements to draw line
    			image[y][x][1] = line_colour.green;
    			image[y][x][2] = line_colour.blue;
    		}											// gradient greater than one if here
    	
    	}
    }
    
    int RefreshImage( )
    {
    	FILE *pfile=NULL;
    	sprintf(filename,"Image%04d.ppm",number);
    	pfile=fopen(filename,"w");
    	fprintf(pfile,"P3\n #Image%i.ppm\n%d %d\n255\n", number, Wide, High);
    
    drawline:
    	for (y=0 ; y<High; y++) 
    	{
    		for (x = 0; x<Wide; x++) 
    		{
    		  fprintf(pfile,"%4i ",image[y][x][0]);			//prints line array information to the .ppm file						
    	      fprintf(pfile,"%4i ",image[y][x][1]);  
    	      fprintf(pfile,"%4i ",image[y][x][2]);  
    		}
    	}
    	fclose(pfile);
    }
    int Menu( )
    {
    	printf("\n****************************************************");
    	printf("\n***************COMMAND LIST*************************");
    	printf("\n*       Ryan Segal and Luke Coomber                *");
    	printf("\n*                                                  *");
    	printf("\n*  A list of commands are shown below:             *");
    	printf("\n*                                                  *");
    	printf("\n*  SZ x y - Sets the height and width of the image.*");
    	printf("\n*  FL r g b - Fills background with chosen colour. *");
    	printf("\n*  FI filename - Reads commands from file in dir.  *");
    	printf("\n*  LI xs xe ys ye r g b - Draws a line from (xs,ys)*");
    	printf("\n*                         to (xe, ye) in the       *");
    	printf("\n*                         specified colour.        *");
    	printf("\n*  If error occurs, use the below troubleshooter:  *");
    	printf("\n*			0: Line is working.                      *");
    	printf("\n*			1: Starting point is invalid.            *");
    	printf("\n*			2: Ending point is invalid.              *");
    	printf("\n*			3: Both start and end points are invalid.*");
    	printf("\n*			4: All points of the line are invalid.   *");
    	printf("\n*  CH - Saves a copy whilst writting all commands  *");
    	printf("\n*       to a new image.                            *");
    	printf("\n*  RE - Saves a copy whilst keeping current image  *");
    	printf("\n*       open for further editing.                  *");
    	printf("\n*  HP - Displays the current menu again.           *");
    	printf("\n*  EX - Closes the program                         *");
    	printf("\n****************************************************");
    	printf("\n****************************************************");
    }
    	
    int CheckpointImage( )
    {
    	number=number+1;
    	RefreshImage( );
    }
    
    int getfile( )
    {
    	FILE *pfile;
    	printf("Open the file: %s\n", file);
    	pfile=fopen(file,"rt");
    
    	*(pnum) = 0;										//predefines required pointers
    	*(pnum+1) = 0;
    	*(pnum+2) = 0;
    	*(pnum+3) = 0;
    	*(pnum+4) = 0;
    	*(pnum+5) = 0;
    	*(pnum+6) = 0;
    
    	if (pfile)
    	{
    		while(!feof(pfile))
    		{
    			fgets(input[z].code, 30, (pfile));
    			z++;
    		}
    		hold = z;
    	}
    	else
    	{
    		printf("Error");								
    	}													
    	z = 0;
    
    line:
    	counter = 0;
    	z =0;
    
    	for(z=0;z<hold;z++)
    	{	
    		if(strncmp(input[z].code, opt, 2) == 0)		
    		{												
    			sscanf(input[z].code,"%s %d %d", command, &Wide, &High);
    
    			image = (int ***)malloc(sizeof(int **) * High);   
    
    			for (y=0; y<High; y++) 
    			{ 
    				image[y] = (int **)malloc(sizeof(int *) * Wide); 
      
    				for (x=0; x<Wide;x++) 
    				image[y][x] = (int *)malloc(sizeof(int) * Layer); 
    			} 
    		}
    		else if(strncmp(input[z].code, opt3, 2) == 0) 
    		{
    rerun:
    				if(strncmp(input[z-1].code, opt3, 2) == 0)			
    				{
    					goto jump;
    				}
    				else if(strncmp(input[z+counter].code, opt1, 2) == 0)	
    				{
    					swap(input[z].code, input[z+counter].code);
    						if(strncmp(input[z+1].code, opt3, 2) == 0)
    						{
    							do
    							{
    								swap(input[z+k].code, input[z+counter].code);
    								k++;
    							}
    							while((z+k)<(z+counter));
    								goto line;
    						}
    						else 
    						{
    							goto jump;
    						}
    				}
    				else
    				{
    					if(strncmp(input[z+counter].code, opt4, 2) == 0)
    					{
    						counter = 0;
    						goto jump;
    					}
    					else 
    					{
    						counter++;
    						goto rerun;
    					}
    				}
    
    jump:	
    			sscanf(input[z].code,"%s %d %d %d %d %d %d %d", command, pnum, (pnum+1), (pnum+2), (pnum+3), (pnum+4), (pnum+5), (pnum+6));
    			Draw_Line_pen(xs, ys, xe, ye, line_colour);
    
    		}
    		else if(strncmp(input[z].code, opt1, 2) == 0)				//Compares to and runs flood image function
    		{
    			sscanf(input[z].code,"%s %d %d %d",command, pnum, (pnum+1), (pnum+2));
    			FloodImage(flood_colour);
    		}
    		else if(strncmp(input[z].code, opt2, 2) == 0)
    		{	
    call:																//Compares to and runs call_file function
    			sscanf(input[z].code,"%s",command);
    			getfile( );
    		}
    		else if(strncmp(input[z].code, opt5, 2) == 0)
    		{
    			sscanf(input[z].code,"%s",command);						//Compares to and runs Rrefresh image function
    			RefreshImage( );
    		}
    		else if (strcmp(command, opt7) == 0)
    		{
    			Menu( );												//Compares to and runs help function
    		}
    		else if(strncmp(input[z].code, opt4, 2) == 0)				//Compares to and runs checkpoint image function
    		{
    			sscanf(input[z].code,"%s",command);
    			CheckpointImage( );	
    			k = 1;
    		}
    
    	}
    line1:
    	fclose(pfile);													//closes file
    }
    
    
    int Input( )
    {	
    	pnum=(int*)malloc(7*sizeof(int));								//reserves memory for pnum pointers
    	printf("Create an image using commands followed found in the help command\n");
    
    	z = 1;
    	*(pnum) = 0;													//declares pointers
    	*(pnum+1) = 0;
    	*(pnum+2) = 0;
    	*(pnum+3) = 0;
    	*(pnum+4) = 0;
    	*(pnum+5) = 0;
    	*(pnum+6) = 0;
    	High = 128;
    	Wide = 256;
    
    
    rturn:
    	z=0;
    
    
    	do
    	{
    		scanf("%s",command);
    
    		if(strcmp(command,opt) == 0)
    		{
    			scanf("%d %d", &Wide, &High);
    
    			image = (int ***)malloc(sizeof(int **) * High);   
    
    			for (y=0; y<High; y++) 
    			{ 
    				image[y] = (int **)malloc(sizeof(int *) * Wide); 
      
    				for (x=0; x<Wide;x++) 
    				image[y][x] = (int *)malloc(sizeof(int) * Layer); 
    			} 
    		}
    		else if(strcmp(command, opt3) == 0)
    		{
    			scanf("%d%d%d%d%d%d%d",pnum, (pnum+1), (pnum+2), (pnum+3), (pnum+4), (pnum+5), (pnum+6));
    			sprintf(input[z].code,"%s %d %d %d %d %d %d %d",command, *(pnum), *(pnum+1), *(pnum+2), *(pnum+3), *(pnum+4), *(pnum+5), *(pnum+6));
    			if(!(*(pnum)>=0 && *(pnum+2)<=Wide && *(pnum+1)>=0 && *(pnum+3)<=High))
    			{
    				z = z-1;
    				DrawLine(xs, ys, xe, ye, line_colour);
    			}
    		}
    		else if (strcmp(command,opt2) == 0)
    		{
    			scanf("%s",file);
    			sprintf(input[z].code,"%s",command);
    			goto call;
    		}
    		else if (strcmp(command , opt1) == 0)
    		{
    			scanf("%d%d%d",pnum, (pnum+1), (pnum+2));
    			sprintf(input[z].code,"%s %d %d %d",command, *(pnum), *(pnum+1), *(pnum+2));
    			if(*(pnum)> 255 || *(pnum+1)>255 || *(pnum+2)> 255 || *(pnum)< 0 || *(pnum+1)<0 || *(pnum+2)<0)
    			{
    				printf("The colour component is out of range. Try again\n");
    				z=z-1;
    			}
    		}	
    		else if (strcmp(command , opt5) == 0)
    		{
    			sprintf(input[z].code,"%s",command);
    			printf("[Image has been drawn]\n");
    			printf("[All points within image.]\n");
    			goto line;
    		}
    		else if (strcmp(command , opt4) == 0)
    		{
    			sprintf(input[z].code,"%s",command);
    			printf("[Image has been drawn]\n");
    			printf("[All points within image.]\n");
    			goto line;
    		}
    		else if (strcmp(command , opt6) == 0)
    		{
    			sprintf(input[z].code,"%s",command);
    			exit(1);
    			return 0;
    		}
    		else if (strcmp(command, opt7) == 0)
    		{
    			Menu( );
    		}
    		else
    		{
    			z=z-1;
    			printf("Unknown command. Please Try again, use help command if needed\n");
    		}
    		z++;
    	}
    	while(z < 9000);
    
    line:
    	counter = 0;
    	z =0;
    
    	for(z=0;z<9000;z++)
    	{	
    		if(strncmp(input[z].code, opt3, 2) == 0)
    		{
    rerun:
    				if(strncmp(input[z-1].code, opt3, 2) == 0)
    				{
    					goto jump;
    				}
    				else if(strncmp(input[z+counter].code, opt1, 2) == 0)	
    				{
    					swap(input[z].code, input[z+counter].code);
    						if(strncmp(input[z+1].code, opt3, 2) == 0)
    						{
    							do
    							{
    								swap(input[z+k].code, input[z+counter].code);
    								k++;
    							}
    							while((z+k)<(z+counter));
    								goto line;
    						}
    						else 
    						{
    							goto jump;
    						}
    				}
    				else
    				{
    					if(strncmp(input[z+counter].code, opt4, 2) == 0)
    					{
    						counter = 0;
    						goto jump;
    					}
    					else 
    					{
    						counter++;
    						goto rerun;
    					}
    				}
    jump:	
    			sscanf(input[z].code,"%s %d %d %d %d %d %d %d", command, pnum, (pnum+1), (pnum+2), (pnum+3), (pnum+4), (pnum+5), (pnum+6));
    			DrawLine(xs, ys, xe, ye, line_colour);
    
    		}
    		else if(strncmp(input[z].code, opt1, 2) == 0)
    		{
    			sscanf(input[z].code,"%s %d %d %d",command, pnum, (pnum+1), (pnum+2));
    			FloodImage(flood_colour);
    		}
    		else if(strncmp(input[z].code, opt2, 2) == 0)
    		{
    call:
    			sscanf(input[z].code,"%s",command);
    			getfile( );
    			goto rturn;
    		}
    		else if(strncmp(input[z].code, opt5, 2) == 0)
    		{
    			sscanf(input[z].code,"%s",command);
    			RefreshImage( );
    			goto rturn;
    		}
    		else if(strncmp(input[z].code, opt4, 2) == 0)
    		{
    			sscanf(input[z].code,"%s",command);
    			CheckpointImage( );	
    			k = 1;
    			goto rturn;
    		}
    	}
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    There is a reason compilers give error messages... it's because they don't understand what we're asking of them.
    What you need to do is take each error message in turn and deal with them. They all include line numbers, so it's not like you have no idea where they're coming from... find and fix... one at a time.

    If you have specific questions... post the shortest code fragment that demonstrates the problem and ask your questions...

    We're not going to debug your code for you.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    41
    Search your code for 'flood_colour', the last two occurrences of that identifier in your program are causing the problems. Hopefully that will help you see the problem.

    Hunter

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You have a lot of stuff to work on my friend. For starters, dumping 500 lines of code on us, without highlighting the lines that give an error is not the coolest thing you could do. Your general practice of writing 500+ lines of code before compiling is bad too. Write a tiny bit, compile, test, debug...wash, rinse, repeat. This makes tracking down errors much easier. Here's a list of general problems you have:
    • You declare main to return an int, but you don't return one (0 is usual for success). Read why it matters here: void main(void) - the Wrong Thing.
    • Global variables are evil. Avoid them whenever possible.
    • goto should also be avoided at all costs: Goto Considered Harmful. Use loops instead.
    • You can't initialize an extern variable. extern only tells the compiler it's defined elsewhere (another .c file).
    • You need to use include guards in your header files.
    • You need better naming all around. Header files named prototypes and prototype2h are redundant and uninformative. A label called rturn is too close to the reserved word 'return', but this will go away when you stop using goto. A file named "functions" is also pretty silly, try something like draw.c, that reflects the purpose of the code inside.
    • If you don't want any parameters, explicitly put void in the param list for prototypes and definitions. Nothing in the parens means any number and type of parameters is okay, so the compiler can't check your usage. E.g. int CheckpointImage(void);
    • For loops like the following don't do anything: for (x = xs; x < xs; x++). Your initialization doesn't satisfy the loop condition, so nothing in there executes. What are xs, xe, ys, ye? Better names here (x_start, x_end?) might make these errors less likely.
    • Don't use feof to control a loop: Cprogramming.com FAQ > Why it's bad to use feof() to control a loop.
    • You need to check the results of all your IO library calls. You use fopen, but don't check pfile == NULL, so you will get nasty problems if the file fails to open (e.g. user mistypes a file name).
    • Avoid magic numbers. Create an enum or #defines for return codes like you use in Draw_Line_pen, and give them sensible names.
    • opt, opt1, opt2, etc could be combined into an array of char *'s or a 2-d array of chars, making it easier to deal with.
    • You don't really need 2 header files, you should combine them.
    • You need to turn up the warnings on your compiler to the max. It will point out many actual and potential problems in your code. I prefer maximum warnings, and to have my compiler halt on warnings, so I can't even compile questionable code.

    That's all I got in my first glance, but it's plenty to work on. The specific errors you mentioned are due to the fact that you don't know whether it's a flood_colour or a floor_colour. This is partially due to your use of global variables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors!
    By maxorator in forum Windows Programming
    Replies: 3
    Last Post: 12-15-2005, 08:59 PM
  2. looking for errors
    By jimothygu in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2003, 08:06 AM
  3. errors.. errrors.. more errors
    By Klinerr1 in forum C++ Programming
    Replies: 17
    Last Post: 07-23-2002, 08:43 PM
  4. what do these errors mean?
    By markdogg2 in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2002, 07:11 PM
  5. help with errors...
    By Gamma in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2002, 07:11 PM

Tags for this Thread