我正在制作一个程序,该程序将从文件读取并在屏幕和文件中输出数据。在添加总体平均和总计数之前,我已经使它工作了,但是现在它崩溃了,编译过程中没有错误。
我已经搜索了类似的线程,但是找不到任何可帮助我修复这些错误的内容。任何帮助表示赞赏。
编辑:更新了代码,并建议了一些更改。现在只是崩溃瓦特/没有错误。
#include <iostream>
#include <string>
#include <limits>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;
void gradeCalc(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double& overallAvg, double gradeSum);
void printGrade(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double& overallAvg, double gradeSum);
int main()
{
int studentID;
int count = 0;
double midterm;
double finalExam;
double researchPaper;
double groupProject;
double participation;
double studentAvg;
double overallAvg;
double gradeSum;
gradeCalc(studentID, count, midterm, finalExam, researchPaper, groupProject, participation, studentAvg, overallAvg, gradeSum);
printGrade(studentID, count, midterm, finalExam, researchPaper, groupProject, participation, studentAvg, overallAvg, gradeSum);
return 0;
}
void printGrade(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double& overallAvg, double gradeSum)
{
ifstream infile;
ofstream outfile;
infile.open("grades.dat");
outfile.open("grades.txt");
outfile << "STUDENT GRADING REPORT: ";
outfile << fixed << showpoint << setprecision(2);
while (!infile.eof()) {
infile >> studentID >> midterm >> finalExam >> researchPaper >> groupProject >> participation;
if (studentID > 1 && studentID <= 999) {
outfile << endl
<< "Student ID: " << studentID << ' ' << "Midterm: " << midterm << ' ' << "Final Exam: " << finalExam << ' ' << "Research Paper: " << researchPaper << ' ' << "Group Project: " << groupProject
<< "Participation: " << participation << ' ' << "Student Average: " << studentAvg;
}
overallAvg = gradeSum / count;
outfile << endl
<< "Total # of Students: " << count
<< "Overall Class Average: " << overallAvg;
}
infile.close();
outfile.close();
};
void gradeCalc(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double& overallAvg, double gradeSum)
{
//midterm = .25
//finalExam = .25
//researchPaper = .20
//groupProject = .20
//participation = .10
ifstream infile;
ofstream outfile;
infile.open("grades.dat");
while (!infile.eof()) {
infile >> studentID >> midterm >> finalExam >> researchPaper >> groupProject >> participation;
studentAvg = ((midterm * .25) + (finalExam * .25) + (researchPaper * .20) + (groupProject * .20) + (participation * .10));
overallAvg = gradeSum / count;
if (studentID > 1 && studentID <= 999) {
count++;
gradeSum += studentAvg;
cout << endl
<< fixed << showpoint << setprecision(2) << "Student ID: " << studentID << ' ' << "Average: " << studentAvg;
}
cout << endl
<< "Total # of Students: " << count
<< "Overall Class Average: " << overallAvg;
}
infile.close();
};
最佳答案
您需要解决一些语法问题:printGrade()
:
outfile << endl
<< "Total # of Students: " << count; //<-- bad semicolon
<< "Overall Class Average: " << overallAvg;
gradeCalc()
: cout << endl
<< "Total # of Students: " << count; //<-- bad semicolon
<< "Overall Class Average: " << overallAvg;
关于c++ - 函数C++中未使用参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46942783/