How to make a program restart based on user input?

1

I'm doing that in a program that asks the user whether he wants to continue using the program or wants to quit. So far, I can already make the program close with the user by typing "close", but when I try to type "restart" nothing happens, just skip the line on the console.

How do I actually restart the program, or close and reopen the program quickly, or return it to the user's prompt?

In an example program, make it go back to the proper question of restarting or closing, or in a larger program, make it go back to the first question that requires the user to enter something.

In Small Basic, what would be the best class and method for what I'm talking about?

Here is the current program I have at hand:

TextWindow.WriteLine("Programa para fechar e recomeçar")
TextWindow.WriteLine("")

Sub ReiniciarOuFecharPrograma
inicio:
If resposta = "reiniciar" Then
  Goto inicio
ElseIf resposta = "fechar" Then
    Program.End()
  EndIf
EndSub

inicio = Program.GetArgument(20) 

TextWindow.WriteLine("Escreva 'fechar' para sair.")
TextWindow.WriteLine("Escreva 'reiniciar' para recomeçar o programa.")
TextWindow.WriteLine("")
TextWindow.WriteLine("Você quer fechar/reiniciar?")
TextWindow.WriteLine("")
resposta = TextWindow.Read()
ReiniciarOuFecharPrograma()

The class and method Program.GetArgument() would not be correct with what I want to do, right? They are interacting with the line 20% with%.

    
asked by anonymous 13.04.2016 / 23:44

1 answer

2

I imagine you're using an introductory language that no one uses, just to learn, right? Would not it be interesting to learn how to code in an organized way? Or do you want to do it anyway?

Learning from the Small Basic documentation is learning to have programming bugs. I'll improve the code to look more like a quality code. For example, do not use Goto . Do not use global variables (which can be seen by subroutines.) I keep the code simple and well-indented. Program.GetArgument(20) does not do what you think.

I do not know the language well, so I may have been wrong about something. For example, I do not know if it has a Boolean variable, so I preferred to use an integer like flag of the While loop. I made this loop which is the structured way to control the restart run.

The While functions as a If . It will execute the following block if the condition is true. The difference is EndWhile . If it were a EndIf certainly the next line to run would be the following line. In%% of it works as if it were EndWhile , but in a structured way, it will necessarily return to Goto . And there will be a new decision whether to execute the block or not.

In practice, this code will close every time you type While . I reproduced the behavior of the code in the question, although this did not make much sense.

continua = 1
While continua = 1
    TextWindow.WriteLine("Programa para fechar e recomeçar")
    TextWindow.WriteLine("")
    TextWindow.WriteLine("Escreva 'fechar' para sair.")
    TextWindow.WriteLine("Escreva 'reiniciar' para recomeçar o programa.")
    TextWindow.WriteLine("")
    TextWindow.WriteLine("Você quer fechar/reiniciar?")
    TextWindow.WriteLine("")
    resposta = TextWindow.Read()
    If resposta = "fechar" Then
        Program.End()
    ElseIf resposta <> "reiniciar" Then
        continua = 0
    EndIf
EndWhile
    
14.04.2016 / 00:14