for some reason it is reading in the same line twice and it doesn't print out the grade. everything else is correct, if someone can help me thanks
this is my code....
Expand|Select|Wrap|Line Numbers
- #include <cstdlib>
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- using namespace std;
- ifstream in_file;
- //structure that holds student record information.
- typedef struct records{
- string name;
- float quizes[7];
- float projects[6];
- float exams[2];
- float labs[14];
- };
- const int num_quizes = 7;
- const int num_exams = 2;
- const int num_projects = 6;
- const int num_labs = 14;
- const float quizes_pts = 70;
- const float exams_pts = 200;
- const float projects_pts = 270;
- const float labs_pts = 1400;
- void get_scores(ifstream& infile, int num_scores, float scores[]);
- double get_average(float scores[], int num_scores, float tot_pts, float& average,int counter,float& current_score);
- void total_average(float& student_average,float ave1,float ave2,float ave3,float ave4);
- void grades_display(float ave1,float ave2,float ave3,float ave4,float total,char& grade,char& grade2);
- int main()
- {
- cout << "No. Name Quiz Project Exam Lab Total Grade"
- << endl;
- cout << "--- --------- ----- ---------- ------ ----- ------- -----"
- << endl;
- cout << endl;
- records grades;
- ifstream in_file;
- // Define a file input stream and open the file
- in_file.open("record.txt"); //opening file
- if (in_file.fail())
- {
- // Failed to open the file (file doesn't exist or isn't readable)
- cout << "Could not open file: "<< "\n";
- exit(1);
- }
- float average = 0;
- int counter = 1;
- float current_score = 0;
- while(!in_file.eof())
- {
- //get string from the file
- in_file >> grades.name;
- cout << counter << " " << grades.name;
- char letter_grade1;
- char letter_grade2;
- float student_average = 0;
- // Repeatedly get characters from the file
- get_scores(in_file, num_quizes, grades.quizes); //function for inputing info in struct.
- get_average(grades.quizes, num_quizes, quizes_pts, average,counter,current_score); //function for average
- float quiz_average = average;
- get_scores(in_file, num_projects, grades.projects); //input project scores
- get_average(grades.projects, num_projects, projects_pts, average,counter,current_score);
- float project_average = average;
- get_scores(in_file, num_exams, grades.exams); //input exam scores
- get_average(grades.exams, num_exams, exams_pts, average,counter,current_score);
- float exams_average = average;
- get_scores(in_file, num_labs, grades.labs); //input lab scores
- get_average(grades.labs, num_labs, labs_pts, average,counter,current_score);
- float labs_average = average;
- total_average(student_average,quiz_average,project_average,exams_average,labs_average);
- grades_display(quiz_average,project_average,exams_average,labs_average,student_average,letter_grade1,letter_grade2);
- counter++;
- }
- // Close the file
- in_file.close();
- return 0;
- }
- //function that takes in parameters to input scores in array
- void get_scores(ifstream& in_file, int num_scores, float scores[])
- {
- for (int i = 0; i < num_scores; i++)
- {
- in_file >> scores[i];
- }
- }
- //funtion that takes the average of the inputed scores
- double get_average(float scores[], int num_scores,float tot_pts,float& average,int counter,float& current_score)
- {
- float a = 0;
- average = 0;
- for (int i = 0; i < num_scores; i++)
- {
- a = scores[i] + a;
- }
- current_score = a + current_score;
- average = ((a)/(tot_pts));
- average = (average * 100);
- while(counter > 2)
- {
- average = (average/100);
- }
- return average;
- }
- //function to take total average for each member
- void total_average(float& student_average,float ave1,float ave2,float ave3,float ave4)
- {
- student_average = ((ave1 + ave2 + ave3 + ave4)/4);
- }
- //function to output letter grade and output scores
- void grades_display(float ave1,float ave2,float ave3,float ave4,float total,char& grade,char& grade2)
- {
- if((100 >= total)&&(total >= 93))
- {
- grade = 'A';
- grade2 = ' ';
- if((93 > total)&&(total >= 90))
- grade = 'A';
- grade2 = '-';
- if((90 > total)&&(total <= 87))
- grade = 'B';
- grade2 = '+';
- if((87 > total)&&(total <= 83))
- grade = 'B';
- grade2 = ' ';
- if((83 > total)&&(total <= 80))
- grade = 'B';
- grade2 = '-';
- if((80 > total)&&(total <= 77))
- grade = 'C';
- grade2 = '+';
- if((77 > total)&&(total <= 73))
- grade = 'C';
- grade2 = ' ';
- if((73 > total)&&(total <= 70))
- grade = 'C';
- grade2 = '-';
- if((70 > total)&&(total <= 67))
- grade = 'D';
- grade2 = '+';
- if((67 > total)&&(total <= 60))
- grade = 'D';
- grade = ' ';
- if((60 > total)&&(total <= 0))
- grade = 'F';
- grade2 = ' ';
- }
- cout << " " << ave1 << " " << ave2 << " " << ave3 << " " << ave4 << " " << total << " " << grade << grade2 << endl;
- }
smith 9 9.33 8 10 5.5 8 10 20 47.5 47 45 47.5 48 83 87 100 98 96 100 98 92 88
96 92 86 92 94 100 96
this is what is output......
No. Name Quiz Project Exam Lab Total Grade
--- --------- ----- ---------- ------ ----- ------- -----
1 smith 85.4714 94.4444 85 94.8571 89.9433
2 smith 85.4714 94.4444 85 94.8571 89.9433