0

I have this code that Ive spent too much time on and I cant figure. Its supposed to get the averages of the numbers and then output the answer.I input the name and then when I type in the numbers it only takes 1 and after that i cannot continue. Im a beginner in small basic and am having a hard time understanding this.I know that visual basic is more advanced so im trying to understand the simple stuff before I get into visual basic. If I can get some advice it would be greatly appreciated.

total=0
count=0
TextWindow.WriteLine("What is the students name? :")
name=TextWindow.Read()

While name<>""
TextWindow.Write(" Enter the grades :")
grades=textwindow.ReadNumber()
While grades<>""
total= total+grades
count=count+1
EndWhile
TextWindow.Write(name+ "average is" +total/grades)
TextWindow.WriteLine("Enter the name of another student or press enter to exit :")
name=textwindow.Read()
user3819134
  • 3
  • 1
  • 4
  • 1
    You should try to make it a habit to indent all the code blocks inside `for`, `while`, `if` and other control statements. This will makes it easier to spot the error. – Bas Swinckels Jul 14 '14 at 08:28
  • Smallbasic provides helper to format your code after the fact. In the editor right click the mouse and click "Format Program". All of your indenting will be reworked ot the standard two spaces. – codingCat Jun 04 '15 at 21:01

3 Answers3

1

I can't test atm but i think you are missing a EndWhile at the end of the code. ( you use 2 loops but only end 1)

  • creating this program in Visual basic would actually be easier becaus there is way more information on VB then there is on small basic.
dwana
  • 413
  • 3
  • 13
  • yes i have downloaded visual studio but it seems intimidating because it has so much stuff. I have tried the typical hello world test in that and for the most part i think its the same maybe only a litte bit more advanced the small basic – user3819134 Jul 14 '14 at 08:22
  • My advise: Ignore all the extras for start, just find out where to type the code and how to execute it. If you are still intimidated, try making MS word or MS excel macro's. – dwana Jul 14 '14 at 08:43
0

You do need a endwhile, but do not use a second while- this results in an infinite loop. I have fixed the code for you:

total=0
count=0
TextWindow.WriteLine("What is the students name? :")
name = TextWindow.Read()

While name<>""
  TextWindow.Write(" Enter the grades :")
  grades = textwindow.ReadNumber()
  If grades > 0 Then 
    total= total+grades
    count=count+1
  Else
    Goto end
  EndIf
EndWhile
end: 
TextWindow.WriteLine(name+ " average is " +total/count)
TextWindow.WriteLine("Enter the name of another student or press enter to exit :")
name = textwindow.Read()

Hope this helps.

vard
  • 1
0

I've fix ur problem!, instead of it going infinite waiting for the grades to be less than 0 to show the average. I gave it a condition for the count, if count equals to 3 (3 terms (since its about school)).Then it will stop to show the average of the student marks in the year (3 terms).

total=0
count=0
TextWindow.WriteLine("What is the students name? :")
name = TextWindow.Read()

While name<>""
  TextWindow.Write(" Enter the grades :")
  grades = textwindow.ReadNumber()
  If grades > 0 Then 
    total= total+grades
    count=count+1
  Else
    Goto end
  EndIf
  If count= 3 then
    Goto end
    EndIf
EndWhile
end: 
TextWindow.WriteLine(name+ " average is " +total/count)
TextWindow.WriteLine("Enter the name of another student or press enter to exit :")
name = textwindow.Read()