不知道如何在main()中将参数从类传递给构造函数

时间:2013-09-11 16:46:04

标签: c++ parameters constructor fstream

我的任务是阅读gradbook文本文件并平均每个学生的课程,期中考试和期末考试成绩,以及学生证和姓名。到目前为止,代码能够适当地编译和计算分数,但它不会读取studentID和名字和姓氏。

我很确定原因是main()中的currentStudent变量没有任何参数,并使构造函数使用默认值。但我不确定如何在main()中从类Student中提供currentStudent值。我对解决方案的最好想法是将所有内容从ReadData移动到main(),但是从我的赋值中读取ReadData的描述,我想我在那里拥有我需要的一切:

“一个名为ReadData(istream&)的方法,它读取学生的数据。它按顺序读取ID号(整数),名字和姓氏(字符串),10个程序分数(所有整数)和期中考试考试成绩(也是整数)。如果所有数据都被成功读取,则返回true,否则返回false“

我很抱歉这篇冗长的描述,我只是看看能否有效地解释我的情况。任何帮助或建议将不胜感激。

我只在下面包含了类定义,构造函数,ReadData和main,因为其他一切只是方程式和get / sets我很确定所有工作,而我正试图减少你可爱的人会必须通读。如果有人想看到完整的代码,我会发布其余的代码。

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Student
{
private:
    int studentID;
    string firstName,
    lastName;
    int score[ 10 ];
    int midterm, final;
public:
    Student ( int, string, string );
    bool ReadData ( istream& );
    //fstream WriteData ( ostream& ); // I need to clear up with professor first
    void setStudentID ( int );
    void setFirstName ( string );
    void setLastName ( string );
    void setMidterm ( int );
    void setFinal (int );
    const int getStudentID ( );
    const string getFirstName ( );
    const string getLastName ( );
    const int getMidterm ( );
    const int getFinal ( );
    void setProgramScore ( int, int[ ] );
    int getProgramScore ( int );
    const double ProgramAvg( );
    const double CourseAvg( );
    ~Student( );
};

Student::Student ( int id = 0, string f = "", string l = "" )
{
    setStudentID ( id );
    setFirstName ( f );
    setLastName ( l );
};

bool Student::ReadData( istream &readStudent ) 
{
    int id;
    string first, last;
    int x[ 10 ], mid, fin;

    readStudent >> id >> first >> last;

    for ( int i = 0; i <= 10 - 1; i++ )
    {
        readStudent >> x [ i ];
        setProgramScore( i, x );
    }
    readStudent >> mid >> fin;

    Student studentInfo ( id, first, last );
    setMidterm( mid );
    setFinal( fin );

    if ( readStudent.good( ) )
        return true;
    else
        return false;
};

// getter,setter和之间的计算

int main( )
{
    ifstream readStudent;
    int lineCount = 0;
    double totalProgramAvg = 0
        , totalFinalAvg = 0
        , totalCourseAvg = 0;

    Student currentStudent; 
    readStudent.open ( "gradebook.txt" );
    if ( readStudent.is_open ( ) )
    {

        while ( currentStudent.ReadData ( readStudent ) == true )
        {
            totalProgramAvg += currentStudent.ProgramAvg();
            totalFinalAvg += currentStudent.getFinal();
            totalCourseAvg += currentStudent.CourseAvg();
            cout << currentStudent.getStudentID() << " "
                << currentStudent.getFirstName() << " "
                << currentStudent.getLastName() << " ";
            for ( int j = 0; j < 10; j++ )
                cout << currentStudent.getProgramScore( j ) << " ";
            cout << currentStudent.getMidterm() << " "
                << currentStudent.getFinal() << endl;
            cout <<  totalProgramAvg << " " << totalCourseAvg << endl;
            lineCount++;
        };
    readStudent.close( );

        cout << lineCount << endl << totalProgramAvg / lineCount << "\n" <<     totalFinalAvg / lineCount << "\n" << totalCourseAvg / lineCount;

    system ("pause");
    };
};

1 个答案:

答案 0 :(得分:1)

bool Student::ReadData( istream &readStudent ) 
{
    int id;
    string first, last;
    int x[ 10 ], mid, fin;

    readStudent >> id >> first >> last;

    for ( int i = 0; i <= 10 - 1; i++ )
    {
        readStudent >> x [ i ];
        setProgramScore( i, x );
    }
    readStudent >> mid >> fin;

    Student studentInfo ( id, first, last );
    setMidterm( mid );
    setFinal( fin );

    if ( readStudent.good( ) )
        return true;
    else
        return false;
}; //what?

我没有检查你的其余代码,但这肯定是错误的。

你不应该声明一个新项目Student studentInfo( id, first, last);你正在创建一个新项目,该项目在函数返回时就会死亡。相反,您应该使用id,first,last来修改您所在的当前对象成员this。您已在类标题中为此声明了项目,但随后声明了本地范围变量,使用它们,使用它创建一个新学生,然后在函数返回时它们都将被销毁并且它们超出范围。只需从函数中删除/添加我标记为适当的内容

bool Student::ReadData( istream &readStudent ) 
{
    int x[ 10 ], mid, fin; //if it ain't broke, don't fix it

    readStudent >> studentID >> firstName >> lastName;  //use your class members that you want to hold that data.  

    for ( int i = 0; i <= 10 - 1; i++ )
    {
        readStudent >> x [ i ];
        setProgramScore( i, x );
    }
    readStudent >> mid >> fin;


    setMidterm( mid );
    setFinal( fin );

    if ( readStudent.good( ) )
        return true;
    else
        return false;
}

你可以直接访问类函数Student::ReadData( istream &readStudent)中的类成员,你应该只对所有这些成员进行操作,但你说得分系统正在运行,所以我就把它留下了。

最后,;追踪},如果它像struct,或class,或一堆stuff我不知道,但不是功能定义。

好的,我在项目流程中看到了另一个错误/缺陷:

while ( currentStudent.ReadData ( readStudent ) == true ) { /*stuff*/ }无法正常工作。您的ReadData函数将读取您当前学生的所有数据,但while循环也将尝试这样做。我无法理解结果,但毫无疑问它将是丑陋的。你最好这样使用它:

if(!(currentStudent.ReadData( readStudent)) {
     //Ooops, I failed, what do I do?
}