我正在创建一个程序,它将从文件中读取并在屏幕和文件中输出数据。我在添加总体平均值和总计数之前就已经工作了,但现在它只是崩溃而在编译过程中没有错误。
我搜索了类似的主题,但无法找到任何有助于我修复这些错误的内容。任何帮助表示赞赏。
编辑:更新了代码,其中包含一些建议的更改。现在只是崩溃没有错误。
#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();
};
答案 0 :(得分:2)
您需要解决一些语法问题:
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;
答案 1 :(得分:1)
printGrade
以overallAvg
为参数。但它从不使用参数,而是将变量重新分配给:
overallAvg = gradeSum / count;
gradeCalc()
有同样的问题。实际上,这些函数似乎都做了所有相同的计算。
您还使用未初始化的变量调用函数。
如果overallAvg
应该是输出参数,则需要将其作为参考参数。
void printGrade(int studentID, int count, double midterm, double finalExam, double researchPaper, double groupProject, double participation, double studentAvg, double &overallAvg, double gradeSum)
表达式错误是由于这些行:
cout << endl
<< "Total # of Students: " << count;
<< "Overall Class Average: " << overallAvg;
第二行末尾的分号结束cout
,因此第三行以无效运算符开头。摆脱那个分号。
cout << endl
<< "Total # of Students: " << count
<< "Overall Class Average: " << overallAvg;
这也可能会修复有关未使用变量的警告,因为您在此输出语句中使用了该变量。
答案 2 :(得分:-1)
这取决于编译器。使用-Wno-unused-parameter
标志进行编译。应该这样做。