none
Stuck on a school program RRS feed

  • Question

  • I'm working on a school programming project, where I have to create a binary to decimal, and vice versa converter, I have finished each of the individual parts, but putting it together has generated a couple of errors, mainly with the placement of Endsubs. Any help in making this program work would be much appreciated. I'm sorry that it isn't the tidiest of codes also. 

    Code:

                      

    starter()
    Sub starter
    TextWindow.WriteLine("Type bd for binary to decimal, or db for decimal to binary")

    choice = TextWindow.Read()

    If choice = "yes" Then
      start()

      elseif choice = "no" then 

      db()

      else 
      TextWindow.WriteLine("That is invalid, please try again")

      starter()

      EndIf





    Sub bd
    TextWindow.Writeline("Enter Binary Value one digit at a time - ")
    cbnum = 0
      sub start
       bin1 = TextWindow.Read() 
       EndSub
       sub start2
         bin2 = TextWindow.Read() 
         EndSub
       sub start3
         bin3 = TextWindow.Read() 
         EndSub
       sub start4
         bin4 = TextWindow.Read() 
         endsub
       sub start5
         bin5 = TextWindow.Read() 
         endsub
       sub start6
         bin6 = TextWindow.Read() 
         endsub
       sub start7
         bin7 = TextWindow.Read() 
         EndSub
       sub start8
         bin8 = TextWindow.Read() 
       EndSub

      If bin1 > 1 Then
      TextWindow.WriteLine("That is not a binary digit, please try again")
      start()
    Else
      start2()
      EndIf

        If bin2 > 1 Then
      TextWindow.WriteLine("That is not a binary digit, please try again")
      start2()
    Else
      start3()
    EndIf

    If bin3 > 1 Then
      TextWindow.WriteLine("That is not a binary digit, please try again")
      start3()
    Else
      start4()
    EndIf

    If bin4 > 1 Then
      TextWindow.WriteLine("That is not a binary digit, please try again")
      start4()
    Else
      start5()
    EndIf

    If bin5 > 1 Then
      TextWindow.WriteLine("That is not a binary digit, please try again")
      start5()
    Else
      start6()
    EndIf

    If bin6 > 1 Then
      TextWindow.WriteLine("That is not a binary digit, please try again")
      start6()
    Else
      start7()
    EndIf

    If bin7 > 1 Then
      TextWindow.WriteLine("That is not a binary digit, please try again")
      start7()
    Else
      start8()
    EndIf

    If bin8 > 1 Then
      TextWindow.WriteLine("That is not a binary digit, please try again")
      start8()
    Else
      nxt()
    EndIf


      Sub nxt

     b1 = bin1
     b2 = bin2
     b3 = bin3
     b4 = bin4
     b5 = bin5
     b6 = bin6 
     b7 = bin7
     b8 = bin8 

    TextWindow.Write(b1)
    TextWindow.Write(b2)
    TextWindow.Write(b3)
    TextWindow.Write(b4)
    TextWindow.Write(b5)
    TextWindow.Write(b6)
    TextWindow.Write(b7)
    TextWindow.Write(b8)



    cbnum = ( (b8 * 1) + (b7 * 1 * 2) + (b6 * 1 * 2 * 2) + (b5 * 1 * 2 * 2 * 2) + (b4 * 1 * 2 * 2 * 2 * 2) + (b3 * 1 * 2 * 2 * 2 * 2 * 2) + (b2 * 1 * 2 * 2 * 2 * 2 * 2 * 2) + (b1 * 1 * 2 * 2 * 2 * 2 * 2 * 2 * 2) )
    TextWindow.WriteLine("")
    TextWindow.WriteLine(cbnum)

    endsub

    Sub db
     count = 0
     bit = 0

     TextWindow.WriteLine("Enter a Decimal Number : ")
     Num = TextWindow.ReadNumber()

     If Num > 255 Then
       TextWindow.WriteLine("That number is too big")
       db()
       Else
     While Num > 0
       bit[count] = Math.Remainder(Num,2)
       Num = Math.Floor(Num / 2)
       count = count + 1
     EndWhile
     TextWindow.Write("In Binary - ")
     For test = Array.GetItemCount(bit) To 0 step - 1
         TextWindow.Write(bit[test])
       EndFor
        TextWindow.WriteLine("")
     EndIf
     db()
     EndSub


    • Edited by Dano298 Thursday, April 3, 2014 6:47 PM
    Thursday, April 3, 2014 6:44 PM

Answers

  • Well, at the end of the 2 subroutines.

      else 
        TextWindow.WriteLine("That is invalid, please try again")
        
        starter()
        
      EndIf
     
    EndSub 'HERE
    
    Sub bd
      TextWindow.Writeline("Enter Binary Value one digit at a time - ")
      cbnum = 0
    EndSub 'HERE
    
    sub start
      bin1 = TextWindow.Read() 
    EndSub
    

    Thursday, April 3, 2014 7:12 PM
    Moderator
  • Here is a simple Binary to decimal converter in the Graphics Window:

    ' Binary to decimal converter 2014-04-07 WhTurner
    GraphicsWindow.Height=200
    GraphicsWindow.DrawText(20,20,"Binary to decimal conversion      Click the buttons")
    For i=1 To 8
      b[i]=Controls.AddButton(0,270-30*i,100)
    EndFor
    dec=0
    GraphicsWindow.FontSize=20
    GraphicsWindow.DrawText(100,50,"Binary number")
    GraphicsWindow.PenColor="Red"
    GraphicsWindow.DrawText(290,50,"Decimal")
    GraphicsWindow.FontSize=20
    GraphicsWindow.BrushColor="Red"
    GraphicsWindow.DrawText(300,100,dec)
    Controls.ButtonClicked=klik
    
    Sub klik
      
      but= Controls.LastClickedButton
      bnum= Text.GetSubTextToEnd(but,7)
      dig=Controls.GetButtonCaption(b[bnum])
      If dig=0 Then
        Controls.SetButtonCaption(b[bnum],1)
        dec=dec+math.Power(2,bnum-1)
      Else
        Controls.SetButtonCaption(b[bnum],0)
        dec=dec-math.Power(2,bnum-1)
      EndIf
      GraphicsWindow.BrushColor="white"
      GraphicsWindow.FillRectangle(300,100,50,20)
      GraphicsWindow.FontSize=20
      GraphicsWindow.BrushColor="Red"
      GraphicsWindow.DrawText(300,100,dec)
    EndSub

    Try to add a decimal to binary part.


    Jan [ WhTurner ] The Netherlands



    Monday, April 7, 2014 1:04 PM
    Answerer

All replies

  • The subroutines 'starter' and 'bd' don't have an EndSub.
    Thursday, April 3, 2014 6:51 PM
    Moderator
  • I understand that part, where would be best too place them?
    Thursday, April 3, 2014 6:56 PM
  • Well, at the end of the 2 subroutines.

      else 
        TextWindow.WriteLine("That is invalid, please try again")
        
        starter()
        
      EndIf
     
    EndSub 'HERE
    
    Sub bd
      TextWindow.Writeline("Enter Binary Value one digit at a time - ")
      cbnum = 0
    EndSub 'HERE
    
    sub start
      bin1 = TextWindow.Read() 
    EndSub
    

    Thursday, April 3, 2014 7:12 PM
    Moderator
  • That's helped a lot, thanks
    Thursday, April 3, 2014 7:29 PM
  • Dano,

    Do you know the "format program" trick. Click anywhere in the source of the program with the right mouse button, and choose Format program.

    All the loops and subroutines are indented automaticaly. You can then see how far each goes. If all statements end up on the left margin, there is something wrong in your loops.


    Jan [ WhTurner ] The Netherlands

    Friday, April 4, 2014 9:35 AM
    Answerer
  • starter()
    Sub starter
    TextWindow.ForegroundColor="Yellow"  
    TextWindow.WriteLine("Type bd for binary to decimal, or db for decimal to binary(bd/db/exit)")
    TextWindow.ForegroundColor="White"
    
    choice = TextWindow.Read()
    
    If choice = "bd" Then
      bd()
    
      elseif choice = "db" then 
    
      db()
      elseif choice = "exit" then
        Program.End()
        
      else
      TextWindow.ForegroundColor="Red"
      TextWindow.WriteLine("That is invalid, please try again!")
    
      starter()
    
      EndIf
    
    EndSub
    
    
    
    Sub bd
    TextWindow.ForegroundColor="Yellow"  
    TextWindow.Writeline("Enter Binary Value one digit at a time - ")
    cbnum = 0
    start()
    EndSub
    sub start
      TextWindow.ForegroundColor="Yellow" 
      TextWindow.Write("A-")
       bin1 = TextWindow.Read() 
       EndSub
       sub start2
         TextWindow.ForegroundColor="Yellow" 
         TextWindow.Write("B-")
         bin2 = TextWindow.Read() 
       EndSub
       sub start3
         TextWindow.ForegroundColor="Yellow" 
           TextWindow.Write("C-")
         bin3 = TextWindow.Read() 
       EndSub
       sub start4
         TextWindow.ForegroundColor="Yellow" 
           TextWindow.Write("D-")
         bin4 = TextWindow.Read() 
       endsub
       sub start5
         TextWindow.ForegroundColor="Yellow" 
           TextWindow.Write("E-")
         bin5 = TextWindow.Read() 
       endsub
       sub start6
         TextWindow.ForegroundColor="Yellow" 
           TextWindow.Write("F-")
         bin6 = TextWindow.Read() 
       endsub
       sub start7
         TextWindow.ForegroundColor="Yellow" 
         TextWindow.Write("G-")
         bin7 = TextWindow.Read() 
       EndSub
       sub start8
         TextWindow.ForegroundColor="Yellow" 
           TextWindow.Write("H-")
         bin8 = TextWindow.Read() 
       EndSub
    
      If bin1 > 1 Then
      TextWindow.ForegroundColor="Red"
      TextWindow.WriteLine("That is not a binary digit, please try again!")
      start()
    Else
      start2()
      EndIf
    
      If bin2 > 1 Then
      TextWindow.ForegroundColor="Red"
      TextWindow.WriteLine("That is not a binary digit, please try again!")
      start2()
    Else
      start3()
    EndIf
    
    If bin3 > 1 Then
      TextWindow.ForegroundColor="Red"
      TextWindow.WriteLine("That is not a binary digit, please try again!")
      start3()
    Else
      start4()
    EndIf
    
    If bin4 > 1 Then
      TextWindow.ForegroundColor="Red"
      TextWindow.WriteLine("That is not a binary digit, please try again!")
      start4()
    Else
      start5()
    EndIf
    
    If bin5 > 1 Then
      TextWindow.ForegroundColor="Red"
      TextWindow.WriteLine("That is not a binary digit, please try again!")
      start5()
    Else
      start6()
    EndIf
    
    If bin6 > 1 Then
      TextWindow.ForegroundColor="Red"
      TextWindow.WriteLine("That is not a binary digit, please try again!")
      start6()
    Else
      start7()
    EndIf
    
    If bin7 > 1 Then
      TextWindow.ForegroundColor="Red"
      TextWindow.WriteLine("That is not a binary digit, please try again1")
      start7()
    Else
      start8()
    EndIf
    
    If bin8 > 1 Then
      TextWindow.ForegroundColor="Red"
      TextWindow.WriteLine("That is not a binary digit, please try again!")
      start8()
    Else
      nxt()
    EndIf
    
    
      Sub nxt
    
     b1 = bin1
     b2 = bin2
     b3 = bin3
     b4 = bin4
     b5 = bin5
     b6 = bin6 
     b7 = bin7
     b8 = bin8 
    TextWindow.ForegroundColor="Green"
    TextWindow.Write(b1)
    TextWindow.Write(b2)
    TextWindow.Write(b3)
    TextWindow.Write(b4)
    TextWindow.Write(b5)
    TextWindow.Write(b6)
    TextWindow.Write(b7)
    TextWindow.Write(b8)
    
    cbnum = ( (b8 * 1) + (b7 * 1 * 2) + (b6 * 1 * 2 * 2) + (b5 * 1 * 2 * 2 * 2) + (b4 * 1 * 2 * 2 * 2 * 2) + (b3 * 1 * 2 * 2 * 2 * 2 * 2) + (b2 * 1 * 2 * 2 * 2 * 2 * 2 * 2) + (b1 * 1 * 2 * 2 * 2 * 2 * 2 * 2 * 2) )
    TextWindow.WriteLine("")
    TextWindow.WriteLine(cbnum)
    starter()
    endsub
    
    Sub db
     count = 0
     bit = 0
     TextWindow.ForegroundColor="yellow"
     TextWindow.WriteLine("Enter a Decimal Number(0-255): ")
     Num = TextWindow.ReadNumber()
    
     If Num > 255 Then
       TextWindow.ForegroundColor="Red" 
       TextWindow.WriteLine("That number is too big!(0-255)")
       db()
       Else
     While Num > 0
       bit[count] = Math.Remainder(Num,2)
       Num = Math.Floor(Num / 2)
       count = count + 1
     EndWhile
     TextWindow.Write("In Binary - ")
     For test = Array.GetItemCount(bit) To 0 step - 1
         TextWindow.Write(bit[test])
       EndFor
        TextWindow.WriteLine("")
     EndIf
     starter()
     EndSub
    this is the running colorful code
    Friday, April 4, 2014 8:49 PM
  • Here is a simple Binary to decimal converter in the Graphics Window:

    ' Binary to decimal converter 2014-04-07 WhTurner
    GraphicsWindow.Height=200
    GraphicsWindow.DrawText(20,20,"Binary to decimal conversion      Click the buttons")
    For i=1 To 8
      b[i]=Controls.AddButton(0,270-30*i,100)
    EndFor
    dec=0
    GraphicsWindow.FontSize=20
    GraphicsWindow.DrawText(100,50,"Binary number")
    GraphicsWindow.PenColor="Red"
    GraphicsWindow.DrawText(290,50,"Decimal")
    GraphicsWindow.FontSize=20
    GraphicsWindow.BrushColor="Red"
    GraphicsWindow.DrawText(300,100,dec)
    Controls.ButtonClicked=klik
    
    Sub klik
      
      but= Controls.LastClickedButton
      bnum= Text.GetSubTextToEnd(but,7)
      dig=Controls.GetButtonCaption(b[bnum])
      If dig=0 Then
        Controls.SetButtonCaption(b[bnum],1)
        dec=dec+math.Power(2,bnum-1)
      Else
        Controls.SetButtonCaption(b[bnum],0)
        dec=dec-math.Power(2,bnum-1)
      EndIf
      GraphicsWindow.BrushColor="white"
      GraphicsWindow.FillRectangle(300,100,50,20)
      GraphicsWindow.FontSize=20
      GraphicsWindow.BrushColor="Red"
      GraphicsWindow.DrawText(300,100,dec)
    EndSub

    Try to add a decimal to binary part.


    Jan [ WhTurner ] The Netherlands



    Monday, April 7, 2014 1:04 PM
    Answerer