Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Sep 18th, 2012  |  syntax: None  |  size: 4.98 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /********************************************************************
  2.  
  3.  * CSCI124 - Ass1
  4.  
  5.  * Gareth Woodorth - gw305
  6.  
  7.  * ass1.cpp - Contains function definitions for student records database
  8.  
  9.  ********************************************************************/
  10.  
  11. #include <iostream>
  12.  
  13. #include <ostream>
  14.  
  15. #include <fstream>
  16.  
  17. #include <cstdlib>
  18.  
  19.  
  20.  
  21. using namespace std;
  22.  
  23.  
  24.  
  25. // ============== Constants ==========================================
  26.  
  27.  
  28.  
  29. const char cTextFileName[]   = "ass1.txt";
  30.  
  31. const char cBinaryFileName[] = "ass1.dat";
  32.  
  33. const int cMaxRecs = 100;
  34.  
  35. const int cMaxChars = 30;
  36.  
  37. const int cMaxSubjects = 8;
  38.  
  39.  
  40.  
  41. // ============= User Defined types ==================================
  42.  
  43.  
  44.  
  45. enum StatusType{eEnrolled,eProvisional,eWithdrawn};
  46.  
  47. struct SubjectType{ char Code[8]; StatusType Status; int Mark;};
  48.  
  49.  
  50.  
  51. struct StudentRecord{
  52.  
  53.         long StudentNo;
  54.  
  55.         char FirstName[cMaxChars];
  56.  
  57.         char LastName[cMaxChars];
  58.  
  59.         int NumSubjects;
  60.  
  61.         SubjectType Subjects[cMaxSubjects];
  62.  
  63. };
  64.  
  65.  
  66.  
  67.  
  68.  
  69. // ============= Global Data =========================================
  70.  
  71.  
  72.  
  73. StudentRecord gRecs[cMaxRecs];
  74.  
  75. int gNumRecs=0;
  76.  
  77.  
  78.  
  79.  
  80.  
  81. // ============= Private Function Prototypes =========================
  82.  
  83.  
  84.  
  85. void PrintRecord(int i);
  86.  
  87. int FindRecord(long StudentNum);
  88.  
  89.  
  90.  
  91. // ============= Public Function Definitions =========================
  92.  
  93.  
  94.  
  95.  
  96.  
  97. void ReadFile()
  98.  
  99. {// Reads data file into array
  100.  
  101.  
  102.  
  103.         ifstream infile;
  104.  
  105.         infile.open (cTextFileName);
  106.  
  107.         if (infile.fail())
  108.  
  109.         {
  110.  
  111.                 cerr << "Cant find data file!\n";
  112.  
  113.                         exit(1);
  114.  
  115.         }
  116.  
  117.         int i = 0;
  118.  
  119.         infile >> gRecs[i].StudentNo;
  120.  
  121.         while(!infile.eof())
  122.  
  123.         {
  124.  
  125.                 infile >> gRecs[i].FirstName;
  126.  
  127.                 infile >> gRecs[i].LastName;
  128.  
  129.                 infile >> gRecs[i].NumSubjects;
  130.  
  131.                 int statustype;
  132.  
  133.                 for ( int j=0; j < gRecs[i].NumSubjects; j++)
  134.  
  135.                 {  
  136.  
  137.                        
  138.  
  139.                         infile >> gRecs[i].Subjects[j].Code;
  140.  
  141.                         infile >> statustype;
  142.  
  143.                         gRecs[i].Subjects[j].Status = static_cast <StatusType> (statustype);
  144.  
  145.                         infile >> gRecs[i].Subjects[j].Mark;
  146.  
  147.                 }
  148.  
  149.  
  150.  
  151.                 i++;
  152.  
  153.                 infile >> gRecs[i].StudentNo;
  154.  
  155.         }
  156.  
  157.  
  158.  
  159.         gNumRecs = i;
  160.  
  161.         infile.close();
  162.  
  163.         cout << gNumRecs << " records read" << endl;
  164.  
  165.  
  166.  
  167. }
  168.  
  169.  
  170.  
  171. void DisplayRecord()
  172.  
  173. {// Displays specified record on screen
  174.  
  175.  
  176.  
  177.         ifstream infile;
  178.  
  179.         int StudentNumber;
  180.  
  181.  
  182.  
  183.         cout << "Enter student number: ";
  184.  
  185.         cin >> StudentNumber;
  186.  
  187.         int i = FindRecord(StudentNumber);
  188.  
  189.         if (i==-1)
  190.  
  191.                 cerr << "Record not found" << endl;
  192.  
  193.         else
  194.  
  195.                 PrintRecord(i);
  196.  
  197.  
  198.  
  199. }
  200.  
  201.  
  202.  
  203. void SaveFile()
  204.  
  205. {// Writes array to text data file
  206.  
  207.        
  208.  
  209.         ofstream ins;
  210.  
  211.         ins.open(cTextFileName);
  212.  
  213.         if (!ins.good())
  214.  
  215.         {
  216.  
  217.                 cerr << "Problem opening data file!\n";
  218.  
  219.                 exit(1);
  220.  
  221.         }
  222.  
  223.         for( int i=0; i < gNumRecs; i++)
  224.  
  225.         {
  226.  
  227.                 ins << gRecs[i].StudentNo;
  228.  
  229.                 ins << gRecs[i].FirstName;
  230.  
  231.                 ins << gRecs[i].LastName;
  232.  
  233.                 ins << gRecs[i].NumSubjects;
  234.  
  235.  
  236.  
  237.                 for(int j=0; j < gRecs[i].NumSubjects; j++)
  238.  
  239.                 {  
  240.  
  241.                         ins << gRecs[i].Subjects[j].Code;
  242.  
  243.                         ins << gRecs[i].Subjects[j].Status;
  244.  
  245.                         ins << gRecs[i].Subjects[j].Mark;
  246.  
  247.                 }
  248.  
  249.         }
  250.  
  251.         ins.close();
  252.  
  253.         cout << gNumRecs << " records saved" << endl;
  254.  
  255.  
  256.  
  257. }
  258.  
  259.  
  260.  
  261. void UpdateRecord()
  262.  
  263. {// updates status or mark of specified subject of specified student number
  264.  
  265.  
  266.  
  267.         int StudentNumber,subcode,markupdate;
  268.  
  269.         char smchange,statusupdate;
  270.  
  271.        
  272.  
  273.         ofstream ins;
  274.  
  275.         ins.open(cTextFileName);
  276.  
  277.  
  278.  
  279.  
  280.  
  281.         cout << "Enter student number: ";
  282.  
  283.         cin >> StudentNumber;
  284.  
  285.         int i = FindRecord(StudentNumber);
  286.  
  287.         if (i==-1)
  288.  
  289.                 cerr << "Record not found!" << endl;
  290.  
  291.         else
  292.  
  293.         {
  294.  
  295.                 PrintRecord(i);
  296.  
  297.                 cout << " enter a subject code: ";
  298.  
  299.                 cin >> subcode;
  300.  
  301.                 if (!i== -1)
  302.  
  303.                 {
  304.  
  305.                         cout << "Select status or mark(s/m): ";
  306.  
  307.                         cin >> smchange;
  308.  
  309.                         switch(smchange)
  310.  
  311.                         {
  312.  
  313.                                 case 's':       cout << " select new status:" << endl;
  314.  
  315.                                                         cout << "e: enrolled " << endl;
  316.  
  317.                                                         cout << "p: provisional " << endl;
  318.  
  319.                                                         cout << "w: withdrawn " << endl;
  320.  
  321.                                                         cout << "status: ";
  322.  
  323.                                                         cin >> statusupdate;
  324.  
  325.                                                         break;
  326.  
  327.                                
  328.  
  329.                                 case 'm':       cout << "select new mark:" << endl;
  330.  
  331.                                                         cin >> markupdate;
  332.  
  333.                                                         markupdate=gRecs[i].Subjects[j].Mark;
  334.  
  335.                                                         ins << markupdate;
  336.  
  337.                                                         break;
  338.  
  339.                                                
  340.  
  341.                                 default: cerr << "invalid input (s/m)" << endl;
  342.  
  343.                         }
  344.  
  345.                 }
  346.  
  347.  
  348.  
  349.                 else
  350.  
  351.                         cerr << "Subject code not found!" << endl;
  352.  
  353.        
  354.  
  355.         }
  356.  
  357.         ins.close();   
  358.  
  359. }
  360.  
  361.  
  362.  
  363.  
  364.  
  365. // ============= Private Function Definitions =========================
  366.  
  367.  
  368.  
  369. void PrintRecord(int i)
  370.  
  371. { // Prints record i on screen
  372.  
  373.  
  374.  
  375.  
  376.  
  377.         cout << "student number: \t" << gRecs[i].StudentNo << endl;
  378.  
  379.         cout << "first name: \t\t" << gRecs[i].FirstName << endl;
  380.  
  381.         cout << "last name: \t\t" << gRecs[i].LastName << endl;
  382.  
  383.         cout << "number of subjects: \t" << gRecs[i].NumSubjects << endl;
  384.  
  385.  
  386.  
  387.         for (int j=0; j < gRecs[i].NumSubjects; j++)
  388.  
  389.         {  
  390.  
  391.                 cout << gRecs[i].Subjects[j].Code << "\t";
  392.  
  393.  
  394.  
  395.                 switch(gRecs[i].Subjects[j].Status)
  396.  
  397.                 {
  398.  
  399.                         case 0: cout << "enrolled\t";
  400.  
  401.                                         break;
  402.  
  403.  
  404.  
  405.                         case 1: cout << "provisional\t";
  406.  
  407.                                         break;
  408.  
  409.  
  410.  
  411.                         case 2: cout << "withdrawn\t";
  412.  
  413.                                         break;
  414.  
  415.  
  416.  
  417.                         default: cerr << " status unresolved\t";
  418.  
  419.                 }
  420.  
  421.                
  422.  
  423.  
  424.  
  425.                 cout << gRecs[i].Subjects[j].Mark << "\n";
  426.  
  427.         }
  428.  
  429.  
  430.  
  431. }
  432.  
  433.  
  434.  
  435. int FindRecord(long StudentNo)
  436.  
  437. {// returns index of matching record or -1 if not found
  438.  
  439.  
  440.  
  441.         for (int i=0; i < gNumRecs; i++)
  442.  
  443.         {
  444.  
  445.                 if (gRecs[i].StudentNo == StudentNo)
  446.  
  447.                         return i;
  448.  
  449.         }
  450.  
  451.  
  452.  
  453.         return -1;
  454.  
  455. }