...

View Full Version : Visual Basic, Loops?



CooperTheGr8
11-03-2004, 05:43 PM
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.





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

:cool:

CooperTheGr8
11-03-2004, 05:51 PM
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

tboss132
11-03-2004, 05:59 PM
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


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.

tboss132
11-03-2004, 06:01 PM
Set the values of the integers to 0 at the beginning of the loop.

CooperTheGr8
11-03-2004, 06:49 PM
how do i do that?

Chris

tboss132
11-03-2004, 06:52 PM
at the beginning of your loop put

inttotal = 0
intallyourotherintegers = 0

that should do the trick

CooperTheGr8
11-03-2004, 07:00 PM
cheers but when i put it in i get this duplication message appear.



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

CooperTheGr8
11-03-2004, 07:05 PM
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 :D

Chris

:thumbsup:

CooperTheGr8
11-03-2004, 07:29 PM
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

CooperTheGr8
11-03-2004, 08:23 PM
O.k ladies and gentlemen, i present you with the working program. :D :D

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.





'****************************************************************************
'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

:thumbsup:

Chris

tboss132
11-04-2004, 12:00 PM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum