GPA Calculator

csharp Bahamas
  • 12 years ago

    I need some help. I want to be able to put in a letter grade within a textbox of a class and have the program know that A is 4.0 (A=4.0) then calclulate the GPA by adding all the class grades and dividing by seven which is the number of classes which equals the GPA.

    This is what it looks like:

    A = 4.0
    A- =3.7
    B+ = 3.3
    B = 3.0
    B- =2.7
    C+=2.3
    C=2.0
    C-=1.7
    NG = 0

     

    My code looks like this so far it just doesn't know how to interpret A as 4.0 and calculate the GPA:

     

     using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace gpacalc
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void label1_Click(object sender, EventArgs e)
            {

            }

            private void label4_Click(object sender, EventArgs e)
            {

            }

            private void textBox8_TextChanged(object sender, EventArgs e)
            {
               
            }


            private void button1_Click(object sender, EventArgs e)
            {
                int x = 0;


                try
                {

                    double g1 = Convert.ToDouble(textBox1.Text);
                    double g2 = Convert.ToDouble(textBox2.Text);
                    double g3 = Convert.ToDouble(textBox3.Text);
                    double g4 = Convert.ToDouble(textBox4.Text);
                    double g5 = Convert.ToDouble(textBox5.Text);
                    double g6 = Convert.ToDouble(textBox6.Text);
                    double g7 = Convert.ToDouble(textBox7.Text);
                    textBox8.Text = Convert.ToString((g1 + g2 + g3 + g4 + g5 + g6 + g7) / 7.0);
                    

                    
                }
                catch
                {
                    Console.Write(textBox8.Text = "ERROR");
                    Console.Beep(x = 266, x = 300);
                }

            }
            private void textBox1_TextChanged(object sender, EventArgs e)
            {

            }

            private void button2_Click(object sender, EventArgs e)
            {
                Close();
            }
        }
    }

     

    If you could help me on this it would be greatly appreciated. Big Smile 

  • 12 years ago

    There's nothing inherent in C# that's going to convert a letter to a numeric grade.  Do you really think the authors of the Convert.ToDouble method would have included that functionality?  What's the logical thing to do here?  Raed the string and if it's "A" then use the number 4.0; if it's "B" use the number 3.0; and so on.  It's just a series of 'if' statements.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” - Brian Kernighan