GPA Calculator Program

Pages: 12
I have an assignment to make a GPA Calculator using four functions, here is the program coded the way the Professor wants it to look: (I will post my code that I'm having trouble with after this post
*********************************************************************/
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
using namespace std;

int main()
{
//local constants
int QUIT = -1; //Init Quit Value
const float GRADE_LOW = 0.0; //Set Grade Low to 0.0
const float GRADE_HIGH = 4.0; //Set Grade High to 4.0
const int HOUR_MIN = 1; //Set Hour Min to 1
const int HOUR_MAX = 4; //Set Hour Max to 4
const int SIZE = 3; //Set Array Size to 6

//local variables
int Count = 0; //Init Count to 0
string Name; //Students Name
float Grade; //Students Grade
float Hours; //Credit Hours
float Pts; //Convert Grade to Pts
float Grade_Array [SIZE]; //Initialize Grade Array
float Hour_Array [SIZE]; //Initialize Hour Array
float Pts_Array [SIZE]; //Initialize Pts Array
float Gpa; //Initialize GPA
float Total_Gradepts; //Total of Grades in Array
float Total_Crdt; //Total of Hrs in Array
/**************************start main program*********************/

// Spaces from top of screen
cout << "\n\n\n\n\n\n";

//Input name or quit value
cout << setw(57) << "Input Students Name Or -1 To Quit" << endl << "\n\n";

//Input Student Name
cout << setw(48) << "Students Name : ";
cin >> Name;

//Input Grade (0.0-4.0)
cout << "\n" << setw(48) << "Students Grade : ";
cin >> Grade;

//WHILE (Grade is not QUIT)
while (Grade != QUIT)
{

//INPUT Credit Hours (1-4)
cout << setw(48) << "Credit Hours : ";
cin >> Hours;

//IF (Grade & Credit Hours are Valid)
if ((Grade >= GRADE_LOW && Grade <= GRADE_HIGH) &&
(Hours >= HOUR_MIN && Hours <= HOUR_MAX))
{

//Calc Grade Pts
Pts = Grade * Hours;

//ADD Grade, Credit Hours, Grade Pts to Array
Grade_Array [Count] = Grade;
Hour_Array [Count] = Hours;
Pts_Array [Count] = Pts;

//Increment Count
Count ++;

//IF (Array if Full)
if (Count == SIZE)
Grade = QUIT;

//ELSE
else
{
cout << "\n" << setw(48) << "Students Grade : ";
cin >> Grade;
}
//END IF
}
//ELSE
else
{
//Display Error

cout << "\n" << setw(42) << "ERROR" << endl;
cout << setw(42) << "-----" << endl;

//Get Next Grade
cout << "\n" << setw(48) << "Students Grade : ";
cin >> Grade;
}

//END IF


//END WHILE
}

//Clear Screen
system ("cls");

// Spaces from output
cout << "\n\n\n";

//Output Students Name
cout << setw(47) << "Final Grades For " << Name << endl << "\n\n";

//Display Grade, Credit Hour, Grade Pts, Header
cout << setw(60) << "Grades Credit Hours Grade Pts" << endl;

//FOR (Each Grade Entered)
for (Count = 0; Count <= SIZE-1; Count ++)
{
//Set to decimal point output
cout << setiosflags (ios::fixed | ios::showpoint) << setprecision (2);

//Display Grade, Credit Hours, Grade Points
cout << setw(26) << Grade_Array[Count] << setw(15) << Hour_Array[Count];
cout << setw(17) << Pts_Array[Count] << endl;
//END FOR
}

//Calculate GPA
Total_Gradepts = Pts_Array[0] + Pts_Array[1] + Pts_Array[2];
Total_Crdt = Hour_Array[0] + Hour_Array[1] + Hour_Array[2];
Gpa = Total_Gradepts / Total_Crdt;

//Output GPA
cout << "\n\n" << setw(40) << "GPA : " << Gpa;

// Spaces from output
cout << "\n\n";

//Pause Screen
system ("pause");

return 0;

}//END Calc Students GPA

closed account (j3Rz8vqX)
Use code tags:
[code]
//Stuff...
[/code]
Is the array supposed to hold 3 or 6 items?

const int SIZE = 3; //Set Array Size to 6
6 I just set it to three for testing, so I would not have to go through 6 sets of grades
Here is the first part of my program, it's too long to fit it all at once:
/**********************************************************************
*Program Name : CSC 110 Calc Students GPA
*Author :
*Due Date : May 12, 2014
*Course/Section : CSC 110 - 005
*Program Description:
*
*BEGIN Main Function
* Input the student name (or quit)
* WHILE (the student name is not the quit value)
* Call function to input the course info and calculate the grade points
* Clear the screen?
* Call function to calculate the gpa
* Call function to display the grade report
* Pause the screen
* Clear the screen
* Get the next student name (or quit)
* END WHILE
* Call function to display closing message (optional)
*END Main Function
*********************************************************************/
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
using namespace std;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//place prototypes here
float input (float Grade, float Hours, int Count, float Pts, float Grade_Array[],
 float Hour_Array[], float Pts_Array[]);
float gpa (float Total_Gradepts, float Pts_Array[], float Total_Crdt, 
           float Hour_Array[], float Gpa);
bool validate(int Grade, int Hours);
void display (int Count, int SIZE, float Grade_Array[], 
              float Hour_Array[], float Pts_Array[], float Gpa);
void close();

int main()
{
	//local constants
	const int QUIT           = -1;		//Init Quit Value
	const string QUIT_NAME   = "-1";	//Init String Quit
    const float GRADE_LOW 	 = 0.0;		//Set Grade Low to 0.0 
	const float GRADE_HIGH	 = 4.0;		//Set Grade High to 4.0   
	const int HOUR_MIN    	 = 1;		//Set Hour Min to 1
	const int HOUR_MAX    	 = 4;		//Set Hour Max to 4   
	const int SIZE           = 3;		//Set Array Size to 6   
	         
	//local variables
  	int Count = 0;					    //Init Count to 0
  	string Name;						//Students Name
  	float Grade;						//Students Grade
  	int Hours;	   						//Credit Hours
  	float Pts;							//Convert Grade to Pts
  	float Grade_Array [SIZE];     	 	//Initialize Grade Array
	float Hour_Array [SIZE];     	 	//Initialize Hour Array
	float Pts_Array [SIZE];     	 	//Initialize Pts Array  	
	float Gpa;							//Initialize GPA
	float Total_Gradepts;				//Total of Grades in Array
	float Total_Crdt;			    	//Total of Hrs in Array
	/**************************start main program*********************/
 	
 	// Spaces from top of screen  
  	cout << "\n\n\n\n\n\n";
  	
  	//Input name or quit value	 
    cout << setw(57) << "Input Students Name Or -1 To Quit" << endl;
    cout << "\n\n";
    
    //Input Student Name
 	cout << setw(48) << "Students Name  : ";
 	cin >> Name;

	//WHILE Name does not equal QUIT
 	while (Name != QUIT_NAME)
 	   {
    	//Call Function To Input 
		input (Grade, Hours, Count, Pts, Grade_Array, Hour_Array, Pts_Array);
 			
		//Clear Screen 
  		system ("cls");
  		
  		//Call Function to Calculate GPA
        gpa (Total_Gradepts, Pts_Array, Total_Crdt, Hour_Array, Gpa);
  		  		
  		//Call Function to Display the grade report
        display (Count, SIZE, Grade_Array, Hour_Array, Pts_Array, Gpa);
  		  		
  		//Pause Screen
    	system ("pause");
		
		//Clear Screen 
  		system ("cls");
  		
  		// Spaces from top of screen  
  		cout << "\n\n\n\n\n\n";
  		
  		//Input name or quit value	 
    	cout << setw(57) << "Input Students Name Or -1 To Quit" << endl;
    	cout << "\n\n";
    	
    	//Input Student Name
 		cout << setw(48) << "Students Name  : ";
 		cin >> Name;
    	
    //END WHILE
       }
  	
    //Call function to display closing message (optional)	
    close();
    
    //Pause Screen
    system ("pause");
  
}//END Calc Students GPA 

/********************************************************************
*Function Name : Input
*Author :
*Date : May 12, 2014
*Program Description: This function will ask user for a grade, &
* credit hours, calculate pts, and put all three in separate arrays.
*
*BEGIN Function Grade Input
* Input Grade (0.0-4.0)
* Input Credit Hours (1-4)
* Calculate Pts
* Put Grade, Credit Hours, & Pts. in separate Arrays
*END Function
************************Start Input Function*************************/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 float input (float Grade, float Hours, int Count, float Pts, float Grade_Array[],
 float Hour_Array[], float Pts_Array[])
 {		
 	//local constant 
 	
	//local variables 
//    float Grade;						//Students Grade
//    float Hours;						//Credit Hours
//    float Pts;						//Convert Grade to Pts
  	
	/*************************************************************/

 	//Input Grade 
 	cout << "\n" << setw(48) << "Students Grade : ";
 	cin >> Grade;
 	
 	//Input Credit Hours 
 	cout << setw(48) << "Credit Hours   : ";
 	cin >> Hours;
 	
 	//Call Function To Validate Input Data
	validate (Grade, Hours);
 	
 	//Calc Grade Pts
 	Pts = Grade * Hours;
 	
 	//ADD Grade, Credit Hours, Grade Pts to Array 
 	Grade_Array [Count] = Grade;
 	Hour_Array [Count] = Hours;
 	Pts_Array [Count] = Pts;
 }


/********************************************************************
*Function Name : Validate
*Author :
*Date : May 12, 2014
*Program Description: This function will check to make sure grade &
* credit hours are valid
*
*BEGIN Function Validate
* WHILE (Grade is not QUIT)
* IF (Grade & Credit Hours are Valid)
* Increment Count
* IF (Array if Full)
* Grade = QUIT
* ELSE
* Get Next Grade
* END IF
* ELSE
* Display Error
* Get Next Grade
* END IF
* END WHILE
*END Function
***********************Start Validate Function***********************/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
bool validate (int Grade, int Hours)
 {		
 	//local constant ??
 	const float GRADE_LOW  = 0.0;		//Set Grade Low to 0.0 
	const float GRADE_HIGH = 4.0;		//Set Grade High to 4.0   
	const int HOUR_MIN     = 1;			//Set Hour Min to 1
	const int HOUR_MAX     = 4;			//Set Hour Max to 4
	const int QUIT         = -1;		//Init Quit Value
	const int SIZE         = 3;			//Set Array Size to 6 
	
	//local variables ??
	int Count = 0;						//Init Count to 0
//	float Grade;						//Students Grade
//  float Hours;						//Credit Hours
  	float Pts;							//Convert Grade to Pts
  	float Grade_Array [SIZE];     	 	//Initialize Grade Array
	float Hour_Array [SIZE];     	 	//Initialize Hour Array
	float Pts_Array [SIZE];     	 	//Initialize Pts Array  

	/*************************************************************/
        		
 		//IF (Grade & Credit Hours are Valid)
 		if ((Grade >= GRADE_LOW && Grade <= GRADE_HIGH) &&
		   (Hours >= HOUR_MIN && Hours <= HOUR_MAX))
         {   
 		   
		   //Calc Grade Pts
 		   //Pts = Grade * Hours;
 		   
 		   //ADD Grade, Credit Hours, Grade Pts to Array 
 		   //Grade_Array [Count] = Grade;
 		   //Hour_Array [Count] = Hours;
 		   //Pts_Array [Count] = Pts;
			
		   //Increment Count
 		   Count ++;
 
 		   //IF (Array if Full)
 		   if (Count == SIZE)
 			   Grade = QUIT;
 
 		   //ELSE
 		   else
 		   {
 			cout << "\n" << setw(48) << "Students Grade : ";
 			cin >> Grade;
 		   }
 		   
	     //END IF
 		 }
          		
 		 //ELSE
 		 else
 		 {
 		  //Display Error
 		  	
 		  cout << "\n" << setw(42) << "ERROR" << endl;
 		  cout << setw(42) << "-----" << endl;
 			
 		  //Get Next Grade
 		  cout << "\n" << setw(48) << "Students Grade : ";
 		  cin >> Grade;
 		 }
 		    
 		 //END IF   
 		
 		
	
 	
 
  //END FUNCTION
 }

Last edited on

And here's the 2nd part, sorry if this is outrageously long, very much a beginner!!

/********************************************************************
*Function Name : Calculate GPA
*Author :
*Date : May 12, 2014
*Program Description: This Function will calculate the GPA from the input
* function
*
*BEGIN Calculate GPA
* Call Input Funtion
* Calculate the GPA
*END Calculate GPA
*************************Start GPA Function**************************/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
float gpa (float Total_Gradepts, float Pts_Array[], float Total_Crdt,
float Hour_Array[], float Gpa) 
 {		
 	//local constants 
 	
	//local variables 
//	float Gpa;							//Initialize GPA
//	float Total_Gradepts;				//Total of Grades in Array
//  float Total_Crdt;			    	//Total of Hrs in Array
	
	/*************************************************************/
	
	//Call Input Funtion
    //??? =	input(???)
	
	//Calculate GPA
    Total_Gradepts = Pts_Array[0] + Pts_Array[1] + Pts_Array[2];
    Total_Crdt = Hour_Array[0] + Hour_Array[1] + Hour_Array[2];
    Gpa = Total_Gradepts / Total_Crdt;
	
 }


/********************************************************************
*Function Name : Display Grade Report
*Author :
*Date : May 12, 2014
*Program Description: This function will display the grade report.
*
*BEGIN Display Grade Report
* Clear the screen
* Output Students Name
* Display Grade, Credit Hour, Grade Pts, Header
* FOR (Each Grade Entered)
* Display Grade, Credit Hours, Grade Points
* END FOR
* Display Final GPA
*END Function Practice - find a cube
************************Start Display Function***********************/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
void display (int Count, int SIZE, float Grade_Array[], 
float Hour_Array[], float Pts_Array[], float Gpa)	
{		
	//local constants 
	
	//local variables 

	
	/*************************************************************/
 	
 	//Clear Screen 
  	system ("cls");
 	
 	// Spaces from output
  	cout << "\n\n\n";
  	 	
  	//Output Students Name
  	cout << setw(47) << "Final Grades For " << endl << "\n\n";
  	
  	//Display Grade, Credit Hour, Grade Pts, Header
  	cout << setw(60) << "Grades      Credit Hours      Grade Pts" << endl;
 	
 	//FOR (Each Grade Entered)
 	for (Count = 0; Count <= SIZE-1; Count ++)
 	{
 	    //Set to decimal point output
	    cout << setiosflags (ios::fixed | ios::showpoint) << setprecision (2);
	    
	 	//Display Grade, Credit Hours, Grade Points
 		cout << setw(26) << Grade_Array[Count] << setw(15) << Hour_Array[Count]; 
		cout << setw(17) << Pts_Array[Count] << endl;
 	//END FOR		
    }
  	
    //Output GPA
    cout << "\n\n" << setw(40) << "GPA : " << Gpa;
    
    //Pause Screen
    system ("pause");
  	
  	//Clear Screen 
  	system ("cls");
    	
}


/********************************************************************
*Function Name : Display Closing
*Author :
*Date : May 12, 2014
*Program Description: This program will display a closing message to
* the user. It will first clear the screen, then display the
* message. The message will include the Authors name and be big
* and bold.
*
*BEGIN Display Closing
* Clear the screen
* Display the closing message
* Pause the screen
* Clear the screen
*END Display Closing
***********************Start Closing Function************************/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void close ()
 {
 	//local constants
	
	//local variables
	
	/*************************************************************/
	
	//Clear Screen 
  	system ("cls");
  	
  	//Display the closing message
  	cout << "\n\n\n";
  	cout << setw(50) << "--------------------" << endl;
  	cout << setw(50) << "Program By          " << endl;
  	cout << setw(50) << "--------------------" << endl;
  	
  	// Spaces from output
  	cout << "\n\n\n\n";
  	
  	//Pause the screen
	system ("pause");
	
	//Clear Screen 
  	system ("cls");	
 }
Last edited on
Dput, sorry if i posted this the wrong way, this is the first time I've tried using this for help, I'm very much a beginner at Programing, any help would be appreciated.
Just occurred to me that maybe seeing the actual assignment might help so here it is:

CSC 110
Function Program


Create a program that will calculate the semester GPA for a student. Your program will input a students name and all of his/her course information for each course taken. Course information will include the course grade and credit hours. Your program will then calculate the GPA and display all information. An example display would be:

Sarah’s GPA Report


Grade Credit Hours Grade Points


3.50 3 10.50
4.00 4 16.00
3.25 3 9.75


Semester GPA: 3.63



You should create functions to:
1. Input the course info (grade, credit hours) and put them into an array along with the grade points
2. Check for valid input data (grade and credit hours)
3. Calculate the gpa
4. Display the student grade report (like above)

I will provide you with a function to format the student’s name. Your program should have 3 arrays. One to store the grades, credit hours, and grade points. You will need to include the string library in order to have string variables.

Pseudocode for the main program. The main function must declare three arrays but that does not go into the pseudocode.

BEGIN Main Function
Input the student name (or quit)
WHILE (the student name is not the quit value)
Call function to input the course info and calculate the grade points
Clear the screen?
Call function to calculate the gpa
Call function to display the grade report
Pause the screen
Clear the screen
Get the next student name (or quit)
END WHILE
Call function to display closing message (optional)
END Main Function
Last edited on
If you can edit your post - highlight the code part, then click on the <> button in the Format palette on the right side of the post - that will format the code so it'll be a lot easier for others to follow. :)
wildblue, now I see what your saying, that does make it a lot easier to see, I appreciate the help, more than you know!!!
Where are you running into trouble?
When I compile it, it compiles fine, but I must be missing a loop somewhere because it doesn't output correctly which tells me I'm not imputing into the arrays incorrectly. That being said the whole "functions" part of this is very confusing to me so I've spent hours just moving stuff around trying to troubleshoot the problem and can't figure it out
What output are you getting instead of what you have above as an example?
A whole bunch of crazy numbers, for example if you ran these two in a compiler the original would ask the user for a student name, then 3 grades and credit hours, store them in arrays and calculate the Grade points and GPA. If you were to run the program I wrote, the screen comes up input the name 1 grade & hour, then it asks for the second grade and immediately goes to the output page with a ton of garbage numbers. So I must need a loop in the input ( I have tried a FOR and WHILE neither of which worked) or I am missing something else
You call the gpa function before the Pts_Array has values for the 1 and 2 elements. Since the arrays have uninitialized values in them, you're going to have crazy numbers. So yes, you want to have the grade entry within a loop. If there's a fixed number of grades to enter, you could use a for loop.

Also - this function says it will return a float - you aren't returning anything though.

1
2
3
4
5
6
7
8
9
float gpa (float Total_Gradepts, float Pts_Array[], float Total_Crdt,
float Hour_Array[], float Gpa) 
 {		
 
    Total_Gradepts = Pts_Array[0] + Pts_Array[1] + Pts_Array[2];
    Total_Crdt = Hour_Array[0] + Hour_Array[1] + Hour_Array[2];
    Gpa = Total_Gradepts / Total_Crdt;
	
 }



Edit: Your input and validate functions also say they will return values, but don't.
Last edited on
So I need to take all the stuff out from the ( ) in line 1 & 2?
Were you intending to have the function return the Gpa back to main for future use? Right now, nothing is happening with the Gpa that you calculate here.

Have you covered passing variables by reference?
Last edited on
We have covered that very briefly, but I don't remember how to/why to use it. According to the instruction sheet, it sounded to me like main calls the input function which would then have to call the validate function before putting the inputs into the array. Where they are stored I'm confused about b/c the assignment doesn't really say.
Sounds like you'd do something like this for each student to be entered (your while loop)

for all grades to be entered (loop set # of times)
... input grade
... input hours
... while validate function returns false (invalid values)
....... output invalid message - try again
....... input grade
....... input hours
... store grade in grade array
... store hours in hours array
... calculate grade points and store in grade point array
// end for loop after all grades for student entered
gpa = result of gpa function
//
then display report for student
That sounds right but these have to be functions, I'm not having a problem writing the Program in main, that's what I did with the first program I posted originally. The problem I am having is properly using the functions correctly
Pages: 12