Closed. This question is off-topic。它当前不接受答案。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
刚开始使用OOP,但它并没有很有趣。我正在编写一个程序,用户可以从菜单中选择3个选项(1.全部打印2.更改得分3.退出)。我想打印出三样东西,每个学生的名字,他们的百分比和分数(从文件中读取分数和名字)。问题是百分比不断打印出垃圾数据。因此,我进行了一些调试,结果发现,当我读完每个学生的分数时,其读入的垃圾数据的额外值正在破坏计算。我试图修复它,但是还没有运气。下面的代码是所有帮助和提示的赞赏,我还将发布调试的IMG和发现存储在scores [i]中的垃圾数据。
想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
5年前关闭。
刚开始使用OOP,但它并没有很有趣。我正在编写一个程序,用户可以从菜单中选择3个选项(1.全部打印2.更改得分3.退出)。我想打印出三样东西,每个学生的名字,他们的百分比和分数(从文件中读取分数和名字)。问题是百分比不断打印出垃圾数据。因此,我进行了一些调试,结果发现,当我读完每个学生的分数时,其读入的垃圾数据的额外值正在破坏计算。我试图修复它,但是还没有运气。下面的代码是所有帮助和提示的赞赏,我还将发布调试的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] .setNumscores(numstu);它应该是newStu [i] .setNumscores(numscores);代替
关于c++ - setPercent函数正在打印垃圾数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21923776/
10-08 22:56