My simple database program (help needed)

This is a discussion on My simple database program (help needed) within the C Programming forums, part of the General Programming Boards category; I haven't been studying c programming for that long but over the christmas break i was asked to code a ...

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    2

    My simple database program (help needed)

    I haven't been studying c programming for that long but over the christmas break i was asked to code a simple database program.

    Code:
    /*-------------Library Headers--------------*/
    
    
    #include <stdio.h>
    #include <stdlib.h>
    /*-----------function prototypes-------------*/
    
    
    void menu(void);
    void add(void);
    void search(void);
    void delete(void);
    
    
    
    
    struct {
           int student_no;
           char firstname[15];
           char surname[15];
           int mark;
    } student;
    /*--------------Main------------------------*/
    int main()
    {
        FILE *pFile;
        pFile = fopen("database.txt","w");
        
        system("CLS");
        menu();
        getch();
        return 0;   
    }
        
        
    /*---------------Function menu-----------------*/
    void menu()
    {
    int choice;
         printf("[1] Enter a student\n");
         printf("[2] Search a student by first name\n");
         printf("[3] Delete a student\n");
         printf("[4] Exit program\n");
         scanf("%d", &choice);
         
         switch(choice){
                        case 1: 
                             add();
                             break;
                        
                        case 2: 
                             search();
                             break;
                                
                        case 3: 
                             delete();
                             break;
                                
                        case 4: 
                             exit(0);
                             break;
                        
                        default: 
                             printf("Incorrect entry ");
                             menu();
                             break;
                        }
    }
    
    
    /*---------------Function Add-----------------*/
    void add()
    {
         FILE *pFile;
         
         char answer;
         int student_no;
         
         system("cls");
         
         printf("First Name: ");
         scanf("%s", student.firstname);
         printf("Surname: ");
         scanf("%s", student.surname);
         printf("\nAge: ");
         scanf("%d", &student.mark);
         
         pFile = fopen("database.txt","w");
         fprintf(pFile, "\n%s\t%s\t%d\t", student.firstname, student.surname, student.mark);
         fclose(pFile);
         
         printf("A student has been added successfully\n");
         printf("Are there any more people? \n[1] for yes \n[0] for no\n");
         scanf("%d", &answer);
         
         if (answer == 1)
         {
          add();           
         }
         if (answer == 0)
         {
          menu();           
         }
       
         
    }
    
    
    /*---------------Function Search -----------------*/
    void search()
    {
         int found, length;
         char studentname [15];
         FILE *pFile;
         
         printf("Enter student first name: ");
         scanf("%s", &studentname);
         printf("Searching for %s\n",studentname);
         
         found = 0;
         
         if ((pFile=fopen("database.txt","r"))==NULL)
         {
          printf("File is empty/missing");                                           
         }
         else
         {
             while(!feof(pFile) && found == 0)
             {
              fscanf(pFile,"\n%s\t%s\t%d",student.firstname, student.surname, student.mark);
              length = strlen (student.firstname);
              found == 1;                   
             }    
         }
         if (found)
         {
          printf("Record found.\n");
          printf("First Name: %s\nSurname: %s\nMark: %d \n",student.firstname, student.surname, student.mark);           
         }
         else
         {
          printf("not found");    
          getch();
         }
         
         
         
    }
    /*---------------Function Delete-----------------*/
    void delete()
    {
         
         
         
         
    }
    Problem 1: Looking at the file after compiling and adding a record, only occasionally something would show up

    Problem 2: when i search for the record that is there the program stops responding and exits...

    all help is appreciated

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    You may want to see about increasing your compiler warning level, if you're getting warnings you need to fix them. Here are the warnings and errors I got when I compiled your code:

    main.c||In function ‘main’:|
    main.c|26|warning: variable ‘pFile’ set but not used [-Wunused-but-set-variable]|
    main.c||In function ‘add’:|
    main.c|94|warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘char *’ [-Wformat=]|
    main.c|77|warning: unused variable ‘student_no’ [-Wunused-variable]|
    main.c||In function ‘search’:|
    main.c|117|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[15]’ [-Wformat=]|
    main.c|130|warning: format ‘%d’ expects argument of type ‘int *’, but argument 5 has type ‘int’ [-Wformat=]|
    main.c|131|error: implicit declaration of function ‘strlen’ [-Wimplicit-function-declaration]|
    main.c|131|warning: incompatible implicit declaration of built-in function ‘strlen’ [enabled by default]|
    main.c|132|warning: statement with no effect [-Wunused-value]|
    main.c|143|error: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration]|
    main.c|112|warning: variable ‘length’ set but not used [-Wunused-but-set-variable]|
    ||=== Build finished: 2 errors, 8 warnings (0 minutes, 0 seconds) ===|
    Jim

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    2
    How do I increase the compiler warning level? I use Dev C++

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    30
    Side note, since you're using strlen() you ought to include string.h, and getch() doesn't seem to have a prototype declaration. Are you able to successfully compile a binary off your pasted code?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Your use of recursion here is troubling, the add function should not be calling add or menu in this manner. Your menu function should have a loop instead of relying on being called recursively by add.

    Your main function opens your file in write mode without closing it. Then, the add function again opens this file. This second fopen command should fail but you're not checking the status of the fopen command for success/failure so you try using a file pointer that is most likely invalid.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple program needed......
    By Jeffery Goggin in forum C Programming
    Replies: 7
    Last Post: 05-26-2011, 11:02 PM
  2. Simple C++ program - help needed
    By Kinnison in forum C++ Programming
    Replies: 9
    Last Post: 10-07-2007, 11:21 AM
  3. simple database program error
    By kaijuu in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2007, 06:45 AM
  4. Help needed. Simple C++ program.
    By Sridar in forum C++ Programming
    Replies: 20
    Last Post: 01-28-2005, 05:57 PM
  5. very simple database program
    By another_newbie in forum C Programming
    Replies: 6
    Last Post: 05-24-2004, 06:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21