I'm looking to obtain an average from a seperate text file.

I'm looking to obtain an average from a seperate text file.

Page 1 of 1

0 Replies - 808 Views - Last Post: 08 December 2007 - 10:09 PM Rate Topic: -----

#1 JaMilt  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-October 07

I'm looking to obtain an average from a seperate text file.

Post icon  Posted 08 December 2007 - 10:09 PM

Greetings!

Id like to extend that program, when finished, it should accumulate the earned and maximum points in each category, and compute the final grade using the relative weighting in the program. My goal is to break it out to look like the follwoing categories:

Attendance: 2/17
Homework: 2/17
Test: 3/17
Project: 10/17


I already have them both averaged out for a final %, I just need assistance putting together the individual categories. THis is what I have:


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const char*  bookName = "C:\\Documents and Settings\gradebook.txt";
const int	columnWidth[4]  = { 10, 24, 6, 7 };
const int	maximumStudents = 100;

void readStudentRecord		(istream& gradebook, int& studentId, string& firstName, string& lastName, int& entryCount);
void reportStudentInformation (ostream& report,	int  studentId, string  firstName, string  lastName);

void readGradeRecord		(istream& gradebook, string& date, string& assignmentType, int& earnedScore, int& maximumScore);
void reportGradeInformation (ostream& report,	string  date, string  assignmentType, int  earnedScore, int  maximumScore);
							   
void reportGradeHeading (ostream& report);

float computeCurrentNumericGrade (int attendanceScore, int attendanceMaximum, 
								  int homeworkScore,   int homeworkMaximum, 
								  int testScore,	   int testMaximum, 
								  int projectScore,	int projectMaximum);

void reportFinalGrades (ostream& report, int id[], string student[], float grade[], int studentCount);

int main(int argc, char *argv[])
{
	int studentId, entryCount;
	string firstName, lastName;

	int currentStudent = 0;
	int attendanceScore;
	int attendanceMaximum;
	int homeworkScore;
	int homeworkMaximum;
	int testScore;
	int testMaximum;
	int projectScore;
	int projectMaximum;
	int studentCount;

	int	studentIdList[maximumStudents];
	float  gradeList[maximumStudents];
	string fullStudentNameList[maximumStudents];

	ifstream theBook;  // Create the input file stream.
					   // The is NOT bound to the file yet ...

	theBook.open(bookName);
	if (theBook)
	{
	   while (!theBook.eof())
	   {
			readStudentRecord (theBook, studentId, firstName, lastName, entryCount);
			reportStudentInformation (cout, studentId, firstName, lastName);
			reportGradeHeading (cout);

			attendanceScore = attendanceMaximum = homeworkScore = homeworkMaximum = 
				testScore = testMaximum = projectScore = projectMaximum = 0;

			int i = 0;
			while (i < entryCount)
			{
				string date, assignmentType;
				int earnedScore, maximumScore;

				readGradeRecord(theBook, date, assignmentType, earnedScore, maximumScore);
				reportGradeInformation (cout, date, assignmentType, earnedScore, maximumScore);
				
				if (assignmentType == "attendance")
				{
					attendanceScore   = attendanceScore + earnedScore;
					attendanceMaximum = attendanceMaximum + maximumScore;
				}
				else if (assignmentType == "homework")
				{
					homeworkScore   = homeworkScore + earnedScore;
					homeworkMaximum = homeworkMaximum + maximumScore;
				}
				else if (assignmentType == "test")
				{
					testScore   = testScore + earnedScore;
					testMaximum = testMaximum + maximumScore;
				}
				else if (assignmentType == "project")
				{
					projectScore   = projectScore + earnedScore;
					projectMaximum = projectMaximum + maximumScore;
				}
				else
				{
					cerr << "Unrecognized assignment type encountered: " << assignmentType << endl;
				}

				i = i + 1;
			}
			cout << endl;

			studentIdList[currentStudent] = studentId;
			fullStudentNameList[currentStudent] = firstName + " " + lastName;
			gradeList[currentStudent] = computeCurrentNumericGrade(attendanceScore, attendanceMaximum, homeworkScore, homeworkMaximum, testScore, testMaximum, projectScore, projectMaximum);	 

			currentStudent++;
		}
		studentCount = currentStudent;
		theBook.close();

		reportFinalGrades (cout, studentIdList, fullStudentNameList, gradeList, studentCount);
	}
	else
	{
		cerr << "Could not open the input file: " << bookName << endl;
	}
	
	system("PAUSE");
	return EXIT_SUCCESS;
}

void readStudentRecord (istream& gradebook,
						int& studentId,
						string& firstName,
						string& lastName,
						int& entryCount)
{
	// Assume the input stream is open ...
	gradebook >> studentId >> firstName >> lastName >> entryCount;
}

void readGradeRecord (istream& gradebook,
					  string& date,
					  string& assignmentType,
					  int& earnedScore,
					  int& maximumScore)
{
	gradebook >> date >> assignmentType >> earnedScore >> maximumScore;
}

void reportStudentInformation (ostream& report,
							   int studentId,
							   string firstName,
							   string lastName)
{
	report << "Name: " << left << setw(29) << lastName + ", " + firstName 
		   << " Student ID: " << right << setw(2) << studentId << endl;
}

void reportGradeHeading (ostream& report)
{
	 report << endl << right
			<< setw(45) << "Score" << endl
			<< left
			<< setw(columnWidth[0]) << "Date" << " "
			<< setw(columnWidth[1]) << "Type" << " "
			<< setw(columnWidth[2]) << "Earned" << " "
			<< setw(columnWidth[3]) << "Maximum" << endl;
}

void reportGradeInformation (ostream& report,
							 string date,
							 string assignmentType,
							 int earnedScore,
							 int maximumScore)
{
	report << left
			<< setw(columnWidth[0]) << date << " "
			<< setw(columnWidth[1]) << assignmentType << " "
			<< right
			<< setw(columnWidth[2]) << earnedScore << " "
			<< setw(columnWidth[3]) << maximumScore << endl;
}

float computeCurrentNumericGrade ( int attendanceScore, int attendanceMaximum, 
								   int homeworkScore,   int homeworkMaximum, 
								   int testScore,	   int testMaximum, 
								   int projectScore,	int projectMaximum)
{
	const float attendanceWeight =  2.0/17.0;
	const float homeworkWeight   =  2.0/17.0;
	const float testWeight	   =  3.0/17.0;
	const float projectWeight	= 10.0/17.0;

	float attendanceGrade = static_cast<float>(attendanceScore) / attendanceMaximum;
	float homeworkGrade   = static_cast<float>(homeworkScore) / homeworkMaximum;
	float testGrade	   = static_cast<float>(testScore) / testMaximum;
	float projectGrade	= static_cast<float>(projectScore) / projectMaximum;
	
	return attendanceGrade * attendanceWeight + homeworkGrade * homeworkWeight + testGrade * testWeight + projectGrade * projectWeight;
}

void reportFinalGrades (ostream& report, int id[], string student[], float grade[], int studentCount)
{
	 const int columnWidth[3] = { 2, 20, 5 };

	 report << "Final Grade Report" << endl << endl;
	 report << left 
			<< setw(columnWidth[0]) << "ID" << " "
			<< setw(columnWidth[1]) << "Name" << " "
			<< setw(columnWidth[2]) << "Grade" << endl;

	 for (int i = 0; i < studentCount; i++)
	 {
				
		 report << fixed << setprecision(1);

		 report << right << setw(columnWidth[0]) << id[i] << " "
				<< left  << setw(columnWidth[1]) << student[i] << " "
				<< right << setw(columnWidth[2]) << grade[i]*100.0 << "%" << endl;
	 }
}





**Text File**
1 STUDENT ONE 16
8/28/2007 attendance 1 1
9/4/2007 attendance 1 1
9/4/2007 homework 1 1
9/11/2007 attendance 1 1
9/11/2007 homework 1 1
9/11/2007 test 31 35
9/18/2007 attendance 1 1
9/18/2007 homework 1 1
10/2/2007 attendance 1 1
10/2/2007 homework 1 1
10/9/2007 attendance 1 1
10/9/2007 homework 1 1
10/9/2007 project 92 100
10/16/2007 attendance 1 1
10/16/2007 homework 1 1
10/16/2007 test 72 75

2 STUDENT TWO 16
8/28/2007 attendance 1 1
9/4/2007 attendance 1 1
9/4/2007 homework 1 1
9/11/2007 attendance 1 1
9/11/2007 homework 1 1
9/11/2007 test 30 35
9/18/2007 attendance 1 1
9/18/2007 homework 1 1
10/2/2007 attendance 1 1
10/2/2007 homework 1 1
10/9/2007 attendance 0 1
10/9/2007 homework 0 1
10/9/2007 project 68 100
10/16/2007 attendance 1 1
10/16/2007 homework 0 1
10/16/2007 test 58 75


/**Text File

Thanks!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1