GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Mar-2010, 10:36
kingnuddy kingnuddy is offline
New Member
 
Join Date: Feb 2010
Posts: 25
kingnuddy is an unknown quantity at this point

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, but i have no idea ow to start it. I would appreciate it if this question could be done and i use it as an example to work others. Thank you very much.
QUESTION

Quote:
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.
  #2  
Old 04-Mar-2010, 12:40
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 827
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Records and files


You would presumably start by creating a data structure to hold the student information. Then, you would create an array of 10 students in your program that you would load from the file at the beginning of the program and save back to the file at the end of the program. You would need to create a menu with user options.

CPP / C++ / C Code:
#include <stdio.h>
#include <ctype.h>

struct Student
{
  int ID;
  char LastName[32]; 
  char FirstName[32];
  int DateOfBirth; // This could be of the form YYYYMMDD. For example someone born on December 21st, 1990 would have a value of 19901221
  char Address[32];
  char TelephoneNumber[11]; // a 10-digit string
  char ProgramPursued[32];   
};


char GetUserOption()
{
  char option = 'I'; // 'I' for Invalid

  while(option == 'I')
  {
    // Print the menu items
    printf("\n");
    printf("Choose one of the following options:\n[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;
}


// students must hold 10 students
void LoadStudents(Student students[])
{
  // TODO: load students from file
}


// students must hold 10 students
void SaveStudents(Student students[])
{
  // TODO: save students to file
}


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':
        // TODO: Let the user update a record
        break;

      case 'P':
        // TODO: Print the students to the screen
        break;

      case 'S':
        // TODO: Let the user search for a student
        break;

      case 'E':
        looping = 0; // exit the loop
        break;
    }
  } 

  // Save the students to the file
  SaveStudents(students);

  return 0;
}
  #3  
Old 11-Mar-2010, 18:30
kingnuddy kingnuddy is offline
New Member
 
Join Date: Feb 2010
Posts: 25
kingnuddy is an unknown quantity at this point

Re: Records and files


thanks for the help. i wonder how comes i dont get the notification in my email.
I should complete this right?
  #4  
Old 12-Mar-2010, 09:28
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 827
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Records and files


Quote:
Originally Posted by kingnuddy
I should complete this right?
It would be good practice and should help you learn.
  #5  
Old 15-Mar-2010, 12:53
kingnuddy kingnuddy is offline
New Member
 
Join Date: Feb 2010
Posts: 25
kingnuddy is an unknown quantity at this point

Re: Records and files


yea but the condition that is suppose to to go in each block im not sure of the syntax of how to write them
  #6  
Old 15-Mar-2010, 13:32
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 827
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Records and files


Well, it might help to write the pseudo-code for what you want to do as a comment before beginning:
CPP / C++ / C Code:
// Lets user find a student and display information.
void LetUserSearchForStudent()
{
  // Get the ID from the user to search for
  
  // Search for that ID

  // If it was found, display that student
}
If you start on it, we can help you with questions you may have.
  #7  
Old 15-Mar-2010, 14:24
kingnuddy kingnuddy is offline
New Member
 
Join Date: Feb 2010
Posts: 25
kingnuddy is an unknown quantity at this point

Re: Records and files


so after i write the pseudocode i should translate it to C?
  #8  
Old 15-Mar-2010, 15:27
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 827
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Records and files


Quote:
Originally Posted by kingnuddy
so after i write the pseudocode i should translate it to C?
Yes. I believe it helps you keep the "big picture" in mind. So, in this example, you would now add this:

CPP / C++ / C Code:
// Lets user find a student and display information.
void LetUserSearchForStudent()
{
  // Get the ID from the user to search for
  int id = GetIDFromUser();
  
  // Search for that ID

  // If it was found, display that student
}

// Gets an "ID" from the user
int GetIDFromUser()
{
  int id = 0;
  printf("Enter the ID: ");
  scanf("%d", &id);
  return id;
}
  #9  
Old 18-Mar-2010, 07:19
kingnuddy kingnuddy is offline
New Member
 
Join Date: Feb 2010
Posts: 25
kingnuddy is an unknown quantity at this point

Re: Records and files


still working on it kinda difficult, Im stuck at the part that said store records into files.
  #10  
Old 18-Mar-2010, 07:36
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 827
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Records and files


Here is one way to do it. Let us know if you have questions.
CPP / C++ / C Code:
// students must hold 10 students
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....here we will use comma-separated values
    fprintf(fp, "%d,%s,%s,%d,%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);  
}
 
 

Recent GIDBlogR for statistics by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you keep track of all the source files, etc? earachefl C++ Forum 2 21-Sep-2007 14:09
Help with creating files in c molatelo C Programming Language 1 28-Apr-2007 07:20
Search All Records In Any State and Country Online! james18 Member Announcements, Advertisements & Offers 0 29-May-2006 01:03
Background check Unlimited Lifetime Membership Allenhan Member Announcements, Advertisements & Offers 0 20-Mar-2006 02:50

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:36.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.