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 14-Sep-2007, 07:45
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point

Problem about using class member function to read text file


The question required to read some values from a text file. a example of text file is following(Student ID, 3 Assigments scores and final exam score);

s0000001 12 12 12 170
s9100002 9 7 10 135
s8000001 10 10 10 179
s9005004 6 9 6 150
s7000001 10 10 10 136

Now , I have a header file below,but I have no idea how to write member function to read those values from text file.
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
};

#endif

Asking for anyone can help!! cheers!
Last edited by admin : 15-Sep-2007 at 08:16. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #2  
Old 14-Sep-2007, 13:53
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Problem about using class member function to read text file


Quote:
Originally Posted by allenfanwenyuan
Now , I have a header file below,but I have no idea how to write member function to read those values from text file.
Is the problem in the part of writing the method or in the part of reading the file or in the part of parsing the data in the file or..?

CPP / C++ / C Code:
    void setUnit(float ***1, float ***2, float ***3, float ex);
Guess this is some kind of a forum filter issue? Doesn't make much sense otherwise..

Just for curiosity, I don't get this part
CPP / C++ / C Code:
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
Does this mean that 3 * 12 = 30? Does it also mean that 180 * 0.7 + 32 * 0.3 = 100?

Or does it mean that total score = 30 * (assSum / 30) + 70 * (exam / 180)?
Last edited by Kimmo : 14-Sep-2007 at 14:25.
  #3  
Old 14-Sep-2007, 22:27
allenfanwenyuan allenfanwenyuan is offline
Junior Member
 
Join Date: Mar 2007
Posts: 38
allenfanwenyuan is an unknown quantity at this point

Re: Problem about using class member function to read text file


Kimmo
The question is to get and calculate each student's final score which including 3 assignment scores(contributes 30%) and final exam score(contributes 70). If total assignment score is greater than 30 , it will be counted 30, if less than 30 ,like 25 ,the total assignment score will be remain 25. The exam score will be scale down to 70, that means , 180 =70, 90=35. So the maximum of final score is still 100.


I am confused how to write definition to get each assignment score and final exam score from a text file. I wrote some belwo ,but seems not corret.
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;
     exam=ex;
     }
                    
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;
}   
float unitType::getTotal() const
{
      return total;
}   
Last edited by admin : 15-Sep-2007 at 08:17. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #4  
Old 15-Sep-2007, 06:20
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 229
Kimmo has a spectacular aura aboutKimmo has a spectacular aura about

Re: Problem about using class member function to read text file


Quote:
Originally Posted by allenfanwenyuan
I wrote some belwo ,but seems not corret.
Yes, well clearly it is not correct since there isn't anything that even possibly could read anything from a file. I get the feeling you're trying to get someone else to write the program for you.

Now when I look at that header file, it seems to me that the intention is not to read the data in a method. The intention is just to pass the data (sounds reasonable, no use making unitType a database manager.)

So try something like this in your main()

Code:
open file if file not open, quit read data from file pass data to unitType object process

If the data in file is guaranteed to be ordered as you showed, then reading the data could be done with something like follows:

CPP / C++ / C Code:
while (ifstreamName)
{
    string temp;
    float score1, score2, score2, exam;

    ifstreamName >> temp >> score1 >> score2 >> score3 >> exam;

// Do something with the data
}
 
 

Recent GIDBlogWelcome to Baghdad 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
PERL: Problem with Read from file Reny Miscellaneous Programming Forum 1 07-Aug-2007 07:44
Major newbie problem cynack MS Visual C++ / MFC Forum 1 08-Apr-2007 11:25
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Database Program goldfish C Programming Language 6 13-May-2006 13:12
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07

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

All times are GMT -6. The time now is 16:23.


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