Hello Again,
My first semester of C++ programming has come to an end, and I ask one more time for advice, not for help in the class, but to help me learn from some mistakes i may have made in this script. (Which was the final project of the term) IF anybody could throw me some pointers, move some stuff around, whatever, it would be greatly appreciated. This script works exactly as it was inteded, but as I see it, I am sure there are ways to make it more concise, and efficient. Thanks again for your help in advance,
-- Jeff
Code:
//Jeffrey Gross
//Final Using Arrays
#include <iostream>
#include <iomanip>
#include <fstream> //included so we can utilize the input and output of external files
#include <string> //include so we are able to use strings to store student names
using namespace std;
//Declare all of the user defined functions which are used
void calculateAverage(int test1,int test2,int test3,int test4,int test5, int& student_average);
char calculateGrade(int& student_average);
int student_counter(ifstream& input2, int& student_count);
int main()
{
string studentName;
int test1,test2,test3,test4,test5;
char grade = ' ';
int student_average = 0;
int student_count = 0;
double class_average = 0;
int class_total = 0;
//declaration of various arrays
int grades[50];
string student_name[10];
int average[10];
char letter_grade[10];
//declaration of all of the various counters
int counter = 0;
int name_count = 0;
int grade_count = 0;
int average_count = 0;
int letter_grade_count = 0;
ifstream infile;//input file stream variable
ifstream infile2;
ofstream outfile; //output file stream variable
infile.open("studentgrades.txt"); //open input file
infile2.open("studentgrades2.txt"); //open input file
outfile.open("results.out"); //open output file
//Print the Heading of the output file!
outfile << "Student" << setw(15) << "Test1" << setw(10) << "Test2" << setw(10) <<
"Test 3" << setw(10) << "Test 4" << setw(10) << "Test 5" << setw(10) <<
"Average" << setw(10) << "Grade" << setw(10) << endl;
//count the number of students in the class
student_counter(infile2,student_count);
//cout << student_count << endl; -- This was here in order to test the functionality of the call
//While there is still data to be read in the input file do the following:
for (counter = 0; counter <= student_count; counter++)
{
//extract the data from the text file
infile >> studentName >> test1 >> test2 >> test3 >> test4 >> test5;
//calculate the average based on the input
calculateAverage(test1,test2,test3,test4,test5,student_average);
//store the average for the particular student in the "average" array.
average[average_count] = student_average;
average_count++;
if (studentName == "end")
{
break;
}
//stores the letter grade for this particular student inside of an array
letter_grade[letter_grade_count] = calculateGrade(student_average);
letter_grade_count++;
//stores the name of the student in the student_name[x] array
student_name[name_count] = studentName;
name_count++;
//stores all of the students 5 grades, these are stored in their appropriate spots
//for example student A will occupy grades[0] through grades[4] and so on...
grades[grade_count] = test1;
grade_count++;
grades[grade_count] = test2;
grade_count++;
grades[grade_count] = test3;
grade_count++;
grades[grade_count] = test4;
grade_count++;
grades[grade_count] = test5;
grade_count++;
class_total = class_total + student_average; //keeps a running total of the students total averages
}
//set the values of the counters to 0 to assure proper loop functionality
counter = 0;
name_count = 0;
grade_count = 0;
average_count = 0;
letter_grade_count = 0;
//execute this loop while there are still students to be read
while (counter <= student_count)
{
//outputs the name of the student in the student_name[x] array
outfile << setw(6) << student_name[name_count];
name_count = name_count + 1;
//outputs all of the students 5 grades these are taken from their appropriate spots
//for example student A will output grades[0] through grades[4] and so on...
outfile << setw(15) << grades[grade_count];
grade_count++;
outfile << setw(10) << grades[grade_count];
grade_count++;
outfile << setw(10) << grades[grade_count];
grade_count++;
outfile << setw(10) << grades[grade_count];
grade_count++;
outfile << setw(10) << grades[grade_count];
grade_count++;
//using the appropriate average (which was calculated in the previous loop)
//we output the average for the student, and we calculate the appropriate grade
//for the corresponding average and output that as well
outfile << setw(10) << average[average_count];
average_count++;
//using the letter_grade array we extract and output the appropriate grade for each student
outfile << setw(10) << letter_grade[letter_grade_count];
letter_grade_count++;
outfile << setw(10) << endl;
counter = counter + 1;
if (counter == student_count) //if the counter is equal to student_count break this loop
{
break;
}
}
class_average = class_total/student_count; //calculates the value of the class average
//Print Final stats to the output file
outfile << endl;
outfile << setw(20) << "Total Number of Students = " << student_count <<
setw(20) << "Class Average = " << class_average << endl;
infile.close();
outfile.close();
return 0;
}
//This function allows the program to count the number of students in the class
int student_counter(ifstream& input2, int& student_count)
{
string studentName;
int test1,test2,test3,test4,test5;
//While there is still data to be read in the input file do the following:
while (input2)
{
input2 >> studentName >> test1 >> test2 >> test3 >> test4 >> test5;
if (studentName == "end")
{
return student_count; //Returns the amount of students to the main function
}
student_count++;
}
}
//calculates the Students average, and passes it back to the main function
void calculateAverage(int test1,int test2,int test3,int test4,int test5, int& student_average)
{
student_average = ((test1+test2+test3+test4+test5))/5;
}
//Calculates the students grade, and returns the appropriate character
char calculateGrade(int& student_average)
{
char grade; //placeholder for students grade
switch((student_average) / 10)
{
case 10:
case 9:
grade = 'a';
return grade;
case 8:
grade = 'b';
return grade;
case 7:
grade = 'c';
return grade;
case 6:
grade = 'd';
return grade;
default:
grade = 'f';
return grade;
}
}