amer_milo 0 Newbie Poster
//Student Name: Mohamad Amiruddin & Frank Douglas
//Course: Diploma in Information Technology 
//Group: IT3
//Student ID: 0165-DDC-08 & 0684-DDC-08
//Program Name: Student Grade Report 
//Assignment Number: 1 
//Assignment Name: Assignment 1 (Mini Project)

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;

//Content Declaration
void reportHeader_weigtage (string course, string semester, float weightage1, float weightage2, float weightage3, float weightage4);
void PrintColumnHeadings(void);
void PrintStudentLine(string id, string lastName, string firstName, int grade1, int grade2, int grade3, int grade4, float finalGrade);
void PrintSumarryReport( int numberOfStudent, float sumOfGrade, float highestGrade, float lowestGrade);

//Grade Calculation
float findHighestGrade (float finalGrade);
float findLowestGrade (float finalGrade);

//Format Declaration
void PrintALine (int range, char pattern);
void SkipLines (int range);
void indent (int range);


ifstream infile ("C:\\students.dat"); 	//input data file: variable name for "C:\\student.dat" is infile.
ofstream outReport ("C:\\report.txt");     	//output text file: variable name for "C:\\report.txt" is outreport.

int main ()
{
	//Declaring Variable
	string course, semester, id, firstName, lastName;
	float w1, w2, w3, w4;	
	int numberOfStudent=0, grade1, grade2, grade3, grade4;		
	
	//Record the number of students and their grades for test 1 - 4.
	float finalGrade, sumOfGrade =0, highestGrade, lowestGrade;			
	
	if (!infile)  	//Files Testing						
	{
   		cerr << "Error: could not open input file\n";			//Prompt when no input file.
	}	
		else if (!outReport)								//If no outReport
		  {
   			cerr << "Error: could not open output file\n";		//Prompt when no output file.
		  }	
			else {
					infile >> course >> semester >> w1 >> w2 >> w3 >> w4;		//Take the datas from "student.dat".
					reportHeader_weigtage (course, semester, w1, w2, w3, w4); //Put the data into this reportHeader & weigtage function.
					while (infile >> id >> lastName >> firstName >> grade1 >> grade2 >> grade3 >> grade4)			//Using loop to record data from "student.dat" student's ID, First and Last name, and Grade for test1-4.
					{
						finalGrade =  w1*grade1 + w2*grade2 + w3*grade3 + w4*grade4;	//The final Grade for the particular student is calculated.
						PrintStudentLine( id,  lastName,  firstName,  grade1,  grade2,  grade3,  grade4,  finalGrade);		//Put the data into this PrintStudentLine function.
						sumOfGrade += finalGrade;														//Add the final Grade to the sumGrade.
						numberOfStudent++;															//Increase the numberOfStudent by 1;
						highestGrade = findHighestGrade(finalGrade);						//Find the highest in findHighestGrade function.
						lowestGrade = findLowestGrade(finalGrade);							//Find the lowest in findLowestGrade function.
					}

					PrintSumarryReport( numberOfStudent,  sumOfGrade, highestGrade, lowestGrade);  //Save the datas in PrintSumarryReport.
		  }
			
		cout << "Close console window?  "; 		
		char c; 
		cin >> c;

	return 0;
}
float findHighestGrade (float finalGrade)
{
	static float MaxSoFar = -1;			//Create a time-limited variable MaxSofar.
	if (finalGrade > MaxSoFar)			//MaxSoFar = -1 : because every positive number is bigger than -1.
	{
		MaxSoFar = finalGrade;
	}
	return MaxSoFar;	
}
float findLowestGrade (float finalGrade)
{
	static float MinSoFar = 150;		//Create a time-limited variable minSoFar.
	if (finalGrade < MinSoFar)			//Record every value which smaller.
	{
		MinSoFar = finalGrade;
	}
	return MinSoFar;
}

void reportHeader_weigtage(string course, string semester, float weightage1, float weightage2, float weightage3, float weightage4)
{
	PrintALine (65, ':');
	indent(0);
	outReport << "Twintech College Sarawak\n";			//Print Twintech College Sarawak.
	indent (0);
	outReport << "Student Grade Report" << endl;		//Print Student Grade Report.
	PrintALine (65, ':');
	outReport << "\nSemester:  " << semester << "\nCourse:  " << course <<endl;		//semester and course are printed.
	PrintALine (65, '.');
	SkipLines(1);   
	PrintALine (65, '.');
	PrintColumnHeadings();
	PrintALine (65, '.');
}

void PrintColumnHeadings(void)				//Print the title for the report.
{
	outReport << setiosflags(ios::right) << setw (10) << "Student ID" << setw(10) <<"Name" << setw(10) << ' ' 
	<< setw(20) << "Grades 1 - 4" << setw(14) <<"Final Grade" << endl;
}

void PrintStudentLine(string id, string lastName, string firstName, int grade1, int grade2, int grade3, int grade4, float finalGrade)
{
	outReport << setiosflags (ios::fixed) << setprecision(2)				//Student content.
		<< setw (11) << setiosflags (ios::left) << id
   		<< setw(10) << firstName << setw(10) << lastName
		<< setiosflags(ios::right)
		<< setw (5) << grade1
		<< setw (5)<< grade2 << setw (5)<< grade3
		<< setw (5)<< grade4 << setw (10) << finalGrade << endl;
}

void PrintSumarryReport( int numberOfStudent, float sumOfGrade, float highestGrade, float lowestGrade)
{
	SkipLines (2);
	PrintALine (65, ':');
	SkipLines (1);
	outReport << setiosflags(ios::showpoint | ios::fixed | ios::left)				//Doing the sumarry.
   		<< "Number of students = " << numberOfStudent << endl
		<< "Class Average = " << sumOfGrade/numberOfStudent << endl
   		<< "Maximum Grade is " << highestGrade << endl
   		<< "Minimum Grade is " << lowestGrade << endl
		<< endl;
	PrintALine (65, ':');
}



//Print line or skip line functions
void PrintALine(int range, char pattern)
{		
for (int i=1; i<=range; i++)	//Print a line with specific character
	outReport << pattern;
outReport << endl;
}

void SkipLines (int range)		//Print a new line
{
for (int i=1; i<=range; i++)
	outReport << endl;
}

void indent (int range)			//Creating spaces
{
for (int i=1; i<=range; i++)
	outReport << ' ';
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.