【问题标题】:Not sure how to pass parameters from class to constructor in main()不确定如何在 main() 中将参数从类传递给构造函数
【发布时间】:2013-09-11 16:46:03
【问题描述】:

我的任务是从毕业簿文本文件中读取每个学生的课程、期中和期末成绩以及学生 ID 和姓名的平均值。到目前为止,代码能够正确编译和计算分数,但它不会读取学生 ID 和名字和姓氏。

我很确定原因是 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");
    };
};

【问题讨论】:

  • 这段代码为我正确读取了名字和姓氏。我强烈建议你minimize this code;您很可能会在此过程中发现错误,如果您不这样做,我们要做的工作就会少很多。此外,当您开发新功能时,请单独进行,不要试图让它与许多其他代码一起工作。

标签: c++ parameters constructor fstream


【解决方案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 &amp;readStudent) 中直接访问类成员,您应该为所有成员执行此操作,但您说分数系统正在工作,所以我不理会它。

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 2015-10-08
    • 2023-04-04
    • 2015-10-11
    • 1970-01-01
    相关资源
    最近更新 更多