GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Sep-2007, 01:01
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point

A class problem, very confused!!


I just start to learn class ,but now i am facing a question which is extremely hard for me.
The question is
You are required to complete a C++ program that reads a specific file with data about students who have completed 3 assignments and the final exam in a unit, and displays top students.

Input: A text file containing students’ information. The information of each student is given in a line with the following format, which includes the student ID and 4 scores of assignment 1, assignment 2, assignment 3 and the final exam in order.

<student ID> <score of assignment 1> <score of assignment 2> <score of assignment 3> <score of final exam>

NOTE:

1. The score of each assignment is in the scope of 0~12 (inclusive). The sum of assignments is in the scope of 0~30(inclusive). E.g., if the sum of 3 assignments is 31 (say 11 + 10 + 10), it is reduced to 30. If the sum is 25 (i.e. no more than 30), it would not be changed.

2. The score of final exam is in the scope of 0~180 (inclusive). To calculate the final total score (in the scope of 0~100 (inclusive)), the exam score should be scaled down to 70%. E.g., the score of 180 is scaled down to 70, and 90 is scaled down to 35.

The program should open once a file with entered filename and read data from it.

Output: The program should display the id and the total score of top students with the best 5 total scores.

This program has two header files and 3 .cpp files, i have done some of them, but look like many of them are incorrect and some syntaxs i can't understand.(especially how to link class member function with my main() program ) I am posting what i have already done there ,hope someone give me some ideas to solve this problem.
There are two header files:
CPP / C++ / C Code:
#ifndef unit_H
#define unit_H

// don't modify this header file
// don't upload this file
// a class for a unit type

using namespace std;

class unitType
{
  public:
    unitType() {assignment1 =0; assignment2 = 0; assignment3 = 0; assSum = 0; exam = 0; total = 0;}; // deafult constructor
    void setUnit(float ***1, float ***2, float ***3, float ex);  // set values of data members
    void calAssSum();     // calculate assSum which is no more than 30
    void calTotal();      // calculate total, which is the sum of assSum (30%) and exam (70%).
    float getAssignment1() const;  // return assignment 1 score
    float getAssignment2() const;  // return assignment 2 score
    float getAssignment3() const;  // return assignment 3 score
    float getAssSum() const;       // return the value of assSum
    float getExam() const;         // return the value of exam
    float getTotal() const;        // return the value of total
         
  private:
    float assignment1, assignment2, assignment3; // scores for 3 assignments - in the scope of [0,12]
    float assSum, exam, total;  // assSum is the sum of 3 assignments score in the scope of [0,30]
                                // exam is the exam score in the scope of [0,180] - it is counted as 70% in total score
                                // total is the total score in the scope of [0,100] - 30% for assignments, 70% for the exam
};
CPP / C++ / C Code:
#ifndef stu_H
#define stu_H

// don't miodify this file
// don't upload this file
// a class for a student type

#include "unitType.h"

using namespace std;

class studentType
{
  public:
    studentType() {id =""; myUnit = unitType();}; // default constructor
    void setStudentType(string ID, unitType u);   // set values of the data members
    string getID() const;
    unitType getUnit() const;
    void setID(string ID);
    void setUNIT(unitType& u);
    void printStudent() const; // display id, assignment1, assignment2, assignment3, assSum, total - useful for dubugging
    void printIDTotal() const; // diaplay id and total - follow the required format
         
  private:
    string id;       // unique id
    unitType myUnit; // scores are in myUnit - another class
};

#endif

There 3 .cpp files
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <string>

#include "unitType.h"

using namespace std;

// implement all member functions of class unitType - check "unitType.h"
void unitType::setUnit(float ***1,float ***2,float ***3,float ex)
{
     assignment1=***1;
     assignment2=***2;
     assignment3=***3;
     
     }
                    
void unitType::calAssSum()
{
    
     assSum=assignment1+assignment2+assignment3;
     if(assSum>30)
     assSum=30;
}               

void unitType::calTotal()
{
     total=assSum+(exam/180)*70;
}

float unitType::getAssignment1() const
{
    
      
     return assignment1;
}
float unitType::getAssignment2() const
{
      return assignment2;
}
float unitType::getAssignment3() const
{
      return assignment3;
}
float unitType::getAssSum() const
{
      return assSum;
}
float unitType::getExam() const
{
      return exam/180*70;
}
float unitType::getTotal() const
{
      return total;
}
CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <string>
#include "unitType.h"
#include "studentType.h"

using namespace std;

// implement member functions of class studentType - check "studentType.h"
void studentType::setStudentType(string ID, unitType u)
{
     id=ID;
     myUnit=u;
}
string studentType::getID() const
{
       return id;
}
studentType unitType::getUnit()
{
                    return myUnit.getAssignment1(),myUnit.getAssignment2(),myUnit.getAssignment3(),
                    myUnit.getExam();
}
void studentType::setID(string ID)
{
     id=ID;
}

void studentType::setUNIT(unitType& u)
{
     myUnit=u;
}
Last edited by LuciWiz : 16-Sep-2007 at 08:04. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 16-Sep-2007, 01:04
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point
Lightbulb

Re: A class problem, very confused!!


CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <vector>
#include "studentType.h"

const int TOP_NUM = 5;

bool readStudents(string fName, vector<studentType>& sList);
// pre:  fName is the filename of student data
// post:
//       this function reads data from the file and insert data to vector sList
//       this function returns false if any error occurs when reading data from the specified file, and otherwise returns true

void printStudents(vector<studentType>& sList, int num);
// pre: sList is the sorted vector of students - sorted by total and id
//      num is the actual number of students to be displayed
//      num may be more than 5 as some top students may have the same total score
//      num may be less than 5 if the input file contains less than 5 students
// post: this function displays the id and total score of top num (say 5 or 7) students in sList 

// you can declare other functions if necessary

using namespace std;

int main()
{
  vector<studentType> studentList;  // don't modify
  int topNum;                       // don't modify
  string fileName;                  // don't modify
  
  // you can declare other variables or class objects here
  
  cout << "Enter the filename of student data: " << flush; // don't modify
  cin >> fileName;                                         // don't modify
  cout << endl;                                            // don't modify
  
  if (!readStudents(fileName, studentList))                // don't modify
    cout << "Input file error." << endl;                   // don't modify
  else                                                     // don't modify
  {
    // add some processing here
    
        
    if (studentList.size() >= 2)                           // don't modify
      cout << "There are " << studentList.size() << " students totally. " << endl << endl; // don't modify
    else                                                                                   // don't modify
      cout << "There is " << studentList.size() << " student totally. " << endl << endl;   // don't modify
    cout << "Top Student List" << endl;                                                    // don't modify
    printStudents(studentList,  topNum);                                                   // don't modify
    // you should initialise topNum before calling this function - the actual number of top students to be displayed
  }
  
  cout << endl;     // don't modify
  system("pause");  // don't modify
  return 0;         // don't modify
}

// provide the implementation of all user-defined functions
Last edited by LuciWiz : 16-Sep-2007 at 08:05. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #3  
Old 16-Sep-2007, 06:13
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 284
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: A class problem, very confused!!


I remember seeing something like this not long ago.

Posting basically the same assignment multiple times doesn't get you any better answers. And posting the same assignment AND being as vague as the previous time REALLY doesn't help much.

Quote:
Originally Posted by allenfanwenyuan
This program has two header files and 3 .cpp files, i have done some of them, but look like many of them are incorrect and some syntaxs i can't understand.(especially how to link class member function with my main() program ) I am posting what i have already done there ,hope someone give me some ideas to solve this problem.
This piece is basically the only in the whole post where you tell anything about the problem.

1. "Looks like many of them are incorrect?" Care to shed some light into what you think is incorrect? And what should that part do that it's not doing.

2. What syntax parts don't you understand?

3. What do you mean by linking class member functions with your main()? If you're using some kind of an IDE and create a project in an IDE, the IDE takes care of linking automatically. If you're not using an IDE, you must link the files manually. (But I assume you know that, since if you must link manually, you are probably also compiling "manually" etc.) Maybe you mean something else?

4. I have some ideas, yes. But I'm expecting some ideas from you too.

CPP / C++ / C Code:
studentType unitType::getUnit()
{
    return myUnit.getAssignment1(),myUnit.getAssignment2(),my Unit.getAssignment3(),
    myUnit.getExam();
}
This is wrong. You can't use the return statement this way. If you want to return multiple values, you have to wrap them in a single object, struct for example. And I don't think this is the idea of the getUnit() method. Your code isn't "getting a unit", your code is "getting the members of a unit." Return a copy of the unit. Isn't that what it says in the header file?

CPP / C++ / C Code:
unitType getUnit() const;    // return a unitType object by value
So even if your return statement worked and your method worked, you would get an error saying something like "no matching function call for plaa plaa", because, if you LOOK at it, there IS NO such method as
CPP / C++ / C Code:
studentType unitType::getUnit();
There is only
CPP / C++ / C Code:
unitType studentType::getUnit() const;

One more advice: Testing. I'm 88% (or more) percent sure you have done no testing at all while writing this stuff.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) 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
Classes and allocating memory BlueFireCO. C++ Forum 13 26-Jul-2007 21:31
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 20:23
Box Class, need help again :( TransformedBG C++ Forum 7 13-Nov-2006 16:11
Problem with Overloaded Constructors and Class within Class jdbrine C++ Forum 2 10-Jul-2006 23:14
a tester class and then some. postage Java Forum 1 06-May-2006 16:48

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

All times are GMT -6. The time now is 06:52.


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