Program runs subfunction instead of terminating

Just starting out? Need help? Post your questions and find answers here.
Banaticus
User
User
Posts: 21
Joined: Fri Oct 24, 2003 5:49 am
Location: Redlands, CA
Contact:

Program runs subfunction instead of terminating

Post by Banaticus »

Code: Select all

Enumeration
#codeco_window
#code_button
#decode_button
#text_close_box
#text_entry_box
#wavelength_box
#wavelength_title
#text_entry_title
#text_display_title
EndEnumeration

Dim code_array.b(95)
Dim alphabet.b(95)

For K = 0 To 94
  alphabet(K) = K + 32
Next K

If WindowID = OpenWindow(#codeco_window, 251, 133, 538, 447,  #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_SizeGadget | #PB_Window_TitleBar | #PB_Window_WindowCentered , "Coder/Decoder, by Sheep Enterprises")
  If CreateGadgetList(WindowID(0))
    ButtonGadget(#code_button, 10, 420, 80, 20, "Code")
    GadgetToolTip(#code_button, "Click here to code text in the upper box")
    ButtonGadget(#decode_button, 100, 420, 80, 20, "Decode")
    GadgetToolTip(#decode_button, "Click here to decode text in the upper box")
    StringGadget(#text_close_box, 10, 260, 510, 150, "")
    GadgetToolTip(#text_close_box, "Coded/decoded text is entered here")
    StringGadget(#text_entry_box, 10, 80, 510, 150, "")
    StringGadget(#wavelength_box, 10, 30, 190, 20, "",#PB_String_Numeric)
    TextGadget(#wavelength_title, 10, 10, 80, 20, "Wavelength")
    TextGadget(#text_entry_title, 10, 60, 220, 20, "Text Entry")
    TextGadget(#text_display_title, 10, 240, 160, 20, "Text Display")
    
    Repeat
      main_program:
      code_text$ = ""
      solved_code$ = ""

      Repeat
        random_starter = Val(GetGadgetText(#wavelength_box))
        RandomSeed(random_starter)
      Until random_starter <> 0

        For J = 0 To 94
          Repeat
            do_over = 0
            random_number=Random(94) + 32
            For K=0 To J
              If random_number = code_array(K)
                do_over = 1
              Break
              EndIf
            Next K
          Until do_over = 0
          code_array(J) = random_number
      Next J
     
    Until WaitWindowEvent()=#PB_Event_CloseWindow

  EndIf
EndIf
End      

code_section:
  OpenConsole()
  PrintN("Enter text to be coded:")
  text$ = Input()
  string_length = Len(text$)
  For K = 1 To string_length
    For J = 0 To 94
      alphabet_check$ = Chr(alphabet(J))
      code_check$ = Chr(code_array(J))
      If Mid(text$,K,1) = alphabet_check$
        code_text$ = code_text$ + code_check$
      EndIf
    Next J
  Next K
  PrintN("")
  PrintN(code_text$)
  CloseConsole()
Return
This is how I understand the code should be written. I merely left code_section: in for ease of reference while I wrote a version that worked with windows. As I understand it, it should open the window and then sit there, waiting for me to close the window. Instead, the window opens and closes itself almost immediately, and the console come up waiting for input, after "Enter text to be coded:".

Perhaps it is something wrong with my system. I have been unable to run DEW.Line. As soon as I click the little spaceship to start the program, it suddenly closes itself and ends.

But, why is the program opening up the console? I told the program to End after the If OpenWindow() loop -- it shouldn't be running the subsection of code after the End, right?
--BX
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Im not entirely sure what this proggy is supposed to be doing but I can immediately see 2 problems.

First, the statement
If WindowID = OpenWindow(#codeco_window ...... etc

causes a problem in that WidowwID is currently zero and yet
OpenWindow would normally return the window handle etc. Thus
your If statement will always result in a false condition etc.

Replace by If OpenWindow(#codeco_window ...... etc.

Secondly, (assuming you make the above correction) because the value of random_starter is always zero, the program is getting locked within the following Repeat ... Until loop indefinitely:

Repeat
random_starter = Val(GetGadgetText(#wavelength_box))
RandomSeed(random_starter)
Until random_starter <> 0

You need to re-write / re-order your main message loop and (presumably) you do not wish to execute the above loop until one of your button gadgets has been pushed.

Hope this helps.
Post Reply