The form is supposed to be able to take your their percentage grade out of 100 and the number of hours per week of each of their courses. The application will determine and output the GPA equivalent of each grade entered as well as the CGPA of all the courses entered. The maximum number of courses any student would take is is 40.
The user enters data the first time and presses calculate, GPA is updated, Quality Points is updated, Number of Courses adds 1, cumulative grade point is updated. when they enter another value and press calculate again these values are updates. & they can do this 40 times.
Program is stuck in a loop…I don’t know how to get it out. I am trying to have the user enter 0 to 40 grade percentages.
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; namespace GPATeamLab{ public partial class frmGPACalculator : Form { public frmGPACalculator() { InitializeComponent(); } private void tipDCGPACalculator_Popup(object sender, PopupEventArgs e) { } private void btnExit_Click(object sender, EventArgs e) { //Closes form when exit button is clicked this.Close(); } private void btnCalculate_Click(object sender, EventArgs e) //Calculate button is clicked { //Declarations //Variables double gpa; //holds value for gpa double qualityPoints; //hold value for quality points int numberOfCourses = 0; double cumulativeQualityPoints = 0; double cumulativeHoursPerWeek = 0; double cumulativeGPA = 0; int gradePercentage; int hrsPerWeek; //Constants //int MIN_GRADE = 0; //int MAX_GRADE = 100; int MAX_COURSES = 40; //INPUT for (int i = 1; i <= MAX_COURSES; i++) { try { { if (IsValidData()) { // gets user input from from //Converts Grade Percentage to decimal and stores value gradePercentage = Convert.ToInt32(txtGradePercentage.Text); //gradePercentage = int.Parse(txtGradePercentage.Text); //Converts Hours per Week to decimal and stores value hrsPerWeek = Convert.ToInt32(txtHrsPerWeek.Text); //hrsPerWeek = int.Parse(txtHrsPerWeek.Text); //PROCESS //Calls calculateGPA method gpa = calculateGPA(gradePercentage); //Calculates numberOfCourses++; //Adds the cumulative number of quality points to the quality points calculation qualityPoints = gpa * hrsPerWeek; cumulativeQualityPoints += qualityPoints; cumulativeGPA = cumulativeQualityPoints / cumulativeHoursPerWeek; // OUTPUT lblGPA.Text = gradePercentage.ToString(); lblQualityPoints.Text = qualityPoints.ToString(); lblNumberOfCourses.Text = numberOfCourses.ToString(); lblCGPA.Text = cumulativeGPA.ToString(); //Clears input boxes txtGradePercentage.Text = “”; txtHrsPerWeek.Text = “”; //Sets focus to Grade Percentage text box txtGradePercentage.Focus(); i–; } } continue; } catch (FormatException myFormatEx) { MessageBox.Show(myFormatEx.Message + “nInvalid numeric format. Please check all entries.”, “Entry Error”); } catch (OverflowException myOverflowEx) { MessageBox.Show(myOverflowEx.Message + “nOverflow error. Please enter smaller values.”, “Entry Error”); } catch (Exception myEx) { MessageBox.Show(myEx.Message + “nn” + myEx.GetType().ToString() + “n” + myEx.StackTrace, “Exception”); } } } private static double calculateGPA(int gradePercentage) { const double MIN_GRADE = 0; const double MAX_GRADE = 100; const double APLUS = 90; const double a = 85; const double AMINUS = 80; const double BPLUS = 75; const double B = 70; const double CPLUS = 65; const double C = 60; const double DPLUS = 55; const double D = 50; double gpa = 0; const double GPA_5 = 5.0; const double GPA_4_5 = 4.5; const double GPA_4 = 4.0; const double GPA_3_5 = 3.5; const double GPA_3 = 3; const double GPA_2_5 = 2.5; const double GPA_2 = 2.0; const double GPA_1_5 = 1.5; const double GPA_1 = 1.0; const double GPA_0 = 0; if (gradePercentage >= MIN_GRADE && gradePercentage <= MAX_GRADE) { if (gradePercentage >= APLUS) { gpa = GPA_5; } else if (gradePercentage >= A) { gpa = GPA_4_5; } else if (gradePercentage >= AMINUS) { gpa = GPA_4; } else if (gradePercentage >= BPLUS) { gpa = GPA_3_5; } else if (gradePercentage >= B) { gpa = GPA_3; } else if (gradePercentage >= CPLUS) { gpa = GPA_2_5; } else if (gradePercentage >= C) { gpa = GPA_2; } else if (gradePercentage >= DPLUS) { gpa = GPA_1_5; } else if (gradePercentage >= D) { gpa = GPA_1; } else { gpa = GPA_0; } } return gpa; } //Event handler that clears all cumulative totals private void btnClearTotal_Click(object sender, EventArgs e) { MessageBox.Show(“This button clears all Cumulative Totals”); lblGPA.Text = “”; lblQualityPoints.Text = “”; lblNumberOfCourses.Text = “”; lblCGPA.Text = “”; txtGradePercentage.Focus(); } private bool IsPresent(TextBox textBox, string name) { // this method checks any textbox for a required entry bool valid = true; // assuming valid if (textBox.Text == “”) // check to see if there is an entry { MessageBox.Show(name + ” is a required field.”, “Entry Error”); textBox.Focus(); // set the focus valid = false; } return valid; } public bool IsDecimal(TextBox textBox, string name) { // this method checks any textbox for a valid decimal entry bool valid = true; // assuming valid try { Convert.ToDecimal(textBox.Text); } catch (FormatException) { MessageBox.Show(name + ” must be a decimal value.”, “Entry Error”); textBox.SelectAll(); // Select the user’s entry valid = false; } catch (OverflowException myOverflowEx) { throw myOverflowEx; // throw to the calling method to handle } catch (Exception myEx) { throw myEx; // throw to the calling method to handle } return valid; } public bool IsInt32(TextBox textBox, string name) { // this method checks any textbox for a valid integer entry bool valid = true; // assuming valid try { Convert.ToInt32(textBox.Text); } catch (FormatException) { MessageBox.Show(name + ” must be an integer.”, “Entry Error”); textBox.SelectAll(); valid = false; } catch (OverflowException myOverflowEx) { throw myOverflowEx; // throw to the calling method to handle } catch (Exception myEx) { throw myEx; // throw to the calling method to handle } return valid; } public bool IsWithinRange(TextBox textBox, string name, decimal min, decimal max) { // this method makes sure a textbox is in a valid range decimal number; bool valid = true; try { number = Convert.ToDecimal(textBox.Text); // try to convert if (number < min || number > max) // not in range { MessageBox.Show(name + ” must be between ” + min + ” and ” + max + “.”, “Entry Error”); textBox.SelectAll(); valid = false; } } catch (FormatException myFormatEx) { textBox.SelectAll(); // Select the user’s entry throw myFormatEx; // throw to the calling method to handle } catch (OverflowException myOverflowEx) { throw myOverflowEx; // throw to the calling method to handle } catch (Exception myEx) { throw myEx; // throw to the calling method to handle } return valid; } private bool IsValidData() { // this method checks all the textboxes on the form for valid entries const int MIN_VALUE = 0; const int MAX_VALUE = 100; return // Validate the Grade Percentage text box IsPresent(txtGradePercentage, “Grade Percentage”) && IsDecimal(txtGradePercentage, “Grade Percentage”) && IsWithinRange(txtGradePercentage, “Grade Percentage”, MIN_VALUE, MAX_VALUE) && // Validate the Hours per Week Rate text box IsPresent(txtHrsPerWeek, “Hours per Week”) && IsDecimal(txtHrsPerWeek, “Hours per Week”) && IsWithinRange(txtHrsPerWeek, “Hours per Week”, MIN_VALUE, MAX_VALUE); } }}
Here is a picture of the form : http://i.stack.imgur.com/JEz4M.jpg
GPA Calculator C# Form – Help fix in loop