Thread: comparing character arrays from a class and and the interface

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    15

    comparing character arrays from a class and and the interface

    ive become frustrated with this application im trying to solve and would be most grateful if someone could help.

    im designing an application which will enable a user to enter marks for a module and work out their result.
    Data for a module is stored in a file.

    Most of it is yet to be finished but my problem currently is to do with the find_module() function. The function in the class Module called bool is_match has to compare the module code entered by the user with the module codes read into a character array from the text file. If theres a match i.e my module code entered exists the function should return true to my find_module().

    text file
    Code:
    3sfe408
    Software Development Principles
    12
    1
    Tut1 2
    Tut2 2
    Tut3 2
    Tut4 2
    Tut5 2
    Tut6 2
    Tut7 2
    Tut8 2
    Tut9 2
    Tut10 2
    Cswk1 10
    Cswk2 20
    50
    50
    30
    30
    40
    30
    3sfe403
    System Software
    4
    1
    Test1 10
    Test2 10
    Test3 10
    Test4 10
    40
    60
    30
    30
    40
    30
    3sfe407
    Introduction to Internet Programming
    9
    1
    Tut1 2
    Tut2 3
    Tut3 2
    Tut4 2
    Tut5 2
    Tut6 3
    Tut7 3
    Tut8 2
    Cswk 20
    40
    60
    30
    30
    40
    30
    3isy453
    Modelling in Information Systems
    2
    1
    Cwk1 10
    Cwk2 30
    40
    60
    30
    30
    40
    30
    header file

    Code:
    // file module.h
    
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    const int MODULE_NAME_SIZE = 50; // e.g. System Software
    const int CSWK_NAME_SIZE = 10;   // e.g Cswk1
    const int MODULE_CODE_SIZE = 10; // e.g. 3sfe403
    const int MAX_COURSEWORKS = 20;  // arbitrary limit?
    
    class Module
    {
    private:
       char code[MODULE_CODE_SIZE]; // module code
       char name[MODULE_NAME_SIZE]; // module name
       int n_cswks; // number of courseworks
       int n_exams; // number of exams (0 or 1)
       char names[MAX_COURSEWORKS][CSWK_NAME_SIZE]; // names of courseworks
       int weightings[MAX_COURSEWORKS]; // weighting of each cswk
       int c_wtg; // overall coursework weighting
       int ex_wtg; // overall exam weighting
       int c_thold; // coursework threshold
       int ex_thold; // exam threshold
       int passmrk; // passmark
       int refer_mark; // referral mark
    public:
       Module();
       bool has_exam(); // returns true if module has an exam
       int num_cswks(); // returns number of cswks
       bool is_match(char id[]); // returns true if id matches code
       char* get_name(); // returns name of module as a string
       char* get_cswk_name(int i); // name of coursework i as a string
       int get_cswk_wtg(int i); // weighting for coursework i
       int get_c_wtg(); // returns coursework weighting
       int get_ex_wtg(); // returns exam weighting
       int get_c_thold(); // returns cswk threshold
       int get_ex_thold(); // returns exam threshold
       int get_passmrk(); // returns minimum pass mark
       int get_refer_mark(); // returns minimum referral mark
       void get(ifstream&); // gets data from file
    };
    module.cc file

    Code:
    // file module.cc
    
    #include <iostream>
    #include "module.h"
    #include <cstring>
    
    using namespace std;
    
    Module::Module()
    {
       int n_cswks =0; // number of courseworks
       int n_exams=0; // number of exams (0 or 1)
       int c_wtg=0; // overall coursework weighting
       int ex_wtg=0; // overall exam weighting
       int c_thold=0; // coursework threshold
       int ex_thold=0; // exam threshold
       int passmrk=0; // passmark
       int refer_mark=0; // referral mark
    }
    
    bool Module::is_match(char id)
    {
      return(strcmp(total,id)==0);
    }     
    
    char* Module::get_name()
    {
       return "DUMMY MODULE NAME"; // UNFINISHED
    }
    
    char* Module::get_cswk_name(int i)
    {
       return "CSWK"; // UNFINISHED
    }
    
    int Module::get_cswk_wtg(int i)
    {
       return 0; // UNFINISHED
    }
    
    int Module::get_c_wtg()
    {
       return 0; // UNFINISHED
    }
    
    int Module::get_ex_wtg()
    {
       return 0; // UNFINISHED
    }
    
    int Module::get_c_thold()
    {
       return 0; // UNFINISHED
    }
    
    int Module::get_ex_thold()
    {
       return 0; // UNFINISHED
    }
    
    int Module::get_passmrk()
    {
       return 0; // UNFINISHED
    }
    
    int Module::get_refer_mark()
    {
       return 0; // UNFINISHED
    }
    
    bool Module::has_exam()
    {
       return false; // UNFINISHED
    }
    
    int Module::num_cswks()
    {
       return 0; // UNFINISHED
    }
    
    void Module::get(ifstream& infile)
    {
       char dummy[80];
       infile.getline(code,MODULE_CODE_SIZE) ;
       infile.getline(name,MODULE_NAME_SIZE) ;
       infile >> n_cswks >> n_exams;
       for (int i = 0; i < n_cswks; i++)
       {
          infile >> names[i] >> weightings[i];
       }
       infile >> c_wtg >> ex_wtg >> c_thold >> ex_thold >> passmrk >> refer_mark;
       infile.getline(dummy,80);
    }
    main file

    Code:
    // file demo.cc
    
    #include "module.h"
    #include <iostream>
    
    using namespace std;
    
    enum Result { PASS, FAIL, REFER}; // used in Result
    
    const int MAX_NUMBER_OF_MODULES = 50; // arbitrary
    
    int find_module(Module modules[],int n,char code[])
    {
      int result;
      int i =0;
      while(i < n)
        {
          if(modules[i].is_match(code))
    	{
    	  result = i;
    	  break;
    	}
          i++;    
        }
    
       // if found returns index of position of code in array of modules
       // if not found returns -1
       // n is number of modules read from input file
       return result;
    }
    
    void get_marks(Module mod, int marks[])
    {
       // gets user entry of marks for module mod
          cout << "Enter marks for " << mod.get_name() << endl;
          cout << "Coursework marks ... " << endl;
          int num_cswks = mod.num_cswks();
          for (int i = 0; i < num_cswks; i++)
          {
             cout << " Enter % mark for " << mod.get_cswk_name(i) << " : ";
             cin >> marks[i];
          }
    
          if(mod.has_exam())
          {
             cout <<"Enter exam mark : ";
             cin >> marks[num_cswks];
          }
    }
    
    void result(Module mod, int marks[])
    {
       // it displays the results (pass / fail / list of referrals
       // mod is the module we are working on
       // marks[] is the array of marks - both coursework and exam
       // the exam mark is there is one is the last mark in marks[]
    }
    
    
    int main()
    {
       int marks[MAX_COURSEWORKS]; // actual coursework marks
       char module_code[MODULE_NAME_SIZE];
       Module m[MAX_NUMBER_OF_MODULES];
       
       // open the data file if possible - abort if not found
       ifstream infile("module.txt");
       if (infile.fail())
       {
          cerr << "Unable to open input file.";
          exit(1);
       }
    
       // read data from file and count modules
       int i = 0;
       while(infile)
       {
          m[i].get(infile);
          if (infile)
          {
             i++;
          }
    
          if(i == MAX_NUMBER_OF_MODULES) break; // just in case!
       }
    
       int n_modules = i; 
    
       // get data from user
       cout << "Enter module code : ";
       cin >> module_code;
       
       // check if the module exists
       int num = find_module(m,n_modules,module_code);
       
       if(num >= 0)
       {  // module exists - num is array index
          get_marks(m[num],marks);
          result(m[num],marks);
       }
       else
       {
          cout << "Module " << module_code << " not found\n";
       }
       return 0;
    }
    at the moment when i run the program it displays "not found"
    all i want it to do at this early stage is return the index of position of code in array of modules to main.

    im not sure if theres a problem with the way im comparing it in the bool is_match function or if theres a problem in the find_module function with what im sending to be compared, or BOTH.

  2. #2
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    Code:
    bool Module::is_match(char *id)
    {
        return (strcmp(code,id)==0);
    }

Popular pages Recent additions subscribe to a feed