Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,174 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,492 people online right now. Registration is fast and FREE... Join Now!




phone book

 
Reply to this topicStart new topic

phone book, linked list

kierot
post 4 Oct, 2008 - 09:36 PM
Post #1


New D.I.C Head

*
Joined: 8 Aug, 2008
Posts: 3

cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct record{
char name[64];
int number;
struct record *next;

}student;

student *addContact(student *list);
student *viewContact(student *list);
student *searchContact(student *list);
student *deleteAll(student *list);

main(){
student *ptr, *head=NULL; int option = 0, k;

while(option!=5){

printf("\n M E N U \n[1] Add Contact\n[2] View Contact\n[3] Search Contact\n[4] Delete All Contact\n[5] Exit\nOption: ");

scanf("%i",&option);

switch(option){
case 1: head = addContact(head);
break;

case 2: head = viewContact(head);
break;

case 3: head = searchContact(head);
break;

case 4: head = deleteAll(head);
break;

case 5:
exit(1);

default: printf("\nInvalid Option!\n\n");
}
}
}
student * addContact(student * list){
student * new;

new = (student *) malloc(sizeof(student));

printf("\nName: "); scanf("%s", new->name);
printf("Number: "); scanf("%i", &new->number);

new->next = list;
return new;
}
student * viewContact(student * list){
student * ptr = list;
if(NULL==ptr){
printf("\nNOTHING to view\n");
}else{
printf("\nCONTACTS\n");
while(NULL!=ptr){
printf("Name: %s\nNumber: %i\n",ptr->name, ptr->number);
ptr = ptr->next;
}
}
return list;
}
student * searchContact(student * list){
student * ptr = list;

printf("search for: ");
scanf("%s", ptr);

while(ptr){
if(strcmp(ptr->next,ptr)>0)
return ptr;
ptr=ptr->next;
}
return NULL;
//printf("NO DATA FOUND\n");
//}else{
//printf("\nName: %s Number: %d\n", ptr->name,ptr->number);
//ptr = ptr->next;
//}
}


student * deleteAll(student * list){
student * ptr = list;

while(ptr){
list = ptr->next;
free(ptr);
ptr = list;
}
return NULL;
}


** Edit ** code.gif
User is offlineProfile CardPM

Go to the top of the page

kierot
post 4 Oct, 2008 - 09:40 PM
Post #2


New D.I.C Head

*
Joined: 8 Aug, 2008
Posts: 3

the problem is in the search function, this function cannot do what it suppose to do...

plz. help..thank you..
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 4 Oct, 2008 - 09:55 PM
Post #3


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 501



Thanked 44 times

Dream Kudos: 25
My Contributions


cpp
student * searchContact(student * list){
student * p = head;

printf("search for: %s\n", list->name);

while(p){
if(strcmp(p->name, list->name) == 0)
return p;
p = p->next;
}
return NULL;
}
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 4 Oct, 2008 - 09:56 PM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


** Topics Merged **
User is offlineProfile CardPM

Go to the top of the page

Sadaiy
post 5 Oct, 2008 - 05:00 AM
Post #5


New D.I.C Head

*
Joined: 3 Oct, 2008
Posts: 35



Thanked 1 times
My Contributions


wouldn't you want while(p != NULL) ??? Also, you need error checking incase the user enters a value that is not in the list.

This post has been edited by Sadaiy: 5 Oct, 2008 - 05:01 AM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 03:28PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month