问题描述
输出文件不想退出。你能帮助我吗?我曾经尝试过,直到没有错误。有什么我想念的吗?我还是个初学者。我需要帮助。
这个问题要求编写一个程序来保存记录并对50名学生进行统计分析。对于每个学生,您将拥有一个10位数的ID,2个测验,2个作业,2个实验室测试,1个测试,1个项目和1个期末考试。学生数据将存储在一系列学生结构中。这个结构有以下字段:
i)字符串:id
ii)3个双数组:测验,屁股,labTest
iii)4双变量:test,proj,final,total
iv)1个char变量:grade。
最多可容纳50名学生。
输入是从文本文件中读取的。文件中的每一行都包含学生ID和2个测验,2个作业,2个实验室测试,1个测试,1个项目和1个期末考试的分数。你的课程必须计算总分并为每个学生分配一个分数
我尝试过:
the output file does not want to exit out . can you help me? i had tried until got no error only . is there anything thai i have miss? and i still a beginner . i need help.
the question asks to Write a program to keeps records and performs statistical analysis for a class of 50 students. For each student you will have a 10-digit ID, 2 quizzes, 2 assignments, 2 lab tests, 1 test, 1 project and one final exam. The student data are to be stored in an array of student structures. This structure has the following fields:
i)A string : id
ii)3 double arrays : quiz, ass, labTest
iii)4 double variables : test, proj, final, total
iv)1 char variable: grade.
Provide for up to 50 students.
The input is read from a text file. Each line in the file contains a student ID and the scores for 2 quizzes, 2 assignments, 2 lab tests, 1 test, 1 project and one final exam, in order. Your program must calculate the total scores and assign a grade for each student
What I have tried:
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
#include<cstdlib>
using namespace std;
struct DATA
{
string id;
double quiz[2];
double ass[2];
double labTest[2];
double test;
double proj;
double final;
double total;
double midTerm;
char grade;
};
int main()
{
ifstream inputfile("studentResults.txt", ios::in);
ofstream outputfile("result.txt", ios::out);
if (!inputfile)
{
cerr << "the file could not be opened";
exit(1);
}
if (!outputfile)
{
cerr << "the file could not be opened";
exit(1);
}
cout << setw(10) << "Matrix Num" << setw(10) << "Quiz(5%)" << setw(10) << "Ass(15%)" << setw(10) << "LabTests(20%)" << setw(10)
<< "Project(15%)" << setw(10) << "MidTerm(15%)" << setw(10) << "Final(30%)" << setw(10) << "Total(100%) " << setw(10) << "Grade" << endl;
void studentGrade();
void countGrade();
void outputResult();
DATA student[50]; int i;
for (i = 0;i < 50;i++)
while (inputfile >> student[i].id >> student[i].quiz[0] >> student[i].quiz[1] >> student[i].ass[0] >> student[i].ass[1] >> student[i].labTest[0]
>> student[i].labTest[1] >> student[i].proj >> student[i].midTerm >> student[i].final >> student[i].total >> student[i].grade)
{
cout << student[i].id << student[i].quiz[0] << student[i].quiz[1] << student[i].ass[0] << student[i].ass[1] << student[i].labTest[0]
<< student[i].labTest[1] << student[i].proj << student[i].midTerm << student[i].final << student[i].total << student[i].grade;
}
cout << endl;
}
void studentGrade(DATA student[])
{
for (int i = 0; i < 50; i++)
{
student[i].total = student[i].midTerm + student[i].proj + student[i].final +
((student[i].quiz[0] + student[i].quiz[1]) / 2) + (student[i].ass[0] + student[i].ass[1]) / 2 +
(student[i].labTest[0] + student[i].labTest[1]) / 2;
if (student[i].total >= 90 && student[i].total <= 100)
student[i].grade = 'A';
else if (student[i].total >= 80)
student[i].grade = 'B';
else if (student[i].total >= 60)
student[i].grade = 'C';
else if (student[i].total >= 40)
student[i].grade = 'D';
else
student[i].grade = 'F';
}
}
void countGrade(DATA student[], int[])
{
int i, a = 0, b = 0, c = 0, d = 0, f = 0;
for (i = 0;i < 50;i++) {
if (student[i].grade == 'A')
a++;
else if (student[i].grade == 'B')
b++;
else if (student[i].grade == 'C')
c++;
else if (student[i].grade == 'D')
d++;
else if (student[i].grade == 'F')
f++;
}
}
void outputResult(DATA student[], int g[5])
{
ofstream outputfile("result.txt", ios::out);
if (!outputfile)
{
cerr << "the file could not be opened";
exit(1);
}
int i;
for (int i = 0;i < 50;i++)
{
student[i].quiz[2] = (student[i].quiz[0] + student[i].quiz[1]) / 2 * 0.05;
student[i].ass[2] = (student[i].ass[0] + student[i].ass[1]) / 2 * 0.15;
student[i].labTest[2] = (student[i].labTest[0] + student[i].labTest[1]) / 2 * 0.2;
}
outputfile << setw(10) << "Matrix Num" << setw(10) << "Quiz(5%)" << setw(10) << "Ass(15%)" << setw(10) << "LabTests(20%)" << setw(10)
<< "Project(15%)" << setw(10) << "MidTerm(15%)" << setw(10) << "Final(30%)" << setw(10) << "Total(100%) " << setw(10) << "Grade" << endl;
for (int i = 0;i < 50;i++)
{
outputfile << setw(10) << setw(10) << student[i].id << setw(10) << student[i].quiz[2] << setw(10) << student[i].ass[2]
<< setw(10) << student[i].ass[2] << setw(10) << student[i].labTest[0] << setw(10) << student[i].labTest[1]
<< setw(10) << student[i].proj << setw(10) << student[i].midTerm << setw(10) << student[i].final
<< setw(10) << student[i].total << setw(10) << student[i].grade << endl;
}
outputfile << endl;
outputfile << "Total Students for Grade A : " << g[0] << endl;
outputfile << "Total Students for Grade B : " << g[1] << endl;
outputfile << "Total Students for Grade C : " << g[2] << endl;
outputfile << "Total Students for Grade D : " << g[3] << endl;
outputfile << "Total Students for Grade F : " << g[4] << endl;
outputfile << "\nA : ";
for (i = 0;i < g[0];i++)
outputfile << "* ";
outputfile << "\nB : ";
for (i = 0;i < g[1];i++)
outputfile << "* ";
outputfile << "\nC : ";
for (i = 0;i < g[2];i++)
outputfile << "* ";
outputfile << "\nD : ";
for (i = 0;i < g[3];i++)
outputfile << "* ";
outputfile << "\nF : ";
for (i = 0;i < g[4];i++)
outputfile << "* ";
outputfile.close();
}
推荐答案
studentGrade(student);
countGrade(student, arrayOfGrades);
和
outputResult(student, arrayOfGrades);
(你必须修改 countGrade
实际填写成绩数组,而不是在退出时丢弃信息。)
(and you will have to modify countGrade
to actually fill the grades array instead of discarding the information when it exits).
这篇关于我如何退出输出文件..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!