Thread: Records and Files

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    14

    Question Records and Files

    This topic is really new to me, i was just reading some stuff ahead of my class and saw a practisable question, I have started it but now im really confuse. I would appreciate it if someone cold modify it so i could gain more practise.
    QUESTION
    You are required to create a student management system that will allow you to store and
    update student records. Each student record should consist of an id, a name (fist name
    and last name), date of birth, address, telephone number and the program being pursued.
    Assume 10 records will be stored.
    1. Create and populate an array of records. Store records into file
    2. Allow user to update a record. Note, the user should only manipulate the data
    structure. All changes to the data structure should then be use to overwrite the
    file.
    3. Allow user to search for a record from the data structure.
    4. Print all records from the structure.
    NB. You should create appropriate functions to solve the problem.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    struct student
    {
      char* ID[10];
      char* LastName[10]; 
      char* FirstName[10]; 
      char* DateOfBirth[10];  
      char* Address[10];
      char* TelephoneNumber[10]; 
      char* ProgramPursued[10];   
    };
    
    struct student students[10]={
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a243","Spencer", "Sochelle", "22-05-87", "43_Olympic_Court", "898-0497", "Computer"},         
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    };
    
    
    char GetUserOption()
    {
      char option = 'I'; // 'I' for Invalid
    
      while(option == 'I')
      {
    
        printf("\n");
        printf("Choose one of the following options:\n\t\t\t[u]pdate   [P]rint   [S]earch   [E]xit\n");
        scanf("%c", &option);
    
        switch(toupper(option))
        {
          case 'U':
          case 'P':
          case 'S':
          case 'E':
            break;
          default:
            option = 'I';
            break;
        }
      }
    
      return option;
    }
    
    void LetUserSearchForStudent()
    {
    
      int count;
      char id,f;
     
      
    
      printf("Enter the id you want to search for\n");
      scanf("%c", & id);
      f='b';
      for(count=0; count<10; count++)
      {
    	if(id== student students[count])
    	{
    		print("%c was found at location %d", id, count);
    		count=10;
    		f='a'
    	}
      }
      if (f=='b')
    	printf("%c was not found", id);
    }
    
    void PrintStudents(Student students[])
    {
      fprintf(fp, "%student", students[]);
    }
    
    
    void LoadStudents(Student students[])
    {
      FILE *fp=fopen("records.txt","r");
    }
    
    void UpdateStudents(Student students[])
    {
      FILE *fp=fopen("records.txt","a");
    }
      
    void SaveStudents(Student students[])
    {
      int i;
    
      // Open the file for writing
      FILE *fp = fopen("records.txt", "wb");
      if(fp == NULL) return; 
    
      // Loop through each student
      for(i = 0; i < 10; ++i)
      {
        // Write the student to the file....
        fprintf(fp, "%s,%s,%s,%s,%s,%s,%s\r\n", 
                students[i].ID,
                students[i].LastName,
                students[i].FirstName,
                students[i].DateOfBirth,
                students[i].Address,
                students[i].TelephoneNumber,
                students[i].ProgramPursued
        );    
      }
    
      // Close the file
      fclose(fp);  
    }
    
    }
    
    
    int main()
    {
      Student students[10];
      int looping = 1;
    
      // Load the students from the file
      LoadStudents(students);
    
      // Loop until exit
      while(looping)
      {
        char option = GetUserOption();
        switch(option)
        {
          case 'U':
            UpdateStudents(Student students[]);
            break;
    
          case 'P':
            PrintStudents(Student sudents[]);
            break;
    
          case 'S':
        LetUserSearchForStudent()
           
                 break;
    
          case 'E':
            looping = 0; // exit the loop
            break;
        }
      } 
    
      // Save the students to the file
      SaveStudents(students);
    
      return 0;
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What are you confused about?

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    The program not compiling. I think something is wrong the functions

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    What are the compiler errors?

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    its alot, could u copy the code and modify it for me plz

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ummm NO! You copy the compiler errors and post them here and I will help you fix it. Nobody here is going to do the work for you though buddy.

    Also, compiler errors are usually VERY Explicit in what you need to look for. They even indicate the line you should look at.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    Code:
     C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp In function `void LetUserSearchForStudent()': 
    69 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp expected primary-expression before "students" 
    69 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp expected `)' before "students" 
    71 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp `print' undeclared (first use this function) 
      (Each undeclared identifier is reported only once for each function it appears in.) 
    74 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp expected `;' before '}' token 
    74 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp At global scope: 
    80 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp variable or field `PrintStudents' declared void 
    80 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp `Student' was not declared in this scope 
    81 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp expected `,' or `;' before '{' token 
    86 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp variable or field `LoadStudents' declared void 
    86 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp `Student' was not declared in this scope 
    87 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp expected `,' or `;' before '{' token 
    91 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp variable or field `UpdateStudents' declared void 
    91 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp `Student' was not declared in this scope 
    92 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp expected `,' or `;' before '{' token 
    96 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp variable or field `SaveStudents' declared void 
    96 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp `Student' was not declared in this scope 
    97 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp expected `,' or `;' before '{' token 
    123 C:\Documents and Settings\Nuddy\Desktop\coursework2.cpp expected declaration before '}' token

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Ok first problem, since you are not typedef-ing the struct you need to use "struct Student" everywhere you use Student.

    So just add the word struct before any use of Student. Let me know what the compiler errors are after that.

    example: struct Student students[10];

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    how would u typedef this...caz i doing that topic and im not getting it

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    One way to do it:

    Code:
    typedef struct student
    {
      char* ID[10];
      char* LastName[10]; 
      char* FirstName[10]; 
      char* DateOfBirth[10];  
      char* Address[10];
      char* TelephoneNumber[10]; 
      char* ProgramPursued[10];   
    }STUDENT;
    Then just use:

    STUDENT mystudent;

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    so both the struct and the typedef can work with each other...........

  12. #12
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Typedef can work with anything. Typedef just redefines how you name a particular existing type.

    In the example above you create struct student define it on the spot and rename it to STUDENT.

    But you can also redefine other things, even primitive types:

    typedef int INTEGER;

    And then do:
    INTEGER x = 10;

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    3
    oooo...'.ok wow.....nice, i see what ur saying now

  14. #14
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Nobody has mentioned this problem yet:
    Code:
    /* Each of these members is an array of 10 pointers to type char , and NOT a 10 byte string array  */
    
    struct student
    {
      char* ID[10];
      char* LastName[10]; 
      char* FirstName[10]; 
      char* DateOfBirth[10];  
      char* Address[10];
      char* TelephoneNumber[10]; 
      char* ProgramPursued[10];   
    };
    
    /* So each of these string constants are assigned to the elements of the struct student member "ID" */
    
    struct student students[10]={
    {"a123","Hall", "Rayon", "16-06-87", "43_Olympic_Court", "898-0497", "Computer"},
    ...
    
    /* I may be way off , but I don't think that that is what was intended */
    I think you should just follow along in you class instead of jumping so far ahead that you are guessing.
    Last edited by HowardL; 04-16-2010 at 11:42 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. An imporant question on files and records
    By yashpal in forum C Programming
    Replies: 15
    Last Post: 06-05-2006, 01:33 AM
  3. I Need To Know Some Things That I Can Put Into A Batch File
    By TheRealNapster in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-20-2003, 08:12 PM
  4. How to read Records in X-Base files thru C/c++
    By hskambo in forum C++ Programming
    Replies: 1
    Last Post: 09-18-2003, 02:48 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 02:51 PM