Graphics program in C

Status
Not open for further replies.

zegulas

Traceur
Joined
Oct 1, 2003
Messages
303
I am trying to make a program which will accept the names and cordinates of the points to be plotted and then plot the points, its a graphics program in C language but sometimes the name given are not displayed properly (sometimes shows NULL or weird characters).

Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <graphics.h>
#include <string.h>

int main()
{
	/* request auto detection */

	int gdriver = DETECT, gmode, errorcode, *vrt_x,*vrt_y,i,v;
	char** vrt_name;

	clrscr();


	/* initialize graphics and local variables */

	initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

	/* read result of initialization */

	errorcode = graphresult();
	if (errorcode != grOk)  	/* an error occurred */
   	{
      		printf("Graphics error: %s\n", grapherrormsg(errorcode));
		printf("Press any key to halt:");
		getch();
	        exit(1); 		/* terminate with an error code */
	}

	/*~~~~~~~~~Let the Plotting Begin!!!~~~~~~~~~~*/

	/* Asking for Vertices */

	printf(" How many vertices are there? ");
  	scanf("%d",&v);

	vrt_x =(int*)calloc(v,sizeof(int)); /* Initialized after accepting Vertices to determine */
	vrt_y =(int*)calloc(v,sizeof(int)); /* the size of memory to be alocated */
	vrt_name =(char**)calloc(v,sizeof(char));


	if(vrt_x!=NULL && vrt_y!=NULL && vrt_name!=NULL)
  	{
		for(i=0; i<v ; i++)
		{
			printf("\n Enter a name for vertice number %d ",i);
			scanf("%s",vrt_name[i]);
		}

		for(i=0; i<v ; i++)
    		{
			printf("\n Enter only the x co-ordinates for %s vertice : ",vrt_name[i]);
			scanf("%d",&vrt_x[i]);

    		}


		for(i=0 ; i<v ; i++)
    		{
			printf("\n Enter only the y co-ordinates for %s vertice: ",vrt_name[i]);
			scanf("%d",&vrt_y[i]);
    		}

		clrscr();


		/* For plotting points */


		for(i=0;i<v;i++)
		{
		      setbkcolor(0);
		      fillellipse(vrt_x[i],vrt_y[i],2,2);
		      outtextxy(vrt_x[i],vrt_y[i]-10,vrt_name[i]);
		}


		/* Adjancey Matrix */



		getch();
		return 0;
	}

	else
  	{
    		printf("Not enough memory\n");
    		return 1;
  	}





}
 
Joined
Jun 14, 2004
Messages
778
Dont you think you should input the vertices name (vrt_name[]) with a %c format specifier? %s might be pushing an extra NULL to the array writing in an out of bound area of memory.
 
Status
Not open for further replies.
Top