1.11M Members

GPA Calculator C# Form - Help fix in loop

 
0
 

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 + "\n\n" + 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

 
0
 

Stuck in the for loop im assuming?

From a quick glance over may this be your issue?

for (int i = 1; i <= MAX_COURSES; i++) // ## Increments i by one here everytime loop occurs ##
            {
                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--; // ## Decrements i by one here counteracting loop? ##
                        }
                    }
                    continue;

Line 34 looks asif it counters out the incrementing functionality of the for loop by setting i back by 1 each loop iteration so it would get stuck on the value of '1' permantently.

Try removing this line and see what happens :)

 
0
 

Still not functioning the way i want it to...ugh. Can someone please tell me what's going on here.

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);
                            //Converts Hours Per Week to decimal and stores value
                            hrsPerWeek = Convert.ToInt32(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;
                            cumulativeHoursPerWeek += hrsPerWeek;
                            cumulativeGPA = cumulativeQualityPoints / cumulativeHoursPerWeek;
                            
                            // OUTPUT
                            numberOfCourses += i;
                            lblGPA.Text = gpa.ToString();
                            lblQualityPoints.Text = qualityPoints.ToString();
                            lblNumberOfCourses.Text = numberOfCourses.ToString();
                            lblCGPA.Text = cumulativeGPA.ToString();
                            
                            //Clears Grade Percentage And Hours Per Week
                            txtGradePercentage.Text = "";
                            txtHrsPerWeek.Text = "";
                            //Sets focus to GradePercentage text box
                            txtGradePercentage.Focus();
                            //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 + "\n\n" + myEx.GetType().ToString() + "\n" + myEx.StackTrace, "Exception");
                }
            }
        }
 
0
 

What exactly isnt functioning?

Line 44 in the above code you linked also looks iffy, your adding the total of i to numberOfCourses each time, so first loop is +1, then +2, then +3 etc. Shouldnt that just be numberOfCourses++ instead of numberOfCourses+=i?

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: