Need Visual Basic help

D

Thread Starter

Doom_scorp

<p>What is wrong with this code

<pre>
Private sum as integer, N as integer
Private A as integer, B as integer, C as integer, D as integer, F as integer
N = InputBox(“How many marks are to be entered?, “Mark Categories”, 10)
for NumOfMarks = 1 to 10
Mark = InputBox(“Enter a student mark” “Mark Entry”, 87)
If Mark >= 80 and Mark <= 100 then
A = A + 1
Elseif Mark >= 70 and Mark < 80 then
B = B + 1
Elseif Mark >= 60 and Mark <=17 0 then
C = C + 1
Elseif Mark >= 50 and Mark <60 then
D = D + 1
Else
F = F1 + 1
Endif
next NumMarks
picOutput.print “Mark Categories”
picOutput.print “# of As = ”; A
picOutput.print “# of Bs =” ; B
picOutput.print “# of Cs =” ; C
picOutput.print “# of Ds =” ; D
picOutput.print “# of Fs =” > F
</pre>
 
D

Dick Krieger

Hi. Looks like your third "test-for-value" line has an error. I think it should be:

ElseIf Mark >= 60 and Mark <= 70 then

Try that. I think it will make that line work OK.

Good luck.

Dick
 
I found 4 errors ...

1. in line 3 (N = InputBox ...) you forgot your end quotes after "How many marks are to be entered?"

2. in line 5 (Mark = InputBox...) you forgot the comma between “Enter a student mark” and “Mark Entry”

3. in line 10 (Elseif Mark >= 60 and Mark <=17 0 ) the 17 0 should just be 70

4. in line 23 the > sign should be a ;
 
1. "for NumOfMarks = 1 to 10" should be
"for NumOfMarks = 1 to N" if you plan on having N other that 10;
2. "> next NumMarks" should be "> next NumOfMarks", othervise you "for" loop won't work;
3. Typo "F = F1 + 1", should be "F = F + 1
"

Sergei
 
D

Dick Krieger

<p>Hi again!

<p>Wups...missed one. The "F" mark variable "F" would never be incremented beyond initial zero because the increment statement
<pre>
Else
F = F1 + 1

should read
Else
F = F + 1
</pre>
<p>The "F = F1 + 1" might generate an error message, such as " unknown variable" because F1 has not been stated as an integer. Or, it might run, but F1 would be initialized as zero each time this line is run...making the F variable no more than 1. (F1 + 1 or 0 + 1)

<p>I also like to use only the "<" and ">" and not the "<=" combo as it makes two comparisons: first, is it less than and if true, is it equal to. For smaller code and a little more speed I use "<" with the lower end value 1 count higher. Example: instead of "<= 80", use "< 81". Gets the same results.

<p>The final error is in the print statement concerning the count for each grade. The "F" line reads:
<pre>
picOutput.print "# of Fs =" > F
</pre>
<p>I think this should be:
<pre>
picOutput.print "# of Fs =" ; F
</pre>
<p>Dick
 
This is incorrect. The <= combo does exactly the same comparison as > and then takes the result the other way around.

In any case, it's a bad idea to try to optimize like this; the correct method is to get the code working right first, then check whether speed is satisfactory, and if it isn't, *then* optimize, using a profiler.

(A profiler is a tool that tells you which parts of the code get used how often: first optimize the subroutines that get called most often and the loops with the largest counts.)

Jiri
--
Jiri Baum <[email protected]> http://www.csse.monash.edu.au/~jirib
MAT LinuxPLC project --- http://mat.sf.net --- Machine Automation Tools
 
Top