11 Replies - 7253 Views - Last Post: 02 December 2012 - 09:59 PM Rate Topic: -----

#1 x RadicalAura x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-December 12

High and Low Test Scores

Posted 01 December 2012 - 08:48 PM

I am programming an application in VB that allows the user to input test scores and then it automatically calculates the average, high score, low score, and a grade distribution (A, B, C, D, F) for the entire class. I have the average, high score, and the distribution working for the most part. However, I am struggling with the low score. The high score works perfectly fine but by using the same logic to get the low score, it usually will leave the low score text box either blank or as "0" once I convert it back from an integer to a string. Also, my grade distribution label is a bit wacky. My instructor requested that we create one label for all letter grades and use concatenation to tie multiple strings together. For the most part, it works, but the problem I'm having is it's delayed. For instance, the first time I enter a number, it doesn't assign the grade to any letter grade value, but when I input a second score, it tracks it perfectly fine. The same applies to entering another score that would fall within a different letter grade category (e.g. 88 is counted as a "B" but then if I enter 70, for instance, immediately afterwards, it still tracks it as a B and will only be corrected after I enter another score.) Sorry for typing so much, I hope this is somewhat coherent. Thanks in advance for any advice, I'm new to VB.
 Dim highInteger As Integer
    Dim lowInteger As Integer
    Dim enteredInteger As Integer
    Dim countA As Integer
    Dim countB As Integer
    Dim countC As Integer
    Dim countD As Integer
    Dim countF As Integer

'High and Low Score Code

        If enteredInteger > highInteger Then
            highInteger = enteredInteger
            highTextBox.Text = highInteger.ToString()
        End If

        If enteredInteger <= 100 Then
            lowInteger = enteredInteger
            lowTextBox.Text = lowInteger.ToString()

        End If


        'Grade Distribution Label Code


        gradesLabel.Text = "A's" & " " & countA.ToString() & " " & " " & "B's" & " " & countB.ToString() & " " & " " & "C's" & " " & countC.ToString() & " " & " " & "D's" & " " & countD.ToString() & " " & " " & "F's" & countF.ToString()

        If enterButton.Enabled = True Then
        End If
        If enteredInteger >= 90 Then
            countA = countA + 1
        Else
            If enteredInteger >= 80 And enteredInteger < 90 Then
                countB = countB + 1
            Else
                If enteredInteger >= 70 And enteredInteger < 80 Then
                    countC = countC + 1
                Else
                    If enteredInteger >= 60 And enteredInteger < 70 Then
                        countD = countD + 1
                    Else
                        countF = countF + 1
                    End If
                End If
            End If
        End If


By the way, just noticed that my low score code is completely off. I know, I know. It's essentially stating that any number less than or equal to 100 is the low score, which is obviously not what I am looking for. I just put that there temporarily because I was tired of seeing "0" or nothing. My original low score code looked like this:
 
If enteredInteger < lowInteger Then
lowInteger = enteredInteger
lowTextBox.Text = lowInteger.ToString()


This post has been edited by macosxnerd101: 01 December 2012 - 08:54 PM
Reason for edit:: Fixed code tags


Is This A Good Question/Topic? 0
  • +

Replies To: High and Low Test Scores

#2 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: High and Low Test Scores

Posted 01 December 2012 - 10:18 PM

View Postx RadicalAura x, on 01 December 2012 - 09:48 PM, said:

For the most part, it works, but the problem I'm having is it's delayed. For instance, the first time I enter a number, it doesn't assign the grade to any letter grade value, but when I input a second score, it tracks it perfectly fine.

You are assigning the string to the label BEFORE you decide what's in the various countX variables.

Oh, and these lines do nothing at all.

        If enterButton.Enabled = True Then
        End If



You should also look at using a Select Case. They are much easier to write, much easier to read, and are less prone to programmer errors.

Consider:

        Select Case enteredinteger
            Case 90 To 100
                countA += 1
            ...
        End Select


Was This Post Helpful? 0
  • +
  • -

#3 x RadicalAura x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-December 12

Re: High and Low Test Scores

Posted 01 December 2012 - 10:42 PM

Thanks for the advice. I tried the Select Case method you mentioned and it seems logical by looking at the syntax, but it doesn't impact my concatenated label of strings whatsoever now. They all (A, B, C, D, and F) just remain 0, no matter what number I enter. I've now made
Select Case enteredInteger
Case 90 to 200
countA += 1
Case 80 to 89
countB += 1
Case 70 to 79
countC += 1
Case 60 to 69
countD += 1
Case 0 to 59
countF += 1



I just don't understand why there's latency. =/

NEVERMIND, got it! I'm stupid, sorry. Just flipped the order of the label with the Select Case code.

*** Edit ***

No need to quote the post above yours.

This post has been edited by GunnerInc: 01 December 2012 - 10:52 PM
Reason for edit:: Removed quote

Was This Post Helpful? 0
  • +
  • -

#4 x RadicalAura x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-December 12

Re: High and Low Test Scores

Posted 01 December 2012 - 11:01 PM

Do you have any ideas as to why my low score isn't working properly from the code I posted? The high score code works perfectly fine. Is it some kind of dispute between lowInteger and highInteger over enteredInteger? Basically, if the enteredInteger is less than the highInteger, it's automatically designated as the lowInteger, which is obviously not what I need it to do. Would a loop be necessary for this? Thanks again.
Was This Post Helpful? 0
  • +
  • -

#5 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: High and Low Test Scores

Posted 01 December 2012 - 11:06 PM

One small point. It doesn't matter in your case (no pun intended), but after all the specific Case statements, you can use Case Else, to cover everything not specified above it.
   Select Case txt_Answer_to_Who_da_man.text
     Case "lar3ry"
       Print "I agree!"
     Case "x RadicalAura x"
       Print "Oh yeah!"
     Case else
       Print "You sure? Never heard of him"
   End Select


Was This Post Helpful? 0
  • +
  • -

#6 x RadicalAura x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-December 12

Re: High and Low Test Scores

Posted 01 December 2012 - 11:51 PM

Any ideas on the low score, anyone? I'm at a complete loss.
'High and Low Score Code

        If enteredInteger > highInteger Then
            highInteger = enteredInteger
            highTextBox.Text = highInteger.ToString()
        End If

        If enteredInteger < lowInteger Then
            lowInteger = enteredInteger
            lowTextBox.Text = lowInteger.ToString()
        End If


Was This Post Helpful? 0
  • +
  • -

#7 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: High and Low Test Scores

Posted 02 December 2012 - 12:45 PM

View Postx RadicalAura x, on 02 December 2012 - 12:51 AM, said:

Any ideas on the low score, anyone? I'm at a complete loss.
'High and Low Score Code
        If enteredInteger < lowInteger Then
            lowInteger = enteredInteger
            lowTextBox.Text = lowInteger.ToString()
        End If


Um, what value does lowInteger have when you first compare it with enteredInteger? Try a breakpoint and single-step, looking at variables if you're unsure.

Oh, and your Select Case goes 90 to 200.
Was This Post Helpful? 0
  • +
  • -

#8 x RadicalAura x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-December 12

Re: High and Low Test Scores

Posted 02 December 2012 - 08:16 PM

View Postlar3ry, on 02 December 2012 - 12:45 PM, said:

View Postx RadicalAura x, on 02 December 2012 - 12:51 AM, said:

Any ideas on the low score, anyone? I'm at a complete loss.
'High and Low Score Code
        If enteredInteger < lowInteger Then
            lowInteger = enteredInteger
            lowTextBox.Text = lowInteger.ToString()
        End If


Um, what value does lowInteger have when you first compare it with enteredInteger? Try a breakpoint and single-step, looking at variables if you're unsure.

Oh, and your Select Case goes 90 to 200.

I'm sorry, could you just elaborate a bit more? I'm not sure what you mean by a breakpoint and single-step. I just started VB in the last week of October. From what I can tell, it just seems like lowInteger and highInteger are conflicting over each other in determining how enteredInteger affects them. Not sure if I'm right on that or not.
Was This Post Helpful? 0
  • +
  • -

#9 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: High and Low Test Scores

Posted 02 December 2012 - 09:18 PM

View Postx RadicalAura x, on 02 December 2012 - 09:16 PM, said:

'High and Low Score Code
        If enteredInteger < lowInteger Then
            lowInteger = enteredInteger
            lowTextBox.Text = lowInteger.ToString()
        End If


I'm sorry, could you just elaborate a bit more? I'm not sure what you mean by a breakpoint and single-step. I just started VB in the last week of October. From what I can tell, it just seems like lowInteger and highInteger are conflicting over each other in determining how enteredInteger affects them. Not sure if I'm right on that or not.

So, I gather you are unsure. Think a moment. What value does lowInteger have when you first declare it. Have you set lowInteger to any value since it was declared? When is the first time you try to set lowInteger to a value?

lowInteger and highInteger are not affecting one another. If you are not setting lowInteger to a value in the code above, it can only mean that you are not executing the statements between If and End If. Why do you suppose that is? Obviously, the first time through the check, enteredInteger is the highest one entered, as well as the lowest one entered, so they should both show up in the TextBox the first time you go through this code. Somehow, the program thinks that enteredInteger is not smaller than lowInteger.

That's why I asked you what the value is when you get to the If.

As for breakpoints and single-stepping, these are fundamental debugging tools, and are skills you SHOULD have been taught on the first day of your course. Unfortunately, most folks taking courses are never taught these skills.

Luckily, we have a great tutorial on debugging, and you will be doing yourself an enormous favour by reading and understanding it. Some of the Function-Keys listed for single-stepping, stepping-over, and stepping-out, etc. are not applicable to Visual Studio 2010, but you can find out which F-keys to use via the Debug menu in Visual Studio.

To set a breakpoint, you can click on the column in the far left of the code editor window. A red sphere-ish thing will appear, and the line will be highlighted. When you run the program, it will stop when that line is about to be executed. At that point, you can hover your mouse over variables (like lowInteger, hint! hint!), and see if they contain what you think they should contain.

Single-stepping will move you from one statement to the next, as they are about to be executed.

Get back to us and tell us how you made out.
Was This Post Helpful? 0
  • +
  • -

#10 x RadicalAura x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-December 12

Re: High and Low Test Scores

Posted 02 December 2012 - 09:34 PM

Thanks for the suggestion, lar3ry. So I did as you said, and set breakpoints around my code to calculate the low score. When I hover over enteredInteger, it says that the value of it is whatever I enter into enteredTextBox as enteredInteger, but for lowInteger it says 0. I am perplexed because this isn't the case with the high score. Also set break points around that code and it says that enteredInteger is again, whatever I entered but the highInteger accurately says 22. The conventions I used to establish lowInteger and highInteger are the same, I guess that's why I'm confused. I don't see why the code behind lowInteger should be any different that highInteger, with the exception of the equality signs being inverted.
Was This Post Helpful? 0
  • +
  • -

#11 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: High and Low Test Scores

Posted 02 December 2012 - 09:56 PM

Well, I guess I'll have to spell it out for you. When you declared lowInteger and highInteger, they both had the same value. That value was 0. When you checked to see if enteredInteger was greater than highInteger, of course it satisfied the If, because the number you entered was definitely greater than 0.

But when you checked to see if lowInteger was less than lowInteger, it wasn't (unless you entered a negative number, of course), because the number you entered was not less than 0.

So, the problem does not lie with the comparison operator (<), but with the initial value of one of the variables you are comparing.

Up there, where you Dim lowInteger as Integer, you need to add something.

Dim lowInteger as Integer = 100, or you could make it 5000, or 36987, because whatever number you enter for a grade will be smaller.
Was This Post Helpful? 0
  • +
  • -

#12 x RadicalAura x   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 01-December 12

Re: High and Low Test Scores

Posted 02 December 2012 - 09:59 PM

View Postlar3ry, on 02 December 2012 - 09:56 PM, said:

Well, I guess I'll have to spell it out for you. When you declared lowInteger and highInteger, they both had the same value. That value was 0. When you checked to see if enteredInteger was greater than highInteger, of course it satisfied the If, because the number you entered was definitely greater than 0.

But when you checked to see if lowInteger was less than lowInteger, it wasn't (unless you entered a negative number, of course), because the number you entered was not less than 0.

So, the problem does not lie with the comparison operator (<), but with the initial value of one of the variables you are comparing.

Up there, where you Dim lowInteger as Integer, you need to add something.

Dim lowInteger as Integer = 100, or you could make it 5000, or 36987, because whatever number you enter for a grade will be smaller.

Thank you for all the help! The program now works 100%. :) I will keep what you mentioned in mind in the future as I'm declaring variables and using If/Else statements.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1