0

I want to enter a name into a program and that name will act as a keyword for a file. The program will search the file for that name and display all other information related to it. Below is a snippet of the code.

#include<conio.h>
#include<stdio.h>
#include<windows.h>
#include<string.h>


FILE *ptr;
int bookgenre;
char fname[20], lname[20],book[20],gender[20];
struct persfile{
    char fname[20];
    char lname[20];
    char bookname[20];
    int  date, latefees;
    char gender;
};
void menu ();
void caseCheck(int);

int main(void)
{
    int New = 0, genrenum = 0, count=0;
    struct persfile personfile[count]; //Intialization of the struct in the main function
    menu();

    /*New = getch();*/
    scanf("%d",&New);
        system ("cls");

        if(New == 2)
        {
         for (count=1; count<=200; count++ )
         {

            int latefees=0;
            ptr = fopen("membersit.txt", "a");

             /*for (count=1;count<=5;count++)*/


            printf("Enter the first name of the person \n");
            scanf("%s", personfile[count].fname);
            printf("Enter the last name of the person \n\n");
            scanf("%s", personfile[count].lname);
            printf("Enter the gender of the individual....(M/F) \n");
            personfile[count].gender = getch();
            printf("%c", personfile[count].gender);
                                                            // At a later date...examine the time library and look at its connection to the
            system("cls");                                          //date at which the book was rented.. //

            printf("The code for the differnt book genres are given below\n\n");
            printf("000-Kids...111-Supernatural...222-Gaming\n\n");
            printf("_____________________\n\n");


            printf("Enter the genre number of said book\n");
            scanf("%d", &genrenum);

            system ("cls");
            printf("These are the books under this genre\n\n");
            printf("______________");
            system("cls");

            caseCheck(genrenum);
            scanf("%s",book);

            printf("Since this is a new individual..he has no outstandng latefees");
            fprintf(ptr,"%s %s %d \n", personfile[count].fname, personfile[count].lname, personfile[count].latefees);
            fclose((ptr));
            system("cls");
         }
        } 

        else if(New==3)
        {

        ptr=fopen("membersit.txt", "r");

        fscanf(ptr,"%s %s",fname,  lname);


        printf("First Name: %s \n Last Name: %s \n  Latefees: %d  \n", fname, lname);

        fclose((ptr));


    /*if (ptr==NULL){
        printf("This file is not in our system");
        exit(0);
     }*/
        }

    /*search the existing files for the name of the person and open up their file
    The opened file would have late fees as a header..*/



system("Pause");
return 0;
};


void menu ()

{
    printf("Welcome to the library!!");
    printf("\n___________________________________________________");
    printf("\n\nWhat would you like to do?\n");
    printf("\n1.Add information to existing member \n\n2.Create a new member");
    printf("\n\n3.Search a member\n\n4.Display all members\n\5.End the program\n\n\n\n");
}

void caseCheck(int genrenum)
{
    switch (genrenum)
    {
        case 000:
            printf("KIDS BOOKS\n_________________________________________\nDora the explorer\nBlues Clues\nGo Diego Go\nThe Little Engine Who Could\n Curious George\n\n");
        break;

        case 111:
            printf("SUPERNATURAL BOOKS\n__________________________________\nLost\nHarry Potter\nAnnie/\n\n");
            break;

        default:
        printf("The data you entered is invalid");
    }
}

/* Notes :
 Good programming practices
 Your '}' should line up when using condition loops
 -> Variables shoul not be started wth a capital letter */

 /*There was something wrong with your while loop.... you did not implement it correctly... Leave off the terminate function for last
  • the code needs to be consistent about indentation. Some compilers will produce code that will crash when the last case is missing the 'break;' statement – user3629249 Feb 27 '15 at 02:14
  • the closing brace '}' of a function does not have a trailing ';' so this code does not compile cleanly. You should enable all the warnings when running the compiler, so it can tell you about such stray characters as that trailing ';' – user3629249 Feb 27 '15 at 02:16
  • the posted code seems to have contained some tabs, which is messing up the formatting of the code. different people, different compilers, etc can use different widths for indentation and for tab widths. strongly suggest never have tabs in source code and use a readable indent width (2 will not show up when using other than mono width fonts. so I suggest using 4 spaces. – user3629249 Feb 27 '15 at 02:19
  • Do you have a specific question about this code? – Jeffrey Bosboom Feb 27 '15 at 02:20
  • suggest that data declared in the file scope, that will not be accessed by other files be always using the 'static' modifier (in C) – user3629249 Feb 27 '15 at 02:21
  • the header file 'conio.h' is not portable and is not available in most environments. suggest not using it in any environment, especially if the code is to be portable between different machines/OSs. – user3629249 Feb 27 '15 at 02:23
  • functions that will have no parameters should be prototyped something like this: 'void menu ( void );' and the actual function should be similar to: 'void menu ()'' – user3629249 Feb 27 '15 at 02:26
  • regarding this line: 'scanf("%d",&New);' 1) keywords (with the capitalization modified) from the 'C++ language should not be used in C programs, because a C++ compiler will can complain and they can be confusing for the viewer (me for instance) suggest using a different (and meaningful) name. 2) the returned value from scanf() should always be checked to assure the input/conversion operation was successful. similar considerations should be given to calls to fscanf and family. – user3629249 Feb 27 '15 at 02:29
  • the returned value from calls to fopen should always be checked to assure the operation was successful (!= NULL) – user3629249 Feb 27 '15 at 02:31
  • suggest using 'getchar()' rather than 'getch()' for portability – user3629249 Feb 27 '15 at 02:35
  • regarding these two lines: 'printf("These are the books under this genre\n\n"); printf("______________");' why call system("cls"); immediately after printing? Due to the call to system(), the user is unlikely to see more than a flash and given that the second printf format string does not end in a '\n', it is unlikely that the user will see anything at all from those two calls to printf() – user3629249 Feb 27 '15 at 02:40
  • this line: 'scanf("%s",book);' has no proceeding printf() to let the user know what they need to input. So the user will be looking at a blank screen with no idea what to do next. BTW: using '%s" without any length modifier means the user can (eventually will) enter enough text to overflow the input buffer (in this case book[]) this overflow results in undefined behaviour and can/will lead to a seg fault event – user3629249 Feb 27 '15 at 02:41
  • when checking the 'New' value, suggest using a switch() statement and placing the whole thing in a loop, so invalid values can be recognized (by the default: case) and the '5' input can be clearly acted upon. – user3629249 Feb 27 '15 at 02:50
  • I don't get what is the question. If you are looking an algo to search into your file, you can look http://en.wikipedia.org/wiki/Trie – Ôrel Feb 27 '15 at 07:19

0 Answers0