GPA Calculator Program

Pages: 12
closed account (j3Rz8vqX)
A possibility:

Pass the data as references:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Function Prototype:
float gpa (float &Total_Gradepts, float Pts_Array[], float &Total_Crdt, float Hour_Array[], float &Gpa);

//Function Declaration:
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;
    //Nothing being returned.. Possibly set to be void return instead.
}

//Function call:
//Call Function to Calculate GPA
gpa (Total_Gradepts, Pts_Array, Total_Crdt, Hour_Array, Gpa);
Yes - I understand you need functions. Three of the functions get called from main - the validate function gets called from the input function.

I don't think you need to return anything from the input function, so that could be void instead of float return type.

The validate function returns either true or false so you know whether to proceed to store the info entered.
1
2
while (validate(grade,hours)==false)
// reenter info 


The gpa function can return the float value you calculate for the gpa back to a variable in main.
gpa = gpa(parameters...)
The gpa then gets sent to the display function to print along with the other grade info.

Edit: Or - if you are comfortable using references - you can use those instead of returning a value.
Last edited on
Dput, thanks for that it worked great

wildblue, the validate worked as well, thanks. I can't send the GPA back to main b/c the Pseudo says I have to call a function to display, unless I'm not understanding you.

Right now the only part that don't work are invalid numbers and the GPA, you both have been a TON of help and I am more than willing to try any other thoughts you have, you both just brought my stress level down about 100 fold!!
Still having problems with this program, invalid numbers not working as well as amount going into arrays, any help would be appreciated.
closed account (j3Rz8vqX)
Possibly something relative to what you are attempting to design/implement:
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
using namespace std;
const int SIZE = 2;	 //Set Array Size to 6

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

	/*************************************************************/

    //IF (Grade & Credit Hours are Valid)
    if ((Grade >= GRADE_LOW && Grade <= GRADE_HIGH) && (Hours >= HOUR_MIN && Hours <= HOUR_MAX))
    {
        cout<<endl;
        return true;
    }
    //ELSE
    else
    {
        //Display Error
        cout << "\n" << setw(42) << "ERROR" << endl;
        cout << setw(42) << "-----" << endl;
        return false;
    }
    return false;
    //END IF
  //END FUNCTION
 }
void input (float Grade, float Hours, int &Count, float Pts, float Grade_Array[], float Hour_Array[], float Pts_Array[])
{
    bool stop = false;
    /*************************************************************/
    if(Count < SIZE)
    {
        do
        {
            //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
            if(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;
                Count++;
                stop = true;
            }
        }while(!stop);
    }
    else
    {
        cout<<setw(42)<<"We are full"<<endl;
    }

}
int main()
{
	//local variables
	const string QUIT_NAME   = "-1";	//Init String Quit
  	int Count = 0;					    //Init Count to 0
  	string Name;						//Students Name
  	float Grade;						//Students Grade
  	int Hours;	   						//Credit Hours
  	float Pts;
    float Grade_Array[10];
    float Hour_Array[10];
    float Pts_Array[10];
    /*************************************************************/
    //Input Student Name
 	cout << setw(48) << "Students Name  : ";
 	cin >> Name;
 	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);

        //Input Student Name
        cout << setw(48) << "Students Name  : ";
        cin >> Name;
    }
    //My basic print:
    for(int i=0;i<Count;i++)
    {
        cout<<setw(42)<<"Grade["<<i<<"]: "<<Grade_Array[i]<<endl;
        cout<<setw(42)<<"Hour["<<i<<"]: "<<Hour_Array[i]<<endl;
        cout<<setw(42)<<"Points["<<i<<"]: "<<Pts_Array[i]<<endl;
    }
    /**************************************************************/
    return 0;
}//END Calc Students GPA 


The above code is a mesh of random things in your program with "one" possible valid check and input function.
wildblue, the validate worked as well, thanks. I can't send the GPA back to main b/c the Pseudo says I have to call a function to display, unless I'm not understanding you.


You can send the gpa value back to main so main can then send that value with the call to the display function. Or you can pass the gpa variable by reference so the gpa function updates the gpa variable back in main. Then the gpa function returns nothing (void return type instead of float).
Would I just send the gpa back to main with the "&" sign from the gpa function?
The & is a reference - you aren't sending anything back - you're actually updating the original variable.

1
2
3
4
5
6
7
8
9
10
11
12
//call from main - send arrays and gpa variable
gpa (Pts_Array, Hour_Array, gpa)

//function definition - receives arrays and knows &Avg is a reference, or just another name for the original gpa variable in main. 
I've made the Total_Gradepts and Total_Crdt local variables in the function, since they don't seem to be needed in main. 

void gpa (float Pts_Array[], float Hour_Array[], float &Avg)
{	
    float Total_Gradepts = Pts_Array[0] + Pts_Array[1] + Pts_Array[2]);
    float Total_Crdt = Hour_Array[0] + Hour_Array[1] + Hour_Array[2];
    Avg = Total_Gradepts / Total_Crdt;
}


or you can have the function return a value which you store in gpa variable in main.

1
2
3
4
5
6
7
8
9
//gpa variable in main holds value returned by 
gpa = gpa (Pts_Array, Hour_Array) 

float gpa (float Pts_Array[], float Hour_Array[]) 
{	
    float Total_Gradepts = Pts_Array[0] + Pts_Array[1] + Pts_Array[2]);
    float Total_Crdt = Hour_Array[0] + Hour_Array[1] + Hour_Array[2];
    return Total_Gradepts / Total_Crdt;
}
Last edited on
Gpa is the variable in main (Capital Letter); gpa (lower case) is the function. I have to specifically follow the directions of the assignment, and if I'm reading it correctly, nowhere does it say anything about storing anything in main which is what is confusing to me. The assignment sheet appears to say that the input function takes in the inputs, puts them in the array and then calls the validate function. I don't see anywhere where I can send something back to main
closed account (j3Rz8vqX)
Example of returning gpa:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//Function Prototype:
float gpa (float &Total_Gradepts, float Pts_Array[], float &Total_Crdt, float Hour_Array[], float Gpa);//<<-----removed & from Gpa

//Function Declaration:
float gpa (float &Total_Gradepts, float Pts_Array[], float &Total_Crdt, float Hour_Array[], float Gpa)//<<-----removed & from 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;
    
    return Gpa;
}

//Function call:
//Call Function to Calculate GPA
Gpa = gpa (Total_Gradepts, Pts_Array, Total_Crdt, Hour_Array, Gpa);//<<--------------Assigned the returning Gpa(its value that is) to this local Gpa 
I'm just about ready to throw in the towel, every time I change something, a different part stops working correctly. Now not only does the GPA not work, neither does the proper amount of inputs, the outputs or the errors. So basically nothing is working at the moment.
I've never used those return true / return false statements before so I'm not sure they are ok to use as well as the do/while loop which is def not ok to use. For some reason he hates those types of loops.
closed account (j3Rz8vqX)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
using namespace std;
const int SIZE = 2;	 //Set Array Size to 6

void 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

    /*************************************************************/
    while((Grade < GRADE_LOW || Grade > GRADE_HIGH) && (Hours < HOUR_MIN || Hours > HOUR_MAX))
    {
        //IF (Grade & Credit Hours are Valid)
        if ((Grade >= GRADE_LOW && Grade <= GRADE_HIGH) && (Hours >= HOUR_MIN && Hours <= HOUR_MAX))
        {
            //You are good to go!
        }
        //ELSE
        else
        {
            //Display Error
            cout << "\n" << setw(42) << "ERROR" << endl;
            cout << setw(42) << "-----" << endl;

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

            //Input Credit Hours
            cout << setw(48) << "Credit Hours   : ";
            cin >> Hours;
        }
        //END IF

    //END FUNCTION
    }
}
void input (float Grade, float Hours, int &Count, float Pts, float Grade_Array[], float Hour_Array[], float Pts_Array[])
{
    /*************************************************************/
    if(Count < SIZE)
    {
        //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;
        Count++;
    }
    else
    {
        cout<<setw(42)<<"We are full"<<endl;
    }
}
int main()
{
	//local variables
	const string QUIT_NAME   = "-1";	//Init String Quit
  	int Count = 0;					    //Init Count to 0
  	string Name;						//Students Name
  	float Grade;						//Students Grade
  	int Hours;	   						//Credit Hours
  	float Pts;
    float Grade_Array[10];
    float Hour_Array[10];
    float Pts_Array[10];
    /*************************************************************/
    //Input Student Name
 	cout << setw(48) << "Students Name  : ";
 	cin >> Name;
 	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);

        //Input Student Name
        cout << setw(48) << "Students Name  : ";
        cin >> Name;
    }
    //My basic print:
    for(int i=0;i<Count;i++)
    {
        cout<<setw(42)<<"Grade["<<i<<"]: "<<Grade_Array[i]<<endl;
        cout<<setw(42)<<"Hour["<<i<<"]: "<<Hour_Array[i]<<endl;
        cout<<setw(42)<<"Points["<<i<<"]: "<<Pts_Array[i]<<endl;
    }
    /**************************************************************/
    return 0;
}//END Calc Students GPA 


I tend to not like prompting for input multiple times, but if want to use a pretest loop, this is an example.

Line 49,50, 69-73 is ensuring that you are not allocating data out of bound.

In this case, our count can only hold 2 members and will not allow more members after that.

However this is not a part of your design, but if not included, just ensure that the user will never enter more data than your array can hold. Else you'll be getting undefined behavior/segmentation fault.

Hopefully my example help resolve things.
Right now everything is working with the exception of invalid entries, such as a 5 or 7 in the grade or score, they are getting set in the array somehow. That being said if I input all valid numbers it works fine, but I have to get it to work properly. I will post the whole program so you can see what I mean.
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
using namespace std;

//place prototypes here
void input (int Count, float Grade_Array[], float Hour_Array[],
             float Pts_Array[], int SIZE);		 
float gpa (float& Total_Gradepts, float Pts_Array[], float& Total_Crdt, 
           float Hour_Array[], float Gpa);
bool validate(int Grade, int Hours);
void display (string Name, 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 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 (Count, Grade_Array, Hour_Array, Pts_Array, SIZE);
 			
		//Clear Screen 
  		system ("cls");
  		
  		//Call Function to Calculate GPA
        Gpa = gpa (Total_Gradepts, Pts_Array, Total_Crdt, Hour_Array, Gpa);
  		  		
  		//Call Function to Display the grade report
        display (Name, 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
************************Start Input Function*************************/
 void input (int Count, float Grade_Array[], float Hour_Array[], 
              float Pts_Array[], int SIZE)
{		
 	//local constant 
 	
	//local variables 
    float Grade;				//Students Grade
    float Hours;				//Credit Hours
    float Pts;					//Convert Grade to Pts	
	/*************************************************************/
	    
    //FOR (Each Grade Entered)
 	for (Count = 0; Count < SIZE; Count ++)
 	  {
 	      //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;   
 	      
 	  //END FOR    
      }
      
}     //END of Input Function
 ***********************Start Validate Function***********************/
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
	
	//local variables 
	/*************************************************************/
        		
 	//IF (Grade & Credit Hours are Valid)
 		if ((Grade >= GRADE_LOW && Grade <= GRADE_HIGH) &&
		   (Hours >= HOUR_MIN && Hours <= HOUR_MAX))
         {  
             //Return True   
 		     cout << endl;
             return true;    
 	     }	
        
        else 
         {
             //Display Error	
 		     cout << "\n" << setw(42) << "ERROR" << endl;
 		     cout << setw(42) << "-----" << endl;
             return false;
         }
             //Return False
             return false;   
             
}   //END of Validate Function
 *************************Start GPA Function**************************/
float gpa (float& Total_Gradepts, float Pts_Array[], float& Total_Crdt, 
           float Hour_Array[], float Gpa) 
{		
 	//local constants 
	//local variables 
	/*************************************************************/
	
	//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;
    
    return Gpa;

}   //END of GPA Function
 ************************Start Display Function***********************/
void display (string Name, 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 " << 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; 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 << endl;
    
    // Spaces from output
  	cout << "\n\n\n";
    
    //Pause Screen
    system ("pause");
  	
  	//Clear Screen 
  	system ("cls");

}   //END of Display Function 
1
2
//Call Function To Validate Input Data
	       validate (Grade, Hours);


You get a bool result returned from validate - but you aren't doing anything with it yet. While it is returning false - you want to prompt the user to re-enter the info before you store the values in the array.
Are you saying after line 156 I need to prompt for another input for grade and hours?


Not in the validate function - in the input function - you can do something with the result you get at line 117.

1
2
while (validate(grade,hours)==false)
// reenter info  

Last edited on
Trying right now
wildblue & Dput, I'm not even sure how to express my thanks to both of you for your patience and help. As you can tell I'm not very good at this on a good day. You both have been so very helpful. The entire program works the way it needs to and I would never have gotten there w/out your help. I'm beyond thankful & overwhelmed. It amazing how one final program can cause so much stress during finals. thank you both so much!!!!!!
Congrats - glad you got it working :)
Topic archived. No new replies allowed.
Pages: 12