Results 1 to 4 of 4

Thread: question about an easy program

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    47

    Post

    I am working on an assignment that requires displaying 7 different proverbs to the user and asking the user to provide a true/false answer, then calculating scores and giving a specific response based on their score.

    I thought I was off to a good start, but I am stuck.
    I cannot figure out how to tally the score. This is what I have been working with: (my score function is WRONG!!)

    Dim Resp As String, Pvb1 As String, Pvb2 As String, Pvb3 As String, _
    Pvb4 As String, Pvb5 As String, Pvb6 As String, Pvb7 As String

    Option Explicit

    Private Sub cmdClick_Click()
    'to display proverbs and collect score
    Call DisplayProverbs
    Call Results
    End Sub

    Private Sub cmdExit_Click()
    End
    End Sub

    Public Function Score() As Integer
    'scoring for the given answers

    If Pvb1 And UCase(Resp) = "T" Then
    Score = Score + 1
    ElseIf Pvb2 And UCase(Resp) = "T" Then
    Score = Score + 1
    ElseIf Pvb3 And UCase(Resp) = "F" Then
    Score = Score + 1
    ElseIf Pvb4 And UCase(Resp) = "F" Then
    Score = Score + 1
    ElseIf Pvb5 And UCase(Resp) = "T" Then
    Score = Score + 1
    ElseIf Pvb6 And UCase(Resp) = "F" Then
    Score = Score + 1
    ElseIf Pvb7 And UCase(Resp) = "T" Then
    Score = Score + 1
    Else
    End If
    End Function

    Public Sub DisplayProverbs()
    'display proverbs to obtain answers
    Pvb1 = "The squeaky wheel gets the grease."
    Pvb2 = "Cry and you cry alone."
    Pvb3 = "Opposites attract."
    Pvb4 = "Spare the rod and spoil the child."
    Pvb5 = "Actions speak louder than words."
    Pvb6 = "Familiarity breeds contempt."
    Pvb7 = "Marry in haste, repent at leisure."
    Resp = Val(InputBox(Pvb1 + " (Reply with 'T' for True or 'F' for False.)"))
    Resp = Val(InputBox(Pvb2 + " (Reply with 'T' for True or 'F' for False.)"))
    Resp = Val(InputBox(Pvb3 + " (Reply with 'T' for True or 'F' for False.)"))
    Resp = Val(InputBox(Pvb4 + " (Reply with 'T' for True or 'F' for False.)"))
    Resp = Val(InputBox(Pvb5 + " (Reply with 'T' for True or 'F' for False.)"))
    Resp = Val(InputBox(Pvb6 + " (Reply with 'T' for True or 'F' for False.)"))
    Resp = Val(InputBox(Pvb7 + " (Reply with 'T' for True or 'F' for False.)"))

    End Sub


    Public Function Response(Score As Integer) As String

    Select Case Score
    Case Is >= 7
    Response = "Perfect"
    Case 5 To 6
    Response = "Excellent"
    Case Is < 5
    Response = "You might consider taking Psychology 101. "
    End Select
    End Function

    Public Sub Results()

    picProverb.Cls
    picProverb.Print Response(Score); " You answered"; Score; " correctly."
    End Sub



  2. #2
    Frenzied Member Mark Sreeves's Avatar
    Join Date
    Nov 1999
    Location
    UK
    Posts
    1,845

    Post

    This works!

    I'm not saying it's well structured though!

    Code:
    Option Explicit
    
    
    Dim pvb(7) As proverb
    
    Private Type proverb
      phrase As String
      value As Integer
    End Type
    
    
    Private Sub cmdClick_Click()
    'to display proverbs and collect score
    DisplayProverbs
    
    End Sub
    
    Private Sub cmdExit_Click()
    End
    End Sub
    
    
    
    Public Sub DisplayProverbs()
    'display proverbs to obtain answers
    Dim i As Integer
    Dim score As Integer
    For i = 0 To 6
    
      If MsgBox(pvb(i).phrase & vbCrLf & vbCrLf & " (Reply with 'YES' for True or 'NO' for False.)", vbYesNo) = pvb(i).value Then
        score = score + 1
      End If
    
     Next i
     
     
    Picture1.Cls
    Picture1.Print Response(score) & vbCrLf & vbCrLf & " You answered " & score & " correctly."
    End Sub
    
    
    Public Function Response(score As Integer) As String
    
    Select Case score
    Case Is >= 7
    Response = "Perfect"
    Case 5 To 6
    Response = "Excellent"
    Case Is < 5
    Response = "You might consider taking Psychology 101. "
    End Select
    End Function
    
    
    
    
    Private Sub Form_Load()
    pvb(0).phrase = "The squeaky wheel gets the grease."
    pvb(0).value = vbYes
    
    pvb(1).phrase = "Cry and you cry alone."
    pvb(1).value = vbYes
    
    pvb(2).phrase = "Opposites attract."
    pvb(2).value = vbNo
    
    pvb(3).phrase = "Spare the rod and spoil the child."
    pvb(3).value = vbNo
    
    pvb(4).phrase = "Actions speak louder than words."
    pvb(4).value = vbYes
    
    pvb(5).phrase = "Familiarity breeds contempt."
    pvb(5).value = vbNo
    
    pvb(6).phrase = "Marry in haste, repent at leisure."
    pvb(6).value = vbYes
    End Sub
    Mark
    -------------------

  3. #3
    Lively Member
    Join Date
    Mar 2000
    Location
    Fort Lauderdale, FL USA
    Posts
    112
    Ok, this is what I found and what I did to fix it:

    1. Resp variable should be an array. They way you had it, everytime someone answers a proverb, the variable will change to that response. The array will hold all the answers.

    2. Use a select statement and a for/next in your scoring function. The way you had it required the proverb AND the response to both be "T" which will never happen. By making the resp variable an array, you can loop through them and update the score.

    3. In your DisplayProverbs procedure, there are two things wrong. First, the Val() function takes a string of numbers and converts them into their numeric value. But because you are accepting the answers as either "T" or "F", there is no reason to convert these alpha characters into numerals. Second, to combine string use "&", not the "+" sign. The plus sign adds the variables together, the "&" combines the two to form one long string.

    I think that's all I found. There's other things you can do to make it more streamlined and functional, but this will at least get you going. The reworked code is below. Any questions or comments, let me know.

    Dim Resp(6) As String, Pvb1 As String, Pvb2 As String, Pvb3 As String, _
    Pvb4 As String, Pvb5 As String, Pvb6 As String, Pvb7 As String

    Option Explicit

    Private Sub cmdClick_Click()
    'to display proverbs and collect score
    Call DisplayProverbs
    Call Results
    End Sub

    Private Sub cmdExit_Click()
    End
    End Sub

    Public Function Score() As Integer
    'scoring for the given answers
    Dim i As Integer

    For i = 0 To 6 Step 1
    Select Case i
    Case 0, 1, 4, 6
    If UCase(Resp(i)) = "T" Then
    Score = Score + 1
    End If
    Case 2, 3, 5
    If UCase(Resp(i)) = "F" Then
    Score = Score + 1
    End If
    End Select
    Next

    End Function
    Public Sub DisplayProverbs()
    'display proverbs to obtain answers
    Pvb1 = "The squeaky wheel gets the grease."
    Pvb2 = "Cry and you cry alone."
    Pvb3 = "Opposites attract."
    Pvb4 = "Spare the rod and spoil the child."
    Pvb5 = "Actions speak louder than words."
    Pvb6 = "Familiarity breeds contempt."
    Pvb7 = "Marry in haste, repent at leisure."
    Resp(0) = InputBox(Pvb1 & "Reply with 'T' for True or 'F' for False.")
    Resp(1) = InputBox(Pvb2 & "Reply with 'T' for True or 'F' for False.")
    Resp(2) = InputBox(Pvb3 & "Reply with 'T' for True or 'F' for False.")
    Resp(3) = InputBox(Pvb4 & "Reply with 'T' for True or 'F' for False.")
    Resp(4) = InputBox(Pvb5 & "Reply with 'T' for True or 'F' for False.")
    Resp(5) = InputBox(Pvb6 & "Reply with 'T' for True or 'F' for False.")
    Resp(6) = InputBox(Pvb7 & "Reply with 'T' for True or 'F' for False.")

    End Sub


    Public Function Response(Score As Integer) As String

    Select Case Score
    Case Is >= 7
    Response = "Perfect"
    Case 5 To 6
    Response = "Excellent"
    Case Is < 5
    Response = "You might consider taking Psychology 101. "
    End Select
    End Function

    Public Sub Results()

    picProverb.Cls
    picProverb.Print Response(Score); " You answered"; Score; " correctly."
    End Sub


  4. #4

    Thread Starter
    Member
    Join Date
    Feb 2000
    Posts
    47

    Post

    WOW, both responses (Mark Streeves and damonous)
    work perfectly!

    I will probably use the suggestions from damonous,
    though pointers from Mark (I see how much more efficiently
    my code can work)will be absorbed for later use.

    I appreciate all the help I can get, as I am a work at home
    adult taking off-campus courses!

    Thanks All!

    Terry

    Edited by koperski on 03-08-2000 at 12:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width