Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Visual Basic, Loops?

    Right here is the program that i am writing i posted before about the input box.
    I need the program to loop, which i have no idea how, i have afew ideas but i don't no exactly how i am ment to loop it. I need is so everything is printed from the last loop before it starts it new loop.


    Code:
     
    
    Public Sub main()
    
    ' Store variables for intStudentNumber
    '                     strStudentName
    
    'Store the variables for intResults
    '                        intCount
    '                        intTotal
    '                        sngScore
    '                        message
    
    'Student number and name
    Dim intStudentNumber As Integer
    Dim strStudentName As String
    'Store message
    Dim intmessageNumber As Integer
    Dim strmessageName As String
    'store message for "Studentnumber"
    Dim strmessage As String
    
    'Student Score
    Dim intResults As Integer
    Dim intCount As Integer
    Dim intTotal As Integer
    Dim sngScore As Single
    
    'Store message
    Dim message As String
    
    
    ' Get Student Number & Name
    
    intStudentNumber = InputBox(" Student Number ")
    
    strStudentName = InputBox("Please Student Name Here")
    
    
    'get input from user for the ten values, add each to the total
    'note the layout of the code, indent the commands within the loop
    
    For intCount = 1 To 6
    intResult = InputBox("Please enter students results-")
    intTotal = intTotal + intResult
    Next intCount
    
    'Calculate average
    
    sngScore = intTotal
    
    'Create Message for strmessage
    strmessage = "Student Number = "
    
    'Create message for student number & name
    
    intmessageNumber = intStudentNumber
    strmessageName = "Student Name = " + strStudentName
    
    
    'create message for score
    message = "Student Score = " & sngScore
    students = ";sngScore"
    
    'Store award variable
    Dim strAward As String
    
    'create award message
    strAward = "Grade Awarded = "
    
    'Creat statments for if statments
    Dim strmessage1 As String
    
    'if statment less than 10
    
    If intTotal < 10 Then
    strmessage1 = " Fail "
    End If
    
    If intTotal >= 10 And intTotal < 20 Then
    strmessge1 = " Pass "
    End If
    
    If intTotal >= 20 And intTotal < 30 Then
    strmessage1 = " Merit "
    End If
    
    If intTotal > 30 Then
    strmessage1 = " Distinction "
    End If
    
    
    'print the message
    Debug.Print strmessage;
    Debug.Print intmessageNumber
    Debug.Print strmessageName
    Debug.Print message
    Debug.Print strAward;
    Debug.Print strmessage1
    
    
    
    End Sub
    And also what do you think to the codeing? its my first real program.


    Cheers Chris


  • #2
    New Coder
    Join Date
    Oct 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    o.k i have got it to loop, how ever, when it goes back, though it all, instead of whipeing everything clean it just adds the student score up every time you put a new student in, so say if i put in a student score of 40 and then on the next loop i put in a score of 5, it willsay 45, when it should only be five.

    how do i make is so it dosn't add it together, it just starts again.

    Chris

  • #3
    Regular Coder
    Join Date
    Oct 2004
    Location
    In front of this computer. (Where else?)
    Posts
    442
    Thanks
    0
    Thanked 0 Times in 0 Posts
    From what i (think i) see, you want to do this for a lot of students so after one is done another is done.
    The best way i would do that would be to put everything in a different sub something like

    Public Sub results()
    ... All your previous code goes here
    End sub

    Then i'd code the sub main like this
    Code:
    Public Sub main()
    dim strstatus as string
    strstatus = MsgBox("would you like to calculate student's scores?",4)
    if strstatus = "yes" then 
    results
    Else
    exit sub
    end if
    End sub

    Please forgive me if there are typos. This is a friends pc and it doesn't have vb so i can't verify ... but something like that should work.
    Tomorrow is the first day of the rest of your life... What have you done today?
    Tutorials
    Web: w3schools, htmldog General tips: Hardware, Networking

  • #4
    Regular Coder
    Join Date
    Oct 2004
    Location
    In front of this computer. (Where else?)
    Posts
    442
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Set the values of the integers to 0 at the beginning of the loop.
    Tomorrow is the first day of the rest of your life... What have you done today?
    Tutorials
    Web: w3schools, htmldog General tips: Hardware, Networking

  • #5
    New Coder
    Join Date
    Oct 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how do i do that?

    Chris

  • #6
    Regular Coder
    Join Date
    Oct 2004
    Location
    In front of this computer. (Where else?)
    Posts
    442
    Thanks
    0
    Thanked 0 Times in 0 Posts
    at the beginning of your loop put

    inttotal = 0
    intallyourotherintegers = 0

    that should do the trick
    Tomorrow is the first day of the rest of your life... What have you done today?
    Tutorials
    Web: w3schools, htmldog General tips: Hardware, Networking

  • #7
    New Coder
    Join Date
    Oct 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    cheers but when i put it in i get this duplication message appear.

    Code:
    Public Sub main()
    
    ' Store variables for intStudentNumber
    '                     strStudentName
    
    'Store the variables for intResults
    '                        intCount
    '                        intTotal
    '                        sngScore
    '                        message
    
    Do
    
    intTotal = 0
    
    'Student number and name
    Dim intStudentNumber As Integer
    Dim strStudentName As String
    'Store message
    Dim intmessageNumber As Integer
    Dim strmessageName As String
    'store message for "Studentnumber"
    Dim strmessage As String
    
    'Student Score
    Dim intResults As Integer
    Dim intCount As Integer
    Dim intTotal As Integer
    Dim sngScore As Single
    
    'Store message
    Dim message As String
    
    
    
    ' Get Student Number & Name
    
    intStudentNumber = InputBox(" Student Number ")
    
    strStudentName = InputBox("Please Student Name Here")
    
    
    
    'get input from user for the six values, add each to the total
    'note the layout of the code, indent the commands within the loop
    
    For intCount = 1 To 6
    intResult = InputBox("Please enter students results-")
    intTotal = intTotal + intResult
    Next intCount
    
    'Calculate average
    
    sngScore = intTotal
    
    'Create Message for strmessage
    strmessage = "Student Number = "
    
    'Create message for student number & name
    
    intmessageNumber = intStudentNumber
    strmessageName = "Student Name = " + strStudentName
    
    
    'create message for score
    message = "Student Score = " & sngScore
    students = ";sngScore"
    
    'Store award variable
    Dim strAward As String
    
    'create award message
    strAward = "Grade Awarded = "
    
    'Creat statments for if statments
    Dim strmessage1 As String
    
    'if statment less than 10
    
    If intTotal < 10 Then
    strmessage1 = " Fail "
    End If
    
    If intTotal >= 10 And intTotal < 20 Then
    strmessge1 = " Pass "
    End If
    
    If intTotal >= 20 And intTotal < 30 Then
    strmessage1 = " Merit "
    End If
    
    If intTotal > 30 Then
    strmessage1 = " Distinction "
    End If
    
    
    'print the message
    Debug.Print strmessage;
    Debug.Print intmessageNumber
    Debug.Print strmessageName
    Debug.Print message
    Debug.Print strAward;
    Debug.Print strmessage1
    
    Loop
    
    End Sub
    o.k there is the code again with the loop in it, alls i have done is put "do" at the beginning of the program and "loop" at the end of it, its fine how ever when i put in intTotal = 0 , thats when i get the duplication error. So where do i put it?

    thanks

    Chris

  • #8
    New Coder
    Join Date
    Oct 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    GOT IT, i did what you said but putting in intTotal = 0 at the end of the program after it had printed everything instead of at the beginning of the program.

    Cheers for helping me

    my first real program

    Chris


  • #9
    New Coder
    Join Date
    Oct 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    o.k ermmmmm.... i havn't got it Some times when i put a score in say a score that equals 11 says pass, then i put in a score that is say 22 which should appear as a merit, but dosen't, some times it still appears with what was givin in the last loop.

    does any one no why? and how i can sort it.

    Chris

  • #10
    New Coder
    Join Date
    Oct 2004
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts
    O.k ladies and gentlemen, i present you with the working program.

    I took all your advice and this is what i came up with, i chucked the If sataments and replaced them with Select Case, now i get the results right every time.



    Code:
    '****************************************************************************
    'Chris Cooper                                                              *
    '****************************************************************************
    'Software Development                                                       *
    '****************************************************************************
    'Program will determin whether a student got a fail,pass,mert or distinction*
    '****************************************************************************
    
    
    
    
    
    Public Sub main()
    
    ' Store variables for intStudentNumber
    '                     strStudentName
    
    'Store the variables for intResults
    '                        intCount
    '                        intTotal
    '                        sngScore
    '                        message
    
    
    ' Start of loop
    Do
    
    'Student number and name
    Dim intStudentNumber As Integer
    Dim strStudentName As String
    
    'Store message
    Dim intmessageNumber As Integer
    Dim strmessageName As String
    
    'store message for "Studentnumber"
    Dim strmessage As String
    
    'Student Score
    Dim intResults As Integer
    Dim intCount As Integer
    Dim intTotal As Integer
    Dim sngScore As Single
    
    'Store message
    Dim message As String
    
    
    ' Get Student Number & Name
    
    intStudentNumber = InputBox(" Student Number ")
    
    strStudentName = InputBox("Please Student Name Here")
    
    
    
    'get input from user for the six values, add each to the total
    'note the layout of the code, indent the commands within the loop
    
    For intCount = 1 To 6
    intResult = InputBox("Please enter students results-")
    intTotal = intTotal + intResult
    Next intCount
    
    'Calculate average
    
    sngScore = intTotal
    
    'Create Message for strmessage
    strmessage = "Student Number = "
    
    'Create message for student number & name
    
    intmessageNumber = intStudentNumber
    strmessageName = "Student Name = " + strStudentName
    
    
    'create message for score
    message = "Student Score = " & sngScore
    students = ";sngScore"
    
    'Store award variable
    Dim strAward As String
    
    'create award message
    strAward = "Grade Awarded = "
    
    'Creat statments for if statments
    Dim strmessage1 As String
    
    'if statment less than 10
    
    Select Case intTotal
    
        Case Is < 10: strmessage1 = "Fail"
        
        Case Is < 20: strmessage1 = "Pass"
      
        Case Is < 30: strmessage1 = "Merit"
      
        Case Is > 30: strmessage1 = "Distinction"
    
    End Select
    
    
    'print the message
    Debug.Print strmessage;
    Debug.Print intmessageNumber
    Debug.Print strmessageName
    Debug.Print message
    Debug.Print strAward;
    Debug.Print strmessage1
    
    'Put back to 0
    
    intTotal = 0
    
    ' Start the Program again
    
    Loop
    
    
    
    End Sub

    Cheers



    Chris

  • #11
    Regular Coder
    Join Date
    Oct 2004
    Location
    In front of this computer. (Where else?)
    Posts
    442
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, just seeing this now. Yes, Select Case is much better than if... then when you are using only one variable. Makes you coding easier and less headache. Wish u luck with VB..
    Cheers
    Tomorrow is the first day of the rest of your life... What have you done today?
    Tutorials
    Web: w3schools, htmldog General tips: Hardware, Networking


  •  

    Posting Permissions

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