char calculateGrade(double studentAverage) { char grade; if(studentAverage < = 100&& studentAverage> = 90) grade =''A''; else if(studentAverage< 90&& studentAverage > = 80) grade =''B''; else if(studentAverage< 80&& studentAverage> = 70) grade =''C''; else if(studentAverage< 70&& stude ntAverage> = 60) grade =''D''; else if(studentAverage< 60&& studentAverage> = 0) grade =''F''; else cout<< 无效成绩 <<结束; 返回等级; }What is wrong with the output? Are there random characters in it? Arethe calculated values incorrect? Does the file end prematurely? Be morespecific in your question.Here is what is wrongI will post it hereStudent Test1 Test2 Test3 Test4 Test5 Average GradeBalto85.00 83.00 77.00 91.00 76.00 82.40 BMickey80.00 90.00 95.00 93.00 48.00 81.20 BMinnie79.00 81.00 11.00 90.00 73.00 66.80 DDoc92.00 83.00 30.00 69.00 87.00 72.20 CGoofy23.00 45.00 96.00 38.00 59.00 52.20 FDuckey60.00 85.00 45.00 39.00 67.00 59.20 FGrumby27.00 31.00 52.00 74.00 83.00 53.40 FSunny93.00 94.00 89.00 77.00 97.00 90.00 APiggy79.00 85.00 28.00 93.00 82.00 73.40 CPluto85.00 72.00 49.00 75.00 63.00 68.80 DPluto85.00 72.00 49.00 75.00 63.00 68.80 DClass average is:69.85and here is my original fileBalto 85 83 77 91 76Mickey 80 90 95 93 48Minnie 79 81 11 90 73Doc 92 83 30 69 87Goofy 23 45 96 38 59Duckey 60 85 45 39 67Grumby 27 31 52 74 83Sunny 93 94 89 77 97Piggy 79 85 28 93 82Pluto 85 72 49 75 63Why is Pluto printed out twice?and for the void function, That s the requirement of the question.//************************************************** *******************************// Write a program that reads a student''s name together with his or hertest *// scores. The program should then compute the average test score foreach student*// and assign the appropriate grade. The grade scale is as follows:*// 90-100, A; 80-89, B ; 70-79, C; 60-69, D; 0-59, F.*// Your program must use the following functions:*//*//a. A void function, calculateAverage, to determine the average of thefive *// test score for each student. Use a loop to read and sum the fivetest score *// This function doesn''t output the average test score. That taskmust be done *// in the function main)*//*// b. A value return function, calculateGrade, to determine and returneach *// student''s grade. This function doesn''t output the grade. Thattask must be *// done in the function main)*//************************************************** ******************************#include <iostream.h>#include <stdlib.h>#include <iomanip>#include <string>#include <fstream>using namespace std;void calculateAverage(double test1, double test2, double test3, doubletest4, double test5, double& studentAverage);char calculateGrade(double grade);int main(){string studentName;int numberOfStudents = 0;double classAverage = 0;double studentAverage = 0;double totalAverage = 0; //To add the average of all studentaverageschar grade;double test1, test2, test3, test4, test5;ifstream inFile; // input stream variable for the student fileofstream outFile; // output stream variableinFile.open("c:\\studentFile.txt");outFile.open("c:\\Student_out.txt");cout << setfill('' '') <<setiosflags(ios:: left) << setw(11) <<"Student" << setw(10) << "Test1"<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"<< setw(8) << "Test5"<< setw(8) << "Average" << setw(8) << "Grade" <<endl;outFile << setfill('' '') <<setiosflags(ios:: left) << setw(11) <<"Student" << setw(10) << "Test1"<< setw(8) << "Test2" << setw(8) << "Test3" << setw(8) << "Test4"<< setw(8) << "Test5"<< setw(8) << "Average" << setw(8) << "Grade" << endl;if (!inFile){cout << "Unable to open the file." <<endl;return 1;}while(inFile){outFile.setf(ios::fixed, ios::floatfield);outFile.setf(ios::showpoint);outFile << setprecision(2);inFile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5;calculateAverage(test1, test2, test3, test4, test5,studentAverage);grade = calculateGrade(studentAverage);cout << setfill('' '') << setiosflags(ios::left) << setw(11) <<studentName << setw (10) << test1<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<grade<<endl;outFile << setfill('' '') << setiosflags(ios::left) << setw(11) <<studentName << setw (10) << test1<< setw(8) << test2 << setw(8) << test3 << setw(8) << test4<< setw(8) << test5 << setw(9) << studentAverage << setw(8) <<grade<<endl;totalAverage = totalAverage + studentAverage;numberOfStudents++;//classAverage = totalAverage / numberOfStudents;}classAverage = totalAverage / numberOfStudents;outFile << endl << setprecision(2)<< "Class average is:" <<classAverage << endl;inFile.close();outFile.close();system("PAUSE");return 0;}//function to calculate the averagevoid calculateAverage(double test1, double test2, double test3, doubletest4, double test5, double& studentAverage){studentAverage = static_cast<double>(test1 + test2 + test3 + test4+ test5) / 5.0;}char calculateGrade(double studentAverage){char grade;if (studentAverage <= 100 && studentAverage >= 90)grade = ''A'';else if (studentAverage < 90 && studentAverage >= 80)grade = ''B'';else if (studentAverage < 80 && studentAverage >= 70)grade = ''C'';else if (studentAverage < 70 && studentAverage >= 60)grade = ''D'';else if (studentAverage < 60 && studentAverage >= 0)grade = ''F'';elsecout << "Invalid grade " << endl;return grade;} 这篇关于IO流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-24 02:31