We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,659 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

GPA Calculator using user-defined functions

Hi, I kind of need help with my homework. I already have a code but it doesn't seem to work. I'm confused about the parameters I put. I don't know which part I actually messed up. I don't know what else to do. Thank you so much in advance for your help :]

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

//Function Prototypes
float gPACalculation(char cGrade);
string honorsDistinction(float fGPA);
void summaryInformation(float fGPA, string sHonorsDistinction);

//Declare Constants
const int SENTINEL = 0;

int main ()
{
	//Declare Variables
	ifstream inFile;				//input file
	ofstream outFile;				//output file
	char cGrade =' ';				//grades
	float fGPA = 0.0;				//GPA
	int nTotalGradePoints = 0;		//Total points
	int nCounter = 0;				//counter
	string sHonorsDistinction = "";		//honors distinction
	string sStudentID = "";				//student id
	int nPointsEquivalent = 0;			//equivalent points
	int nNumberOfGrades = 0;			//number of grade

	
	inFile.open("StudentData.txt");		//open input file

	if (inFile)
	{
		outFile.open("SummaryStudentData.txt");  //open output file

		inFile >> sStudentID;
		
		inFile >> cGrade;			//get grades from input file
	
		fGPA = gPACalculation (cGrade);	//call GPA calculation function		

		sHonorsDistinction = honorsDistinction(fGPA);     //call HonorsDistinction function

		summaryInformation(fGPA, sHonorsDistinction);

		outFile.close();	//closes output file

		cout << "Program executed successfully." << endl;		//prints success on the screen

		cout << "Refer to SummaryStudentData.txt file for output." << endl;	//prints refer to the screen
	}
	
	else
		{
		cout << "Couldn't open StudentData.txt." << endl;

		cout << "Program terminated." << endl;
		}
	
	system("pause");

	return 0;
}

float gPACalculation(char cGrade)
{
	int nPointsEquivalent = 0;
	int nTotalGradePoints = 0;
	int nCounter = 0;
	ifstream inFile;
	float fGPA = 0.0;

	while (!inFile.eof())       //while loop not at the end of the input file
		{
			nTotalGradePoints = 0;			//set total number of points to 0

			nCounter = 0;			//set counter to 0
			
			inFile >> cGrade;		//get grades from input file
		}
	
	if (cGrade == 'A')	//If statement to determine points
	
		nPointsEquivalent = 4;	//Points for A

	else if (cGrade == 'B')

		nPointsEquivalent = 3;	//Points for B

	else if (cGrade == 'C')
	
		nPointsEquivalent = 2;	//Points for C

	else if (cGrade == 'D')

		nPointsEquivalent = 1;	//Points for D

	else if (cGrade == 'F')

		nPointsEquivalent = 0;	//Points for F

	fGPA = nTotalGradePoints / nCounter;

	return cGrade;

}

string honorsDistinction(float fGPA)
{
	//declare variables
	string sHonorsDistinction = " ";
	
	if (fGPA > 3.7)		//if statement that determines the honor distinction of the equivalent GPA
		
		sHonorsDistinction = "Summa Cum Laude";		//prints summa cum laude
	
	else if ((fGPA < 3.7) && (fGPA > 3.4))
	
		sHonorsDistinction = "Magna Cum Laude";		//prints magna cum laude

	else if ((fGPA < 3.4) && (fGPA > 3.1))
		
		sHonorsDistinction = "Cum Laude";			//prints cum laude

	return sHonorsDistinction;				//returns honor distinction
}	

void summaryInformation(float fGPA,string sHonorsDistinction)
{
	//declare variables
	ifstream inFile;				//input file
	ofstream outFile;				//output file
	string sStudentID = "";

	inFile >> sStudentID;

	outFile << "Summary of Student GPA's and Honors" << endl;
	outFile << endl;
	outFile << sStudentID << fGPA << sHonorsDistinction << endl;

	return;

}
6
Contributors
8
Replies
1 Year
Discussion Span
6 Months Ago
Last Updated
10
Views
k3na26
Newbie Poster
3 posts since May 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

It would help to know what kind of errors you are getting and what methods actually work

geojia
Junior Poster in Training
86 posts since May 2010
Reputation Points: 14
Solved Threads: 12
Skill Endorsements: 0

Maybe you want to add the total points that the student has scored.

Maybe you dont want to keep resetting the variable which is supposed to keep track of how many grades the student has got

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
Skill Endorsements: 0

first error that comes up is it doesn't read/open the student data file, the next one is it doesn't calculate the gpa. :) thanks

k3na26
Newbie Poster
3 posts since May 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Well, if it doesn't open the file, how can it calculate the data?

Where did you save the file?
Where is the program looking for the file?
Make sure they are the same place.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 36

Well, if it doesn't open the file, how can it calculate the data?

Where did you save the file?
Where is the program looking for the file?
Make sure they are the same place.

the file was saved on my desktop,same as the project. I think I missed something on my main function but I can't figure out what it is. I'm sorry I'm kinda new to this and I don't know much about c++. thankyou :]

k3na26
Newbie Poster
3 posts since May 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

There maybe an issue with file paths. The solution to this problem is

1. Write a sample program to create a file. Give the file a weird name. Say aabbccddeeffgghh.txt.
2. After you create the file, exit the program and then use the search function to locate the file. From now one keep the file SummaryStudentData.txt in that folder.

Another option is to use absolute file names as opposed to relative file names

abhimanipal
Master Poster
742 posts since Dec 2009
Reputation Points: 114
Solved Threads: 104
Skill Endorsements: 0

please can can u show me what the input file looks like...

chidd2011
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

for this simple thing why are u exragatting too much

satabios
Newbie Poster
1 post since Nov 2012
Reputation Points: -1
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0866 seconds using 2.77MB