Big problem, but a noob to identify it!

This is a discussion on Big problem, but a noob to identify it! within the C Programming forums, part of the General Programming Boards category; A mobile phonebook system that just doesnt want to work. This is what ive got so far... Code: #include <conio.h> ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Big problem, but a noob to identify it!

    A mobile phonebook system that just doesnt want to work.

    This is what ive got so far...

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <alloc.h>
    #include <string.h>
    
    struct node
    {
    	int num;
    	char name[15];
    	struct node *next;
    };
    
    struct node *list; /* global start pointer */
    FILE *fp; /* Global file pointer */
    
    struct node *getrecord()
    {
    	struct node *temp,e;
    	temp=(struct node*)malloc(sizeof(struct node));
    	
    	printf("Reached till here...");
    	getch();
    	
    	fflush(stdin);
    	fscanf(fp,"%d %s",&temp->num,temp->name);
    	
    	fread(&temp,sizeof(temp),1,fp);
    	printf("%d %s",temp->num,temp->name);
    	
    	temp->num=e.num;
    	strcpy(temp->name,e.name);
    	getch();
    	temp->next = NULL;
    	return temp;
    }
    
    struct node *getnode()
    {
    	struct node *temp;
    	temp = (struct node*)malloc(sizeof(struct node));
    	printf("Enter the person's phone number--->");
    	scanf("%d",&temp->num);
    	printf("Enter the person's Name--->");
    	scanf("%s",&temp->name);
    	
    	temp->next = NULL;
    	return temp;
    }
    
    struct node *search(int id,int *flag)
    {
    	struct node *cur,*prev;
    	*flag=0;
    
    	if
    		(list==NULL)
    		return NULL;
    	
    	for(prev=NULL,cur=list; (cur); prev=cur,cur=cur->next)
    		if(cur->num == id)
    		{
    			*flag = 1;
    			break;
    		}
    	return prev;
    }
    
    int insert(struct node *new1)
    {
    	struct node *prev;
    	int flag;
    	
    	prev = search(new1->num,&flag);
    	
    	if(list==NULL)
    	{
    		list = new1; /* first node in list */
    		return;
    	}
    	
    	if(flag==1)
    		return -1;
    	else
    	{
    		new1->next = prev->next;
    		prev->next = new1; /* insert in the middle or end */
    	}
    	
    	return 0;
    }
    
    void display()
    {
    	struct node *cur;
    	auto int n=0;
    	
    	if(list==NULL)
    	{
    		printf("List empty !");
    		return;
    	}
    	
    	printf("The phone records are--->");
    	cur=list;
    	
    	for(cur=list;(cur);cur=cur->next)
    	{
    		printf("Record no. %d--->",n+1);
    		printf("Record phone no.:%d",cur->num);
    		printf("person Name:%s",cur->name);
    		n++;
    	}
    }
    
    int delnode(int id)
    {
    	struct node *prev,*temp;
    	int flag=0;
    	
    	if(list==NULL)
    		return -1;
    	
    	prev=search(id,&flag);
    	
    	if(flag==0)
    		return -1;
    	
    	if(prev==NULL)
    	{
    		temp=list;
    		list=list->next;
    		free(temp);
    	}
    
    	else
    	{
    		temp=prev->next;
    		prev->next=temp->next;
    		free(temp);
    	}
    	
    	return 0;
    }
    
    struct node *query(int id,char *flag,int toggle)
    {
    	struct node *cur,*prev;
    	int x=0;
    	
    	if(list==NULL)
    		return NULL;
    	
    	if(toggle)
    	{
    		for(prev=NULL,cur=list; (cur); prev=cur,cur=cur->next)
    			
    			if(cur->num == id)
    			{
    				x=1;
    				break;
    			}
    	}
    	
    	else
    	{
    		for(prev=NULL,cur=list; (cur); prev=cur,cur=cur->next)
    			
    			if(!strcmp(cur->name,flag))
    			{
    				x=1;
    				break;
    			}
    	}
    	
    	if(x==0)
    	{
    		printf("The number doesnt exist !");
    		return NULL;
    	}
    	
    	return cur;
    }
    
    void CopyToFile()
    {
    	FILE *fp;
    	struct node *cur;
    	fp=fopen("phone.txt","w");
    	
    	cur=list;
    	
    	if(cur==NULL)
    	{
    		printf("The list is empty, nothing to write back...");
    		return;
    	}
    
    	for(cur=list;(cur);cur=cur->next)
    		fprintf(fp,"%d %s",cur->num,cur->name); /* writing back to file */
    	fclose(fp);
    }
    
    
    
    void main()
    {
    	int ch=0,ans=0;
    	int val=0;
    	char str[15];
    	struct node *new1,*new2;
    	
    	clrscr();
    	/* initialization of list with the existing records */
    	
    	fp=fopen("phone.txt","r");
    	
    	if(fp==NULL)
    	{
    		printf("File cant be opened !");
    		getch();
    		
    		exit(1);
    	}
    	
    	while(!feof(fp))
    	{
    		new1=getrecord();
    		
    		if(insert(new1)==-1)
    			printf("Cant insert record ! Error...");
    		else
    			printf("Record entered...");
    	}
    	
    	getch();
    	fclose(fp);
    	
    	do
    	{
    		clrscr();
    		printf("Menu");
    		printf("1. Add a New phone record.");
    		printf("2. Delete an existing Record.");
    		printf("3. show all Records.");
    		printf("4. Modify a particular Record.");
    		printf("5. Exit.");
    		printf("Enter your choice--->");
    		scanf("%d",&ch);
    		
    		switch(ch)
    		{
    			case 1:
    				new1 = getnode();
    				val = insert(new1);
    				
    				if(val==-1)
    					printf("Employee id already exists ! try again...");
    				else
    					printf("Employee details successfully stored");
    				break;
    			case 2:
    				printf("Enter the phone no.you wish to delete--->");
    				scanf("%d",&val);
    				ans=delnode(val);
    				if(ans==-1)
    					printf("Record doesnt exist ! Try again...");
    				if(ans==0)
    					printf("Record deleted !");
    				break;
    			case 3:
    				display();
    				break;
    			case 4:
    				printf("Search by phone no. or Name ? (1/2)--->");
    				scanf("%d",&ch);
    				if(ch==1)
    				{
    					printf("Enter the phone no. you wish to search for--->");
    					scanf("%d",&val);
    					new2=query(val,&str,1);
    				}
    				else
    				{
    					printf("Enter the name of the person you wish to search for--->");
    					scanf("%s",&str);
    					new2=query(val,&str,0);
    				}
    				
    				if(new2)
    				{
    					printf("Enter the New name of the person--->");
    					scanf("%s",&new2->name);
    					printf("Enter new phone no. of the person--->");
    					scanf("%d",&new2->num);
    					printf("Record modified successfully !");
    				}
    				break;
    			case 5:
    				printf("Leaving Database,writing back to file...");
    				CopyToFile();
    				getch();
    				free(list);
    				exit(1);
    				break;
    		}
    		
    		getch();
    	}while(1);
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    OK, help us help you. What doesn't work? Narrow it down please.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    ROCKINGSTUDENTZ NO 1 BLOG FOR STUDENTS: Read Phone Records in a .txt fle
    View topic - Program To Implement A Phone Record Database in C &bull; India Largest Job Discussion Forum &bull; IndiaJobForum.com
    Phone Book Project.

    Your code looks awfully familiar. When you write code yourself, you usually have a much better chance at finding the problem, since you actually know what's going on.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by lovenetuk View Post
    A mobile phonebook system that just doesnt want to work.

    This is what ive got so far...
    Ummm... no.... here's what you've copied so far. More frigging scoop and poop coding!

    If you'd actually written the code you'd find simple problems like a phone number being to big to fit in an integer.

    As already pointed out... If you'd written that code yourself, you would know what's going on inside it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Problem, I'm such a noob :d
    By Shamino in forum C++ Programming
    Replies: 1
    Last Post: 10-18-2005, 09:52 AM
  2. [noob] i can't even name the problem
    By knoxville in forum C Programming
    Replies: 19
    Last Post: 03-15-2005, 05:32 AM
  3. can you identify this car?
    By axon in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 07-19-2004, 02:59 PM

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