Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Visual Basic Help

1. ## Visual Basic Help

We have this coursework on Visual Basic where we have to make a histogram (through "*") for a group of marks.

This is my code:

Module Module1

Sub Main()

Dim Mark, Bar1, Bar2, Bar3, Bar4, Fail, Pass, Total, Highest, Lowest, Range, Average As Integer
Dim Value As String

Console.WriteLine("Enter the marks. [Enter '101' to end.]")
Do While Mark <= 101
If IsNumeric(Value) = True Then
Mark = Value
If Mark >= 101 Then
Exit Do
End If
If Mark > Highest Then Highest = Mark
If Mark < Lowest Then Lowest = Mark
Range = Highest - Lowest
Average += Mark
If Mark >= 0 And Mark <= 29 Then
Bar1 += 1
Fail += 1
Console.WriteLine("Catagory one: 0-29")
ElseIf Mark >= 30 And Mark <= 39 Then
Bar2 += 1
Fail += 1
Console.WriteLine("Catagory one: 30-39")
ElseIf Mark >= 40 And Mark <= 69 Then
Bar3 += 1
Pass += 1
Console.WriteLine("Catagory one: 40-69")
ElseIf Mark >= 70 And Mark <= 100 Then
Bar4 += 1
Pass += 1
Console.WriteLine("Catagory one: 70-100")
End If
Total += 1
Console.WriteLine("Total number of students: " & Total)
Else
MsgBox("Please enter a digit.")
End If
Loop
Average /= Total

Console.WriteLine("Histogram of the marks:")

Console.Write("00-29 ")
For i = 1 To Bar1
Console.Write("*")
Next
Console.WriteLine()
Console.Write("30-39 ")
For i = 1 To Bar2
Console.Write("*")
Next
Console.WriteLine()
Console.Write("40-69 ")
For i = 1 To Bar3
Console.Write("*")
Next
Console.WriteLine()
Console.Write("70-100 ")
For i = 1 To Bar4
Console.Write("*")
Next
Console.WriteLine()
Console.WriteLine("Number of passes: " & Pass)
Console.WriteLine("Number of fails: " & Fail)
Console.WriteLine("Highest mark: " & Highest)
Console.WriteLine("Lowest mark: " & Lowest)
Console.WriteLine("Range: " & Range)
Console.WriteLine("Average mark: " & Average)
Console.WriteLine()
End Sub

End Module

---------------

The only problem is the "Lowest" stays on 0 and won't take the lowest mark entered. Please help.

2. End If
If Mark > Highest Then Highest = Mark
If Mark < Lowest Then Lowest = Mark
Range = Highest - Lowest
Try either putting a end if or else if in between these conditions.

such as
If Mark > Highest Then Highest = Mark
End If
If Mark < Lowest Then Lowest = Mark
End If

3. Originally Posted by nojtb
Try either putting a end if or else if in between these conditions.

such as
If Mark > Highest Then Highest = Mark
End If
If Mark < Lowest Then Lowest = Mark
End If
Still doesn't work.

4. first remove this...
Do While Mark <= 101
If IsNumeric(Value) = True Then
Mark = Value
If Mark >= 101 Then
Exit Do
End If
and change it to

Code:
```Do While Mark <= 101
Mark = Int32.Parse(Value)```
I think you code is confusing you logic wise- but the reason it is always 0 is because you are assuming that they will always enter a positive number and you never set Lowest initially, so Lowest is 0 and if you are evaluating @ < Lowest you will need to enter a negative number to hit that statement

I also think your if statements are nested wrong- for example let's pretend you enter your "high" range for 30, then you want to enter your low range (let's say 15)- well first the High range eval has to pass this will not happen because 15 > 30 is false so therefore the next if statement (Mark < Lowest) will never hit

5. When you initialize Lowest it will be treated as having a value of 0, which will always be lower than any positive number.
Insert e.g `Lowest=1000` anywhere before you make the first comparison.

What value you set it to is unimportant, as long as it is higher than the highest value that it can have after a comparison

6. Originally Posted by lerura
When you initialize Lowest it will be treated as having a value of 0, which will always be lower than any positive number.
Insert e.g `Lowest=1000` anywhere before you make the first comparison.

What value you set it to is unimportant, as long as it is higher than the highest value that it can have after a comparison
Still doesn't work.

7. Originally Posted by Keejee
Still doesn't work.
try reading my post- especially since lerura spammed off my post anyways-

I think you code is confusing you logic wise- but the reason it is always 0 is because you are assuming that they will always enter a positive number and you never set Lowest initially, so Lowest is 0 and if you are evaluating @ < Lowest you will need to enter a negative number to hit that statement
also read the next part I said because even if you initialize Lowest to something other than 0, you still have an issue with your if statements
for example let's pretend you enter your "high" range for 30, then you want to enter your low range (let's say 15)- well first the High range eval has to pass this will not happen because 15 > 30 is false so therefore the next if statement (Mark < Lowest) will never hit
let's apply it to initializing lowest to something other than 0, say 10... now you enter 8, highest gets set to 8 as well as lowest because of the way logic is set up.... BUT now try and set it to something lower than 8 (say 7), it will fail on the high check (7>8 evaluates false) and never hits lowest check

VB is not like most other languages... for example javascript and C# and Java (syntax aside) this would work just fine
Code:
```if (Mark > Highest)
Highest = Mark;
if (Mark < Lowest)
Lowest = Mark;```
this does not work the same for VB because VB ends if loops with an End If... The same statement above needs to be set up as
Code:
```If Mark > Highest Then
Highest = Mark
End If
If Mark < Lowest Then
Lowest = Mark
End If```
and then to further explain the first chunk of my post
Change...
Code:
```Do While Mark <= 101
....```
The reason you can remove that is because the exact nature of a do while loop will automatically evaluate against your constraint- there is no need to do a check during the loop. You should note though that doing a do while loop will always fire the loop once regardless of constraint because it checks against the constraint at the end- if you want to short circuit it use a while loop (enable it with a bool and flip the bool false somewhere in the loop) and if you want to stop it before it fires use a for loop (to evaluate @ the start of loop)

do while loops follow this logic
do something
and after you've done it check to see if you should do it again

for loops follow this logic
do something if you are allowed

and while loops follow this logic
do something until I tell you to stop

8. Originally Posted by alykins
... especially since lerura spammed off my post anyways ...
Explain why you see a related comment as spam.
I gave him relevant information that you didn't give him.

9. Originally Posted by lerura
Explain why you see a related comment as spam.
I gave him relevant information that you didn't give him.
You said the exact same thing that I did- and it was not a "we said it at the same time" or even close to the same time where we could have been responding in the same fashion and just not seen the other reply- there is a significant time span between our posts. and considering that you left out everything I said as well the OP did not do what I had said and hence their code is still not working, you negatively affected someone by your half* post- elaborating on what I had said or adding on to it or even pointing out a flaw is one thing; saying the same thing is Trolling/Spamming- Your post doesn't really fall into either Trolling or Spamming "perfectly" since you didn't spam a website and you didn't flame anyone, but your signature does have links so in the battle between Troll vs Spam I would say Spamming wins

Edit: And I quoted exactly how you said the same thing I did- if you still can't see it I will gladly explain it in more detail

10. I gave him an information to set the initial value of Lowest so high that it would not affect the outcome of the comparisons. IMO You didn't give him that information clear enough, so I did.

and for the terms:
Spamming: making a reply that is off topic/unneccesary.
Trolling: making a reply that have the purpose of angering other users.

11. I am well aware of the definitions hence
you post doesn't fit into trolling or spamming "perfectly"...
But w/e dude- I've said my opinion, you've said yours; neither of us will agree in the end and continuing to argue will not help the OP nor anyone else in the future-

@OP
If you feel like reading my responses and learning from them I am more than willing to continue to help you or clarify- if not then good luck

#### Posting Permissions

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