c++ - How to write functions and call them from main -
#include<iostream> #include<iomanip> #include<string> #include<fstream> using namespace std; struct studenttype { string studentfname[20]; string studentlname[20]; string studentdata[20][3]; int testscore[20][5]; char grade[20]; }; studenttype s; int main() { int index; int maxindex; int maxindex1; int coldex; int coldex1; int totalscore[20]; double avscore[20]; double highscore; double highindivscore; maxindex = 0; maxindex1 = 0; coldex1 = 0; ifstream infile; infile.open("test.txt"); for(index = 0; index < 20; index++) { infile >> s.studentfname[index] >> s.studentlname[index] >> s.testscore[index][0] >> s.testscore[index][1] >> s.testscore[index][2] >> s.testscore[index][3] >> s.testscore[index][4]; totalscore[index] = ((s.testscore[index][0]) + (s.testscore[index][1]) + (s.testscore[index][2]) + (s.testscore[index][3]) + (s.testscore[index][4])); avscore[index] = (static_cast<double>(totalscore[index])/5); if(avscore[index]<= 100) { s.grade[index] = 'a'; } if(avscore[index]<= 89.9) { s.grade[index] = 'b'; } if(avscore[index]<= 79.9) { s.grade[index] = 'c'; } if(avscore[index] <= 69.9) { s.grade[index] = 'd'; } if(avscore[index] <= 59.9) { s.grade[index] = 'f'; } } (index = 0; index < 20; index++) { cout << s.studentlname[index] << "," << " " << s.studentfname[index] << " " << s.testscore[index][0] << " " << s.testscore[index][1] << " " << s.testscore[index][2] << " " << s.testscore[index][3] << " " << s.testscore[index][4] << " " << s.grade[index] << endl; } cout << endl; (index = 1; index < 20; index++) (coldex = 0; coldex < 5; coldex++) { if (s.testscore[maxindex1][coldex1] < s.testscore[index][coldex]) { maxindex1 = index; coldex1 = coldex; } } highindivscore = s.testscore[maxindex1][coldex1]; (index = 1; index < 20; index++) { if (avscore[maxindex] < avscore[index]) { maxindex = index; } } highscore = avscore[maxindex]; cout << s.studentfname[maxindex] << " " << s.studentlname[maxindex] << " achieved highest average test score of a: " <<highscore <<endl; cout << s.studentfname[maxindex1] << " " << s.studentlname[maxindex1] << " " << s.testscore[maxindex1][coldex1] << endl; infile.close(); system("pause"); return 0; }
here homework question, know did wrong why need help. how make main function calls. new this. have lab that's got me stumped. assignment reads (emphasis mine):
write programs following , test make sure work. please follow book’s guidelines or style guidelines write code. write program reads students’ names followed test scores given input file. program should output file, output.txt, each student’s name followed test scores , relevant grade.. student data should stored in struct variable of type studenttype, has 4 components: studentfname , studentlname of type string, testscore of type int , grade of type char. suppose class has 20 students. use array of 20 components of type studenttype. program must contain @ least following functions:
- a function read students’ data array.
- a function assign relevant grade each student.
your program should output each student’s name in form: last name followed comma, followed space, followed first name; name must left justified. moreover, other declaring variables , opening input , output files, function main should collection of function calls.
this called refactoring. guess assignment want move in main individual functions. cut , paste whole body of main , move 1 function , call in main. don't think point of assignment though.
instead, read on main , separate/extract procedures. should separate main functions 1 thing i.e. handle input/output, loop, check variable , return letter, etc. these kind of changes not make program easier understand , come later, make easier reuse procedures repeated throughout program.
Comments
Post a Comment