Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Divine's command line phonebook application */
- /*written in the C programming language w/vs */
- #include <stdio.h>
- #include <stdlib.h>
- char name[25];
- int number[25];
- lookup();
- add();
- add2();
- dump_line();
- noname();
- main()
- {
- int x=0;
- printf(" *********************PHONE BOOK********************\n\n");
- printf("1. Lookup\n2. Add new entry\n3. Exit\n\n");
- scanf("%d",&x);
- while (x != 1 && x != 2 && x != 3){
- dump_line(stdin);
- scanf("%d",&x);
- }
- if (x == 1 )
- {
- system("cls");
- lookup();
- }
- else if (x ==2)
- {
- system("cls");
- add();
- }
- else{
- printf("Exiting phonebook\n");
- return 0;
- }
- getch();
- } /********END OF MAIN FUNCTION***********/
- lookup()
- {
- int x,y=5;
- FILE *pt;
- char findname[25];
- char words[25];
- pt = fopen ("phonebook.txt","r");
- if (pt == NULL)
- {
- printf("File not found!\n");
- return 0;
- }
- printf(" *****************PHONE BOOK LOOKUP*****************\n\n\n");
- printf("Type \"all\" to list all entries\n\n");
- printf("Enter a name: \n");
- scanf("%25s",name);
- if ( stricmp(name,"all") != 0 ){
- do{
- if (y == EOF){
- printf("Name doesn't exist\n\n");
- getch();
- noname();
- }
- y = fscanf(pt,"%s",findname);
- }while (stricmp(name,findname) != 0);
- fgets (number,25,pt);
- printf("\n\nName: %s\nNumber:%s",findname,number);
- }
- else if ( stricmp (name,"all") == 0 )
- {
- x = 1;
- printf("\n");
- do {
- x = fscanf(pt,"%s",words);
- if (x == EOF)
- break;
- printf("Name: %s\n",words);
- x = fscanf(pt,"%s",words);
- printf("Number: %s\n",words);
- printf("\n---------------------------\n");
- } while (x != EOF);
- }
- getch();
- printf("\n\nWould you like to look up another name? (y//n)\n\n");
- do {
- x = getch();
- } while (x != 'y' && x != 'n');
- if (x == 'y'){
- system("cls");
- lookup();
- }
- else if (x == 'n'){
- system("cls");
- main();
- }
- fclose (pt);
- } /*******END OF LOOKUP FUNCTION************/
- add()
- {
- FILE *pt;
- printf(" *****************ADD ENTRY*****************\n\n\n");
- printf("Add a name and a number now:\n\n");
- pt = fopen ("phonebook.txt","a");
- if (pt == NULL)
- {
- printf("NO PHONEBOOK FOUND!\n");
- return 0;
- }
- scanf("%s %s",name,number);
- fprintf (pt,"%s %s\n",name,number);
- printf("\nNumber added successfully!\n\n");
- getch();
- add2();
- } /*******END OF ADD FUNCTION********/
- add2()
- {
- int option;
- printf("Would you like to add another number? (y//n)\n");
- do {
- option = getch();
- } while (option != 'y' && option != 'n');
- if (option == 'y'){
- system ("cls");
- add();
- }
- else {
- printf("\nReturning to main menu\n\n");
- system ("cls");
- main();
- }
- } /***end of add2 function***/
- noname()
- {
- int option;
- printf("Would you like to look up another name? (y//n)\n\n");
- do{
- option = getch();
- } while (option != 'y' && option != 'n');
- if (option == 'y'){
- system("cls");
- add();
- }
- else {
- printf("Returning to main menu\n\n");
- system ("cls");
- main();
- }
- } /***end of noname function****/
- dump_line(FILE *fp)
- {
- int ch;
- while( (ch = fgetc(fp)) != EOF && ch != '\n' )
- /* null body */;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement