|
 |
|
 |
04-21-2003, 10:30 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2003
Posts: 4
|
more help needed
I have made this program, but for everything it says "name not found", and runs out of memory, why is this happening???
the tbfile.txt is an empty file
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define TEL_BOOK_SIZE 50
#define MAX_STRING_SIZE 82
struct telBookElement
{
char *name;
char *telNum;
};
typedef struct telBookElement TelBookElement;
struct telBook
{
TelBookElement element[TEL_BOOK_SIZE];
int n;
};
typedef struct telBook TelBook;
int binarysearch (const TelBook *tbp, const char searchname[], int *ip);
void putTelBook(const char *fname,TelBook *tbp);
void fgetTelBook(TelBook *tbp);//done
void displayMenu(void);//done
char *strToUpper(char *s);
void lookUpName(TelBook *tbp);
void listName(TelBook *tbp);
void modify(TelBook *tbp);
void del(TelBook *tbp);
void inputNewBook(const char *fname,TelBook *tbp);
void sortTelBook(TelBook *tbp);
char *fgetString(FILE *fp);//done
void lookupnumber(TelBook *tbp);
int linearSearch(const TelBook *tbp, const char num[]);
void main()
{
TelBook tb;
TelBook *tbp=&tb;
char save,keyHit,fname[22]="a:tbfile.txt";
fgetTelBook(tbp);
do
{ displayMenu();
keyHit=getchar();
getchar(); /* get rid of the new line character */
switch(keyHit)
{ case '1': inputNewBook(fname,tbp); break;
case '2': lookUpName(tbp); break;
case '4': modify(tbp); break;
case '5': del(tbp); break;
case '6': listName(tbp); break;
case '3': lookupnumber(tbp);break;
case '7':
printf("Do you want to save it (y/n)? "); save=getchar();
if(save=='y'||save=='Y')
putTelBook(fname,&tb);
break;
default:
printf("Option is not correct. Hit Enter to continue");
getchar();
break;
}
}while(keyHit!='7');
system("PAUSE");
}
/**********************************************************************************************/
/**********************************************************************************************/
void displayMenu(void)
{
printf("Automated Telephone Book Menu\n");
printf("1) New telephone book-upper case and sort it\n");
printf("2) Look up name to get number\n");
printf("3) Look up number to get name\n");
printf("4) Insert new/modified element\n");
printf("5) Delete element\n");
printf("6) List names starting with given letter\n");
printf("7) Quit\n");
printf("Enter choice (1-7): ");
}
/**********************************************************************************************/
/**********************************************************************************************/
char *fgetString(FILE *fp)
{ char s[MAX_STRING_SIZE];
char *inputStatus, *sp;
int n;
inputStatus = fgets(s, MAX_STRING_SIZE, fp); /*input string*/
if (inputStatus == NULL) /*if end of file*/
return NULL; /*return NULL like fgets*/
n = strlen(s); /*compute string length*/
if (s[n-1] == '\n') s[--n] = '\0'; /*if present, get rid of new-line char*/
sp = malloc(n + 1); /*allocate dynamic memory*/
if (sp == NULL) /*if out of memory*/
{ printf("Out of storage for string\n"); /*print error message*/
exit(1); /*exit program, abnormal termination*/
} /*otherwise memory was allocated*/
strcpy(sp, s); /*copy string to allocated memory*/
return sp; /*return pointer to allocated string*/
}
/**********************************************************************************************/
/**********************************************************************************************/
void fgetTelBook(TelBook *tbp)
{ char *namep;
int i;
FILE *fp = fopen("a:tbfile.txt", "r"); /*change file name to complete path name*/
if (fp == NULL) /*check to see if file opened*/
{ printf("The \"tbfile.txt\" cannot be opened\n"); /*if not print error message*/
exit(1); /*exit program, abnormal termination*/
} /*otherwise file was opened*/
i = 0;
namep = fgetString(fp); /*input name*/
while(namep != NULL) /*while not at end of file*/
{ tbp->element[i].name = namep; /*put name in i_th element*/
tbp->element[i].telNum=fgetString(fp);
i++;
namep = fgetString(fp); /*input next name*/
}
tbp->n=i;
fclose(fp);
}
/**********************************************************************************************/
/**********************************************************************************************/
void inputNewBook(char *fname,TelBook *tbp)
{
int m;
char save;
printf("Do you want to save the current one (y/n)? ");
save=getchar(); getchar();
if(save=='y'||save=='Y')
putTelBook(fname,tbp);
printf("Enter the filename: ");
gets(fname);
fgetTelBook(tbp);
for(m=0;m<tbp->n;m++)
strToUpper(tbp->element[m].name);
sortTelBook(tbp);
printf("New telephone book has been inputed.\n");
printf("Press Enter to continue"); /* pause the output */
getchar();
}
/**********************************************************************************************/
/**********************************************************************************************/
void sortTelBook(TelBook *tbp)
{ int pass,m;
char hold[MAX_STRING_SIZE];
for(pass=1;pass<tbp->n;pass++)
for(m=0;m<(tbp->n)-pass;m++)
if(strcmp(tbp->element[m].name,tbp->element[m+1].name)>0)
{ /* swap 2 elements */
strcpy(hold,tbp->element[m].name);
strcpy(tbp->element[m].name,tbp->element[m+1].name);
strcpy(tbp->element[m+1].name,hold);
strcpy(hold,tbp->element[m].telNum);
strcpy(tbp->element[m].telNum,tbp->element[m+1].telNum);
strcpy(tbp->element[m+1].telNum,hold);
}
}
/**********************************************************************************************/
/**********************************************************************************************/
char *strToUpper(char *s)
{ int i;
for(i=0;s[i]!='\0';i++)
s[i]=toupper(s[i]);
return s;
}
/**********************************************************************************************/
/**********************************************************************************************/
int binarySearch(const TelBook *tbp, const char searchName[], int *ip)
{ int low,high,middle;
low=0;high=tbp->n-1;
while(low<=high)
{ middle=(low+high)/2;
if(strcmp(searchName,tbp->element[middle].name)==0)
{ *ip=middle;
return 1;
}
else if(strcmp(searchName,tbp->element[middle].name)<0)
high=middle-1;
else
low=middle+1;
}
*ip=low;
return 0;
}
/**********************************************************************************************/
/**********************************************************************************************/
void putTelBook(const char *fname,TelBook *tbp)
{ int m;
FILE *fp;
if ((fp=fopen(fname, "w"))==NULL)
{ printf("File could not be opened to write\n"); exit(1);
}
for(m=0;m<tbp->n;m++)
{ fputs(tbp->element[m].name,fp);
fputs(tbp->element[m].telNum,fp);
}
fclose(fp);
}
/**********************************************************************************************/
/**********************************************************************************************/
void lookUpName(TelBook *tbp)
{ char searchName[MAX_STRING_SIZE];
int i;
printf("Enter a name you want you look up: ");
fgets(searchName,MAX_STRING_SIZE,stdin);
strToUpper(searchName);
if(binarySearch(tbp,searchName,&i))
{ printf("Name found:\n");
printf("%s",tbp->element[i].name);
printf("%s",tbp->element[i].telNum);
}
else
printf("Name not Found\n");
printf("Press Enter to continue");
getchar();
}
/**********************************************************************************************/
/**********************************************************************************************/
void listName(TelBook *tbp)
{ char name[MAX_STRING_SIZE];
int i,found=0;
printf("Enter a name you want to list: ");
gets(name);
strToUpper(name);
for(i=0;i<tbp->n;i++)
if(tbp->element[i].name==strstr(tbp->element[i].name,name))
{ found=1;
printf("%s",tbp->element[i].name);
printf("%s",tbp->element[i].telNum);
}
if (!found)
printf("Name not in the list\n");
printf("Press Enter to continue");
getchar();
}
/**********************************************************************************************/
/**********************************************************************************************/
void modify(TelBook *tbp)
{ int i,j;
char name[MAX_STRING_SIZE],tel[MAX_STRING_SIZE];
printf("Enter the name to modify or insert: ");
fgets(name,MAX_STRING_SIZE,stdin);
strToUpper(name);
printf("Enter the telephone number: ");
fgets(tel,MAX_STRING_SIZE,stdin);
if(binarySearch(tbp,name,&i)) /* found element */
{ printf("Modified element Inserted\n");
strcpy(tbp->element[i].name,name); /* i is position modified */
strcpy(tbp->element[i].telNum,tel);
}
else /* not found the element, i is position inserted */
{
printf("New element Inserted\n");
/* moving entries up by one position*/
for(j=tbp->n;j>i;j--)
{ strcpy(tbp->element[j].name,tbp->element[j-1].name);
strcpy(tbp->element[j].telNum,tbp->element[j-1].telNum);
}
strcpy(tbp->element[i].name,name);
strcpy(tbp->element[i].telNum,tel);
tbp->n++;
}
printf("Press Enter to continue");
getchar();
}
/***********************************************************************
***********************************************************************/
void del(TelBook *tbp)
{ int i,j;
char name[MAX_STRING_SIZE];
printf("Enter the name to delete: ");
fgets(name,MAX_STRING_SIZE,stdin);
strToUpper(name);
if(binarySearch(tbp,name,&i))
{ printf("element Deleted\n");
for(j=i;j<tbp->n-1;j++) /* moving entries down by one position*/
{ strcpy(tbp->element[j].name,tbp->element[j+1].name);
strcpy(tbp->element[j].telNum,tbp->element[j+1].telNum);
}
tbp->n--;
}
else
printf("Name not Found\n");
printf("Press Enter to continue");
getchar();
}
/***********************************************************************
This function looks up the number
***********************************************************************/
void lookupnumber(TelBook *tbp)
{
char num[MAX_STRING_SIZE];
int i=0,k;
printf("Enter a number to look up: ");
fgets(num,MAX_STRING_SIZE,stdin);
k=linearSearch(tbp,num);
if(k!=-1)
{
printf("Name found:\n");
printf("%s",tbp->element[k].name);
printf("%s",tbp->element[k].telNum);
}
else
printf("Name not Found\n");
printf("Press Enter to continue");
getchar();
}
/***********************************************************************
performs linear search
***********************************************************************/
int linearSearch(const TelBook *tbp, const char num[])
{
int i=0;
for ( i = 0 ; i < tbp->n ; i++ ) {
if ( strcmp( num, tbp->element[i].telNum ) == 0 ) return i;
}
return -1;
}
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:32 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|