5 Replies - 348 Views - Last Post: 05 July 2013 - 09:44 AM Rate Topic: -----

#1 ajparkerv1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-July 13

First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 09:52 AM

Greetings,

New to programming. I'm taking a 16 week CINS 113 course and jamming it into 8 weeks over the summer. I'm a little lost.

My assignment is to take three test scores, average them, and give a letter grade. Seems easy enough?

Here is the function.

    Private Function CalculateGrade _
(ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, _
ByVal testScore3 As Decimal) As String
        'calculates the grade

        Dim intGrade As Integer
        Dim decTestScore1 As Decimal
        Dim decTestScore2 As Decimal
        Dim decTestScore3 As Decimal
        intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3
        Dim strAnswer As String

        If intGrade >= 90 Then
            strAnswer = "A"
        ElseIf intGrade >= 80 Then
            strAnswer = "B"
        ElseIf intGrade >= 70 Then
            strAnswer = "C"
        ElseIf intGrade >= 60 Then
            strAnswer = "D"
        Else   '
            strAnswer = "F"
        End If

        Return strAnswer '

    End Function


Here is the button display click procedure:

Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        ' prompts user to input 3 testscores,
        ' then calls a function to calculate them,
        ' then displays the grade

        Dim decTestScore1 As Decimal
        Dim decTestScore2 As Decimal
        Dim decTestScore3 As Decimal
        Dim decAvgGrade As Decimal
        Dim intGrade As Integer

        decAvgGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3
        intGrade = (decAvgGrade)

        lblGrade.Text = intGrade.ToString("N2")


Whenver I try to call the function as intGrade = CalculateGrade(decAvegGrade) I get an error. Without it, there are no visable errors until I run it. Then I get this error:

A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe
A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe
A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe
A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe
A first chance exception of type 'System.NotImplementedException' occurred in Grade Project.exe

I'm very, very close on this one. Very very close.

Can anyone please help?

Is This A Good Question/Topic? 0
  • +

Replies To: First Chance Exception in Grade Calculator Code

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8947
  • View blog
  • Posts: 33,544
  • Joined: 12-June 08

Re: First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 10:16 AM

First off - order of operations in line 12..

12	        decAvgGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3


You are adding decTestScore1 to decTestScore2 to the result of "decTestScore3 / 3". Doesn't seem right.
Same here:
10	        intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3


What are you doing here?
13	        intGrade = (decAvgGrade)



You do know that the default value for a decimal is 0, right? If you don't assign your incoming parameters (the testscore1, testscore2, etc) to the 'dectestcore1', 'dectestscore2', etc that you are just adding up zeros in line 10.
02	(ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, _
03	ByVal testScore3 As Decimal) As String
04	        'calculates the grade
05	 
06	        Dim intGrade As Integer
07	        Dim decTestScore1 As Decimal
08	        Dim decTestScore2 As Decimal
09	        Dim decTestScore3 As Decimal
10	        intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3

Was This Post Helpful? 1
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 10:34 AM

Quote

I'm very, very close on this one. Very very close.

Unfortunately, you are not close. Fortunately, you have made the attempt, and have asked the right questions.

The first thing you should do is to add the line "Option Strict On" above the Class statement. See the link in my signature for why you should. After you put the line in there, you can fix any errors that show up with a wiglly underline.

Now... here's what I see in your code.

1. According to your comments in the Button Click Sub, you are prompting the user for three testscores, but you are not. You can use InputBoxes or TextBoxes for that.

2. You Dim 3 decimal variables, which is fine (though they do not have to be Decimal if your possible test scores do not have decimal points); they can be integers. The problem here is that you never assign any values to them, so they are all initialized to 0.

3.You then add them together, and divide by three, assigning the result to decAvgGrade. This again is fine, but 0+0+0 divided by 3 is always going to be 0.

4 Your comments further state that you are calling a function, but you are not. You are trying to call it, I think, with the line: intGrade = (decAvegGrade) , but all that line does is to assign decAvgGrade to intGrade. Yiu need to actually call the function. Reading the function itself tells us that it is called like this:

   <somevariable> = CalculateGrade(testScore1,testScore2, testScore3)


Now, <somevariable> has to be a String variable if your function returns a string, which its definition says it does.

5. In your function, testScore1, testScore2 ,testScore3 are already declared, by virtue of being in the parameter list in the Fnction definition. BUt shortly after that, you DIm 3 Decimal variables, and never assign values to them. You then (again) calculate 0+0+0 divided by 3, which will always be 0.

6. You should also learn about operator precedence, and use parentheses to force the precedence. in your calculation of the average, scores of 40, 50, and 60 will result in an average of 110, which is obviously wrong.

Get back to us if you have any further difficulties.
Was This Post Helpful? 1
  • +
  • -

#4 ajparkerv1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-July 13

Re: First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 10:47 AM

View Postlar3ry, on 04 July 2013 - 10:34 AM, said:

Quote

I'm very, very close on this one. Very very close.

Unfortunately, you are not close. Fortunately, you have made the attempt, and have asked the right questions.

The first thing you should do is to add the line "Option Strict On" above the Class statement. See the link in my signature for why you should. After you put the line in there, you can fix any errors that show up with a wiglly underline.

Now... here's what I see in your code.

1. According to your comments in the Button Click Sub, you are prompting the user for three testscores, but you are not. You can use InputBoxes or TextBoxes for that.

2. You Dim 3 decimal variables, which is fine (though they do not have to be Decimal if your possible test scores do not have decimal points); they can be integers. The problem here is that you never assign any values to them, so they are all initialized to 0.

3.You then add them together, and divide by three, assigning the result to decAvgGrade. This again is fine, but 0+0+0 divided by 3 is always going to be 0.

4 Your comments further state that you are calling a function, but you are not. You are trying to call it, I think, with the line: intGrade = (decAvegGrade) , but all that line does is to assign decAvgGrade to intGrade. Yiu need to actually call the function. Reading the function itself tells us that it is called like this:

   <somevariable> = CalculateGrade(testScore1,testScore2, testScore3)


Now, <somevariable> has to be a String variable if your function returns a string, which its definition says it does.

5. In your function, testScore1, testScore2 ,testScore3 are already declared, by virtue of being in the parameter list in the Fnction definition. BUt shortly after that, you DIm 3 Decimal variables, and never assign values to them. You then (again) calculate 0+0+0 divided by 3, which will always be 0.

6. You should also learn about operator precedence, and use parentheses to force the precedence. in your calculation of the average, scores of 40, 50, and 60 will result in an average of 110, which is obviously wrong.

Get back to us if you have any further difficulties.



This is embarrassing. You (not sure if guys or gals) people are EXTREMELY helpful. Thank you so much.

re: 1-3 I thought the user iputs the testscores and thus assigns the variables?

I need to try to wrap my head around this.

I thought the user assigns the variables when they input their scores?

View Postmodi123_1, on 04 July 2013 - 10:16 AM, said:

First off - order of operations in line 12..

12	        decAvgGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3


You are adding decTestScore1 to decTestScore2 to the result of "decTestScore3 / 3". Doesn't seem right.
Same here:
10	        intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3


What are you doing here?
13	        intGrade = (decAvgGrade)



You do know that the default value for a decimal is 0, right? If you don't assign your incoming parameters (the testscore1, testscore2, etc) to the 'dectestcore1', 'dectestscore2', etc that you are just adding up zeros in line 10.
02	(ByVal testScore1 As Decimal, ByVal testScore2 As Decimal, _
03	ByVal testScore3 As Decimal) As String
04	        'calculates the grade
05	 
06	        Dim intGrade As Integer
07	        Dim decTestScore1 As Decimal
08	        Dim decTestScore2 As Decimal
09	        Dim decTestScore3 As Decimal
10	        intGrade = decTestScore1 + decTestScore2 + decTestScore3 / 3


Line 13 I'm trying to call the function. I agree it doesn't seem right at all. Very little of this does.

Ugh.
Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: First Chance Exception in Grade Calculator Code

Posted 04 July 2013 - 12:29 PM

View Postajparkerv1, on 04 July 2013 - 11:47 AM, said:

This is embarrassing. You (not sure if guys or gals) people are EXTREMELY helpful. Thank you so much.

No need for embarrassment. We've all been there. Everyone has to start somewhere, and we are here because we like to help those who want to learn.

Quote

re: 1-3 I thought the user iputs the testscores and thus assigns the variables?

I need to try to wrap my head around this.

I thought the user assigns the variables when they input their scores?

The programmer has to ask the user for input in one way or another. In your program, you need three testscores, so you need to have some way the user can put them in. Otherwise, your program doesn't know what the scores are. There are a number of ways to accomplish this. You can use three TextBoxes, and position a Label next to each one, then provide a Button to start the process of gatting the input into variables and calculating the averag.

Or, you could use an InputBox within a loop, and gather the three values that way.

Check out the MSDN Library, where you can search for any control names (InputBox, TextBox, etc.), or any object, method or property, to get documentation and/or examples of usage.

Quote

Line 13 I'm trying to call the function. I agree it doesn't seem right at all. Very little of this does.

As I mentioned, you call a Function by assigning its return value to a variable. Have a look at our Subroutine and Function Tutorial for some ideas.
Was This Post Helpful? 0
  • +
  • -

#6 ajparkerv1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-July 13

Re: First Chance Exception in Grade Calculator Code

Posted 05 July 2013 - 09:44 AM

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1