setPercent函数正在打印垃圾数据

时间:2014-02-21 02:02:03

标签: c++ arrays class oop

刚刚开始使用OOP,它并不是很有趣。即时编写程序,用户从菜单中选择3个选项(1.打印全部2.更改分数3.退出)我卡在打印所有功能上。我想打印3个东西,每个学生的名字,他们的百分比和他们的分数(分数和名字从文件中读入)。问题是百分比不断打印出垃圾数据。所以我做了一些调试,我发现当我读取每个学生的分数时,它读取了垃圾数据的额外值,这破坏了计算。我试图解决它,但没有运气。以下是代码的所有帮助和提示,我还将发布我的调试的IMG和我发现存储在得分[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;
    }
};

garbage data being read in(not sure but thats my assumption) also you can see next to it that when i print out the scores in the printAll fucntion they are being printed normally with no garbage data

1 个答案:

答案 0 :(得分:0)

发现此行中的错误:newStu [i] .setNumscores(numstu);它应该是newStu [i] .setNumscores(numscores);代替