C code problematic


amitraj
02-27-2004, 04:52 AM
hello,
I have generated a code which
1.) creates a STATIC ARRAY OF STRUCTURE and holds
- CITY NAME
- X and Y coordinates of the city.
2.) It then asks the user to enter some names, if at all they are there in the ARRAY, it accepts and creats a linked list of the entered names, which again contais the names and theie corresponding coordinates.
3.) It then asks for printing the data.

---here comes the proble. Actually, the code is not printing the data properly, it also prints a bulk of GARBAGE and then exits.

Could somebody help me getting out of the problem?
CODE::

/*
finaltemp.c
*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<alloc.h>
#include<process.h>
#define SIZE 10
struct trystr
{
char city[20];
int x,y;
};
static struct trystr t1[SIZE]; /*An array which will hold 10 entries of cities*/

struct link /*Linked List Source*/
{
char city[20];
int x,
y;
struct link *next;
};
struct link *root,
*prev,
*run;


/*...GLOBAL DECLARATION ENDS...*/

void main(void)
{
void readarr(void);
void init_struct_arr(void);
void showlist(int); /*...function declaration*/

clrscr();
init_struct_arr(); /*function call for array initialisation*/
readarr(); /*function call for reading*/


getch();
}

void init_struct_arr(void)
{
strcpy(t1[0].city,"pune");
t1[0].x=21;
t1[0].y=22;
strcpy(t1[1].city,"mumbai");
t1[1].x=89;
t1[1].y=45;
strcpy(t1[2].city,"banglore");
t1[2].x=56;
t1[2].y=67;
strcpy(t1[3].city,"delhi");
t1[3].x=100;
t1[3].y=100;
strcpy(t1[4].city,"chennai");
t1[4].x=17;
t1[4].y=45;
strcpy(t1[5].city,"bhopal");
t1[5].x=14;
t1[5].y=85;
strcpy(t1[6].city,"aurangabad");
t1[6].x=78;
t1[6].y=23;
strcpy(t1[7].city,"jammu");
t1[7].x=200;
t1[7].y=45;
strcpy(t1[8].city,"nagpur");
t1[8].x=60;
t1[8].y=41;
strcpy(t1[9].city,"kashmir");
t1[9].x=300;
t1[9].y=50;
}

void readarr() /*function to creat a linked list of asked cities if they exist in the structure array*/
{

int i, /*General counter*/
cmpptr1, /*Return value after CMPSTR in the outer comparison*/
nodes=0, /*Total nodes in the output linked list*/
choice=1;
char usercity[20];
void showlist(int);

root=(struct link *)malloc(sizeof(struct link *));
prev=run=root;
while(choice==1)
{
printf("\nWhat City:");
scanf("%s",usercity);
for(i=0;i<SIZE;i++)
{
cmpptr1=strcmp(usercity,t1[i].city);
if ( cmpptr1==0 )
{
printf("\nCity Found...\n");
strcpy(run->city,t1[i].city);
run->x=t1[i].x;
run->y=t1[i].y;
run=(struct link *)malloc(sizeof(struct link *));
prev->next=run;
prev=run;
prev->next=NULL;
nodes=nodes+1;
printf("\nNode Created...");
break;
}
}
if(cmpptr1!=0)
{
printf("\ncity not found!!!!");
}
printf("\nContinue? (1 for YES):");
scanf("%d",&choice);
}
run->next=NULL;
printf("Total cities considered=%d",nodes);
showlist(nodes);
}


void showlist(int nodes)
{
int i;
clrscr();
printf("Print the cities? (1 for YES) :");
scanf("%d",&i);
if(i==1)
{
prev=run;
run=root;
while(run->next!=NULL)
{
printf("\n%s\t%d,%d",run->city,run->x,run->y);
run=run->next;
}
printf("\n%d node(s)",nodes);
}
}


REGARDS.