C++ setPercent函数正在打印垃圾数据

C++ setPercent函数正在打印垃圾数据,c++,arrays,class,oop,C++,Arrays,Class,Oop,刚开始使用OOP,但它不是很有趣。我正在编写一个程序,用户可以从菜单中选择3个选项。打印全部2个。改变分数3。退出“全部打印”功能中的Im。我想打印出三件事,每个学生的名字,他们的百分比和分数,分数和名字从文件中读取。问题是百分比一直在打印垃圾数据。所以我做了一些调试,我发现当我读入每个学生的分数时,它读入了额外的垃圾数据值,这破坏了计算。我试着把它修好了,但运气不好。下面所有的帮助和提示都是代码,我还将发布调试的IMG和存储在scores[i]中的垃圾数据 #include <iostr

刚开始使用OOP,但它不是很有趣。我正在编写一个程序,用户可以从菜单中选择3个选项。打印全部2个。改变分数3。退出“全部打印”功能中的Im。我想打印出三件事,每个学生的名字,他们的百分比和分数,分数和名字从文件中读取。问题是百分比一直在打印垃圾数据。所以我做了一些调试,我发现当我读入每个学生的分数时,它读入了额外的垃圾数据值,这破坏了计算。我试着把它修好了,但运气不好。下面所有的帮助和提示都是代码,我还将发布调试的IMG和存储在scores[i]中的垃圾数据

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

class Student
{
private: 
    string name;
    int *scores;
    int numstu;
    int numscores;
    int maxscore;
    double percent;

public:
    //Mutator
    void setName(string inName) {name = inName;}
    void setNumstu(int iNum) {numstu = iNum;}
    void setNumscores(int sNum) {numscores = sNum;}
    void setMaxscore(int mNum) {maxscore = mNum;}
    void setScores(int *list);
    void setPercent ();

    //Accessor
    string getName () const {return name;}
    int getNumScores () const {return numscores;}
    int getNumStu () const {return numstu;}
    int getMaxScore () const {return maxscore;}
    double getPercent () const {return percent;}
    int *getScoreslist () const {return scores;}

    //constructor
    //Student();
};


void Student::setScores(int *list)
{
    scores = new int[numscores];
    for (int i = 0; i < numscores; i++)
    {
        scores[i] = list[i];
    }
};

void Student::setPercent() 
{   
    double sum = 0;
    //debugging shows scores is being filled with garbage data
    for (int i = 0; i < numscores; i++)
    {
        cout << scores[i] << endl;
    } 

    for(int i = 0; i < numscores; i++)
    {   
        sum = sum + scores[i];
    }
    //cout << sum;
    percent = (sum/maxscore) * 100.0;
    sum = 0;

    //cout << percent;
};


Student *fillArr(int &numstu, int &numscores, int &maxscore);
void printAll(Student *stuArr, int numstu, int numscores);

int main() 
{

    int numstu;
    int numscores;
    int maxscore;
    int choice;


    Student *stuArr;
    stuArr = fillArr(numstu, numscores, maxscore);
    if(stuArr == 0)
    {
        cout << "Error." << endl;
        return 0;
    }

    cout << "Menu:" << endl;
    cout << "1. Print All" << endl;
    cout << "2. Change Score" << endl;
    cout << "3. Quit" << endl;
    cin >> choice;

    do 
    {
        if(choice == 1)
        {
            printAll(stuArr, numstu, numscores);
        }
        else if (choice == 2)
        {
            cout << "Change Score" << endl;
        }
        else if (choice == 3)
        {
            cout << "Good Bye" << endl;
            exit(100);
        }
        else
        {
            cout << "That is not a valid option." << endl;
            return 0;
        }
    } while (choice !=1 && choice !=2 && choice != 3); 

    return 0;
};

Student *fillArr(int &numstu, int &numscores, int &maxscore)
{
    //Opening file and checking if it exists
    ifstream infile;
    infile.open("grades.txt");
    if(!infile)
    {
        cout << "Error Opening file\n";
        return 0;
    }
    string temp;

    //Reding in number of students, number of scores, and maximum score
    infile >> numstu >> numscores >> maxscore;

    //Dynamically Allocating new memory for each student 
    Student *newStu = new Student[numstu];
    infile.ignore();

    for (int i = 0; i < numstu; i++)
    {
        getline(infile, temp);
        newStu[i].setName(temp);
        newStu[i].setMaxscore(maxscore);
        newStu[i].setNumscores(numstu);

        int *list = new int[numscores];

        for (int z = 0; z < numscores; z++)
        {
            infile >> list[z];      
        };

        newStu[i].setScores(list);

        infile.ignore();    
    };


    return newStu;
    infile.close();
};

void printAll(Student *stuArr, int numstu, int numscores)
{
    cout << "Name\t" << "\tGrade\t" << "\tScores\t" << endl;

    for (int i = 0; i < numstu; i++)
    {
        //calling setpercent mutator
        stuArr[i].setPercent();
        cout << setprecision(1) << fixed << left;

        //printing out each name and percent of each student 
        cout << setw(20) << stuArr[i].getName() << setw(19) << stuArr[i].getPercent() << setw(20);

        printing out the scores of each student works correctly here for some odd reason
        const int *ptr = stuArr[i].getScoreslist();
        for (int z = 0; z < numscores; z++)
        {
            cout << left;
            cout << setw(4) << ptr[z];
        }
        cout << endl;
    }
};

在此行中发现错误:newStu[i].setNumsCoresunmstu;应该是newStu[i].setNumsCoresNumsCoresNumsCores;相反

Soo。。构造器在哪里?你肯定没有遵循3的规则你在调试的时候有没有试着打印出maxscore的值?3的规则?是的,maxscore正在被正确读取。如果不看到输入文件,很难判断出哪里出了问题。看起来您的分数数组比您正在读取的分数数长。