Thread: Help with our Program pleaseee :(

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    Help with our Program pleaseee :(

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<conio.h>
    #include<stdlib.h>
    
    
    typedef struct
    {
            char FN[50],LN[50], MI;
    
    
    } fullname;
    
    
    typedef struct student
    {
          fullname name;
          int ID_no, year;
          char course[30];
          struct student *next;
    } studentinfo;
    
    
    
    
    typedef struct book
    {
            char title[30], genre[20];
            fullname author;
            int ISBN;
            struct book *next;
    }bookinfo;
    //void pass();
    
    
    void student();
    void addStudent(studentinfo *);
    void viewAllRecords(studentinfo *);
    void editRecord(studentinfo *);
    void deleteRecord(studentinfo *);
       /*
    
    
    void book();
    void addbook();
    void viewAllbook();
    void deletebook();
    void editbook();  */
    
    
    void menu();
    
    
    
    
    
    
    int main()
    {
    
    
    
    
       // pass();
        menu();
    
    
    
    
    }
    
    
    
    
    
    
    
    
    /*void pass()
    {
         char password[10];
         int i;
    
    
    
    
         printf("\n Enter Password: ");
    
    
    
    
         for(i=0; i<8;i++)
         {
                  password[i]=getch();
                  printf("*");
    
    
         }
        gets(password);
    
    
    
    
         if( password=="sexygirl")
         {
              printf("Correct Password!\n");
    
    
    
    
         }
         else
         {
             printf("Password did not match! \n");
             printf("Enter Password again!\n");
             pass();
    
    
         }
    
    
    }
    
    
    */
    
    
    void menu()
    {
     int a;
    
    
    
    
    
    
         printf("\nEnter the number of your choice: \n [1]student \n [2]book \n [3]search \n [4]Exit\n");
         scanf("%d",&a);
    
    
         while( a!=3)
         {
                switch(a)
                {
                case 1:clrscr();student();getch();break;
                case 2:clrscr();book();getch();break;
                //case 3:search();getch();break;
                default:exit(1);
    
    
                }
    
    
         }
    }
    
    
    void student()
    {   studentinfo *head;
        int s;
    
    
        printf("\nEnter the number of your choice: \n [1]add student \n [2]delete student \n [3]view all student\n [4]edit student\n [5]Main Menu\n");
        scanf("%d",&s);
    
    
          while( s!=6)
         {
                switch(s)
                {
                case 1:clrscr();addStudent(head);getch();break;
                case 2:clrscr();deleteRecord(head);getch();break;
                case 3:clrscr();viewAllRecords(head);getch();break;
                case 4:clrscr();editRecord(head);getch();break;
                case 5:clrscr();menu();getch();break;
    
    
                }
    
    
         }
    
    
    }
    
    
    
    
    void addStudent(studentinfo *head)
    {
        studentinfo *p,*temp;
        int i,count;
        FILE *fp;
    
    
    
    
        if((fp = fopen("data.dat", "ab"))==NULL)
        {
         printf("\nFile not found!");
         exit(1);
        }
    
    
    printf("Enter Number of Students to be added:\n");
    scanf("%d",&count);
    printf("\n===========================================================\n");
    
    
    head = (studentinfo *) malloc(sizeof(studentinfo));
    p=head;
    
    
     for(i=0;i<count;i++)
    {
    
    
    
    
      temp=(studentinfo*)malloc(sizeof(studentinfo));
      printf("\nEnter your first name: \n");
      fflush(stdin);
      gets(p->name.FN);
    
    
      printf("Enter your Middle Initial: \n ");
      scanf("%c",&p->name.MI);
    
    
      printf("Enter your last name: \n");
      fflush(stdin);
      gets(p->name.LN);
    
    
      printf("Enter ID Number: \n ");
      scanf("%d",&p->ID_no);
    
    
      printf("Enter year level: \n ");
      scanf("%d",&p->year);
    
    
      printf("Enter your course: \n");
       fflush(stdin);
      gets(p->course);
    
    
        printf("Successfully Added!\n");
        fwrite(p,sizeof(*p),1,fp);
     }
     p->next=NULL;
     fclose(fp);
    
    
    
    
     clrscr();
     student();
    
    
    }
    
    
    
    
    void viewAllRecords(studentinfo *head)
    {
         studentinfo *p;
         FILE *fp;
         int i;
          if((fp = fopen("data.dat", "rb"))==NULL)
        {
         printf("\nFile not found!");
         exit(1);
        }
        head = (studentinfo *) malloc(sizeof(studentinfo));
        p = head;
    
    
     while(p!=NULL)
        {
    
    
          fread(p,sizeof(*p),1,fp);
          printf("\nName: %s",p->name.FN);
          printf("\t%c.",p->name.MI);
          printf("\t%s",p->name.LN);
          printf("\nID NO: %d",p->ID_no);
          printf("\tCourse & Year: %s-%d\n",p->course,p->year);
          p = p->next;
    
    
    
    
          }
    
    
        fclose(fp);
          student();
    }
    
    
    void deleteRecord(studentinfo *head)
    {
         studentinfo *p,*temp;
        int num;
        FILE *fp;
    
    
    
    
        if((fp = fopen("data.dat", "ab"))==NULL)
        {
         printf("\nFile not found!");
         exit(1);
        }
    
    
        printf("Enter ID number of student to be deleted\n");
         scanf("%d", &num);
    
    
         head = (studentinfo *) malloc(sizeof(studentinfo));
         p=head;
    
    
         while(p->next->ID_no != num)
         p=p->next;
    
    
         if (p->next != NULL)
         {
                 temp=p->next;
                 p->next=temp->next;
            free(temp);
          }
         fclose(fp);
         student();
         }
    
    
    void editRecord(studentinfo *head)
    {
         studentinfo *p,*temp;
        int num,a;
        FILE *fp;
    
    
    
    
        if((fp = fopen("data.bnf", "ab+"))==NULL)
        {
         printf("\nFile not found!");
         exit(1);
        }
    
    
         head = (studentinfo *) malloc(sizeof(studentinfo));
         p=head;
         printf("Enter ID number of the student to be edited\n");
         scanf("%d", &num);
         while(p->next !=NULL && p->next->ID_no != num)
         p=p->next;
         temp=p->next;
    
    
    
    
            printf("Enter the number of the item to be edited: \n [1]Student's First Name \n [2]Student's Middle Name \n [3]Student's Last Name\n [4]ID Number \n [5]Year Level \n [6]Course \n [7]Exit\n");
         scanf("%d",&a);
    
    
         while( a!=7)
         {
                switch(a)
                {
                case 1:
                     free(temp->next->name.FN);
                     printf("Enter New First Name:\n");
                     gets(temp->next->name.FN);
                     fscanf(fp,"\n%s",temp->next->name.FN);
                     getch();
    
    
                     break;
                case 2:
                     printf("Enter New Middle Name:\n");
                     scanf("%c", &temp->next->name.MI);
                     fscanf(fp,"\n%c",temp->next->name.MI);
                     break;
                case 3:
                     free(temp->next->name.LN);
                     printf("Enter New Last Name:\n");
                     gets(temp->next->name.LN);
                     fscanf(fp,"\n%s",temp->next->name.LN);
                     break;
                case 4:
                     printf("Enter New ISBN:\n");
                     scanf("%d",&temp->next->ID_no);
                     fscanf(fp,"\n%d",temp->next->ID_no);
                     break;
                case 5:
                     printf("Enter New Last Name:\n");
                     scanf("%d",temp->next->year);
                     fscanf(fp,"\n%d",temp->next->year);
                     break;
                case 6:
                     free(temp->next->course);
                     printf("Enter New Course");
                     gets(temp->next->course);
                     fscanf(fp,"\n%s",temp->next->course);
                case 7:
                     exit(1);
                     break;
    
    
                }
    
    
    
    
         }
    
    
         clrscr();
         fclose(fp);
         student();
    }
    our problem is it only displays 1 record on viewAllRecords() function and delete and edit functions are not working please we need help badly

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So, so many problems. I'm not trying to be rude, but you really have a whole lot of work to do to get this in a decent, working state. I think you should review the fundamentals too, malloc/free, linked lists and file IO. Honestly, I would start from scratch. That sounds scary, but if you follow a good process, it may actually save you time at this point. See below for more info.

    Your indentation and formatting are crap. If your code is hard to read, it's easy to make mistakes and hard to find or fix them. See here: SourceForge.net: Indentation - cpwiki. Note, this includes smashing several statements onto one line. Spread things out a bit so you can actually see what your code is doing.

    I'm guessing you wrote 428 lines of code before you did any real testing. That's the wrong way to develop. Instead:

    1. Understand what the problem is asking
    2. Figure out how you (a human) would approach this (think old-school, paper and pencil). If you can't do it, you can't program a computer to do it
    3. Sketch out your plan in pseudo code
    4. Convert that pseudo code into real code little by little. That means write 5-10 lines, compile it (with error level set to maximum), fix all errors and warnings, then test it. Don't move on to the next section until all previous sections work perfectly.

    Code:
    $ make student
    gcc -g -Wall -std=c99 -o student student.c -lpthread -lm -lpq
    student.c:3:18: fatal error: conio.h: No such file or directory
    compilation terminated.
    make: *** [student] Error 1
    conio.h is an outdated and non-standard header. You should avoid it wherever possible. That also means no using getch or clrscr (by the way, clearing the screen all the time is really annoying to the user).

    I removed conio.h and all calls to getch and clrscr so I can compile your code. Doing so gives me:
    Code:
    $ make student
    gcc -g -Wall -std=c99 -o student student.c -lpthread -lm -lpq
    student.c: In function ‘menu’:
    student.c:146:13: warning: implicit declaration of function ‘book’ [-Wimplicit-function-declaration]
    student.c: In function ‘addStudent’:
    student.c:191:21: warning: variable ‘temp’ set but not used [-Wunused-but-set-variable]
    student.c: In function ‘viewAllRecords’:
    student.c:270:10: warning: unused variable ‘i’ [-Wunused-variable]
    student.c: In function ‘editRecord’:
    student.c:389:18: warning: format ‘%c’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat]
    student.c:400:18: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]
    student.c:404:18: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
    student.c:405:18: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat]
    student.c: In function ‘student’:
    student.c:171:30: warning: ‘head’ may be used uninitialized in this function [-Wuninitialized]
    /tmp/ccUjHlcp.o: In function `addStudent':
    /home/cagarvin/sandbox/cprogramming/student.c:223: warning: the `gets' function is dangerous and should not be used.
    /tmp/ccUjHlcp.o: In function `menu':
    student.c:(.text+0x51): undefined reference to `book'
    collect2: ld returned 1 exit status
    make: *** [student] Error 1
    • You call a function named book(), but you never defined that function.
    • You need to learn how to use fscanf. Read the documentation and Google for examples. You must use the right format specifier for the type of data you are reading in.
    • You need to properly initialize head before using it.
    • Don't use gets. Read this link. Use fgets instead, but remember to remove the new line (use the strchr method seen here)

    And more errors the compiler didn't/couldn't catch:

    • Don't use fflush(stdin), it results in undefined behavior. See here.
    • Don't use magic numbers. Define constants and use them everywhere you can.
    • Use a loop to repeat menus/actions instead of a giant mess of mutual recursion. Recursion is an excellent tool for some things, but not in this case (e.g. having student() call addStudent(), which then calls student() is bad). Here it just wastes memory and makes debugging difficult.
    • Having a main function that does nothing but call another function that does everything main should is just silly. Move the contents of student() into main() and get rid of your student() function.
    • You can not safely write a linked list to a file. When you read the data back, they are not guaranteed to read back into the same addresses, so the next pointer will be incorrect. You will have to read one node at a time, and make sure you properly insert it into a list or update the next pointer.
    • Don't cast malloc. Read this: FAQ > Casting malloc - Cprogramming.com.
    • You're calling free on things you never malloc'ed. That's bad, it results in undefined behavior meaning anything can happen, though in reality it will probably just crash.

    That is all I could come up with. I am saturated, I can no longer keep track of all the things wrong with your code.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    so tell me then, how could we possible edit or delete some records that have already been saved into the file? and how can we implement linked list when we are going to retrieve data from files?
    ughhh this is so frustrating, 2 days till our deadline for this project and we will fail this subject please help us we dont want to fail and reenroll for this subject again

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I already mentioned one way, to write the nodes with the pointer data (which will be invalid on read), but then to fix the pointer data as you read the node back out. This is actually pretty easy, though IMO not the best way.

    A few other suggestions:
    Write the data to the file in human-readable format (with fprintf), and read it back with fscanf. This has the benefit of being similar to the way the user would enter data, except the data comes from a file instead of the keyboard/user.

    Decouple your studentinfo node from your list. Make a studentinfo struct that only contains student data, no next pointer. Then, have a separate struct (e.g. studentlist) for your list that contains a studentinfo (or pointer to a studentinfo) and a next pointer (to another studentlist struct). Then, you traverse your list, and call fwrite with just the studentinfo data. That way you don't have to worry about fixing the pointers.

    Two days sounds like not much time, but with a good methodology like I mentioned, this could be done in maybe 5 or 6 hours of solid work.

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Quote Originally Posted by anduril462 View Post
    I already mentioned one way, to write the nodes with the pointer data (which will be invalid on read), but then to fix the pointer data as you read the node back out. This is actually pretty easy, though IMO not the best way.

    A few other suggestions:
    Write the data to the file in human-readable format (with fprintf), and read it back with fscanf. This has the benefit of being similar to the way the user would enter data, except the data comes from a file instead of the keyboard/user.

    Decouple your studentinfo node from your list. Make a studentinfo struct that only contains student data, no next pointer. Then, have a separate struct (e.g. studentlist) for your list that contains a studentinfo (or pointer to a studentinfo) and a next pointer (to another studentlist struct). Then, you traverse your list, and call fwrite with just the studentinfo data. That way you don't have to worry about fixing the pointers.

    Two days sounds like not much time, but with a good methodology like I mentioned, this could be done in maybe 5 or 6 hours of solid work.
    can u give us an example please? please?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Albert Saludaga View Post
    can u give us an example please? please?
    Umm...of which method? I'm certainly not doing all of them. I can give you an example, but if I do, it wont be something you can copy-paste for your homework assignment. That would prevent you from learning, which is the purpose of homework. Whatever it ends up being, I'm going to bed for the night, so it will be tomorrow morning at the earliest.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-11-2012, 12:25 AM
  2. Replies: 1
    Last Post: 03-03-2009, 04:47 PM
  3. need complexity pleaseee!!!
    By kwdikosno8 in forum C Programming
    Replies: 17
    Last Post: 09-10-2007, 09:17 AM
  4. Replies: 5
    Last Post: 08-16-2007, 11:43 PM
  5. Replies: 18
    Last Post: 11-13-2006, 01:11 PM

Tags for this Thread