| | |
need help on simple turbo C program.. thx
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2007
Posts: 2
Reputation:
Solved Threads: 0
My program is a very simple address book program. The program works but i don't know how to make it display the same names. Like for example, the name "Mark Lee", if there are more than one Mark Lee, i would like to display all of those Mark Lee w/ its corresponding address & telephone no. How do i do that? What do i have to change in my program? I'm really very confused.
This is what my program looks like right now.
This is what my program looks like right now.
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <conio.h> #include <string.h> #include <stdlib.h> struct person { char fname[20]; char lname[15]; char address[10]; char telephone[10]; }; char Selection(char choice) { printf("a. Data Entry"); printf("\nb. Search and Edit a Record"); printf("\nc. Search and Delete a Record"); printf("\nd. Display all Records"); printf("\ne. Exit from the program"); printf("\n\nEnter your choice: "); scanf(" %c",&choice); return choice; } int Data_Entry (struct person prson[20], int i) { char fn[20], ln[15], addy[50], tphone[10]; printf("Enter info for student no. %d",i+1); gets("\n"); printf("\nFirst Name: "); gets(fn); strcpy(prson[i].fname, fn); printf("Last Name: "); gets(ln); strcpy(prson[i].lname, ln); printf("Address: "); gets(addy); strcpy(prson[i].address, addy); printf("Telephone No.: "); gets(tphone); strcpy(prson[i].telephone, tphone); i++; return i; } int Data_Edit(struct person prson[20], int i) { int j=0, k=3, found=0; char ans; char last[15]; char first[20]; char lastn[15]; char firstn[20]; char addr[10]; char tele[10]; printf("Enter last name: "); gets("\n"); gets(last); printf("\nEnter first name: "); gets(first); for ( j=0;j<i;j++ ) { if ( stricmp(last, prson[j].lname)==0 && stricmp(first, prson[j].fname)==0 ) { found=1; clrscr(); printf("Name Address Tel. No."); gotoxy(1,k);printf("%s, %s",prson[j].lname, prson[j].fname); gotoxy(30,k);printf("%s",prson[j].address); gotoxy(60,k);printf("%s",prson[j].telephone); gotoxy(1,k+3);printf("Continue editing?(Y/N): "); scanf(" %c",&ans); gets("\n"); if ( ans=='y' || ans=='Y' ) { printf("\n\nEnter last name: "); gets(lastn); strcpy(prson[j].lname, lastn); printf("\nEnter first name: "); gets(firstn); strcpy(prson[j].fname, firstn); printf("\nAddress: "); gets(addr); strcpy(prson[j].address, addr); printf("\nTelephone No.: "); gets(tele); strcpy(prson[j].telephone, tele); } } } if ( found==0 ) { printf("\n\nThe name you entered cannot be found."); getch(); } } int Data_Delete(struct person prson[20], int i) { int a=0, j=0, k=3, found=0; char ans2; char lstn[15]; char fstn[20]; char addre[10]; char telle[10]; printf("Enter last name: "); gets("\n"); gets(lstn); printf("\nEnter first name: "); gets(fstn); for ( j=0;j<i;j++ ) { if ( stricmp(lstn, prson[j].lname)==0 && stricmp(fstn, prson[j].fname)==0 ) { found=1; clrscr(); printf("Name Address Tel. No."); gotoxy(1,k);printf("%s, %s",prson[j].lname, prson[j].fname); gotoxy(30,k);printf("%s",prson[j].address); gotoxy(60,k);printf("%s",prson[j].telephone); gotoxy(1,k+3);printf("Continue deleting?(Y/N): "); scanf(" %c",&ans2); gets("\n"); if ( ans2=='y' || ans2=='Y' ) { strcpy(prson[j].lname, prson[j+1].lname); strcpy(prson[j].fname, prson[j+1].fname); i--; } } } if ( found==0 ) { printf("\n\nThe name you entered cannot be found."); getch(); } return i; } Display(struct person prson[20], int i) { int j, k=3; clrscr(); printf("Name Address Tel. No."); for ( j=0;j<i;j++ ) { gotoxy(1,k);printf("%s, %s",prson[j].lname, prson[j].fname); gotoxy(30,k);printf("%s",prson[j].address); gotoxy(60,k);printf("%s",prson[j].telephone); k++; } getch(); } typedef struct person prson; main() { int i=0; struct person prson[20]; char choice=''; do { clrscr(); choice=Selection(choice); if ( choice=='a' ) { clrscr(); i=Data_Entry(prson,i); } if ( choice=='b' ) { clrscr(); Data_Edit(prson, i); } if ( choice=='c' ) { clrscr(); i=Data_Delete(prson,i); } if ( choice=='d' ) { Display(prson, i); } } while ( choice!='e' ); printf("\n\n\n Good Bye!"); getch(); }
Last edited by Ancient Dragon; Aug 19th, 2007 at 6:12 am. Reason: add line numbers
>>The program works
No it doesn't.
you use gets() all over the place. Try entering some text that is longer than the buffer can hold and you will find that your program doesn't work afterall. gets() will just scribble all over memory with the extra characters and cause your program to crash at some point. The solution is to use fgets() instead to limit data input.
line 30: parameter to gets() is wrong.
in main() variable i is apparently being used to mark the next available slot in the prson array. But then you use i again for several other reasons. I think you need to set up a different variable for that purpose and not use it for anything else. You also need to verify that you don't try to enter more than 20 people because that's all the array can hold -- if you add the 21st person your program will crash.
As for your original question -- I think you need another menu option to show records for only a specific person then prompt for that person's name. Then its a simple loop to search the array for the person and print it when found.
No it doesn't.
you use gets() all over the place. Try entering some text that is longer than the buffer can hold and you will find that your program doesn't work afterall. gets() will just scribble all over memory with the extra characters and cause your program to crash at some point. The solution is to use fgets() instead to limit data input.
line 30: parameter to gets() is wrong.
in main() variable i is apparently being used to mark the next available slot in the prson array. But then you use i again for several other reasons. I think you need to set up a different variable for that purpose and not use it for anything else. You also need to verify that you don't try to enter more than 20 people because that's all the array can hold -- if you add the 21st person your program will crash.
As for your original question -- I think you need another menu option to show records for only a specific person then prompt for that person's name. Then its a simple loop to search the array for the person and print it when found.
Last edited by Ancient Dragon; Aug 19th, 2007 at 6:21 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
I want to say only that, use some more modern compiler than turbo c, like gcc (mingw or cygwin in windows). Turbo c is an extinct compiler, somewhat hopelessly outdated, and you cannot find libraries for it etc. Otherwise, using conio is perfectly ok, there is conio for windows, and there is also conio for linux, so we may say it's a cross-platform library, and at that very easy to implement. There are really no better alternatives for windows, because windows console is so primitive that it cannot accept ansi terminal escape sequences. And it doesn't make sense to learn curses, as today it doesn't make sense any more to write user interfaces in text mode, but gui is a bit too complicated for the most simple programs.
Knowledge is regarded by the fool as ignorance, and the things that are profitable are to him hurtful. He liveth in death. -- Thoth the Atlantean
![]() |
Similar Threads
- need help for address book program in Turbo C not C++.. (C)
- help needed with my assignment (C)
- writing a simple cat program (C)
- Help with simple Java IO program running under cygwin (Java)
- How do you add animation to visual C++ program? (C++)
Other Threads in the C Forum
- Previous Thread: Read a Line of Text from the User
- Next Thread: Access Violation
| Thread Tools | Search this Thread |
adobe api array arrays bash binarysearch calculate char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators initialization intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue multi mysql oddnumber odf open opendocumentformat opensource openwebfoundation pattern pdf performance pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition scanf scheduling scripting segmentationfault send shape socketprograming socketprogramming stack standard strchr string strings suggestions test testautomation unix urboc user variable voidmain() win32api windows.h






