How do I restart a program based on user input?

Asked

Viewed 179 times

1

I’m doing that in a program that asks the user if he wants to continue using the program or if he 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 make the program actually restart, either by quickly closing and reopening the program, or by getting it back to the question that asks for user’s answer?

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 type 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 in 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() wouldn’t be the right ones with what I want to do, right? They’re interacting with line 20 resposta = TextWindow.Read().

1 answer

2


I imagine you’re using an introductory language that no one uses, just to learn, right? Wouldn’t it be interesting to learn to do the code in an organized way? Or do you want to do it anyway?

Learning from the Small Basic documentation is learning to have programming flaws. I will 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. O Program.GetArgument(20) doesn’t do what you think.

I don’t really know the language, so I might have missed something. For example, I don’t know if there is a boolean variable, so I prefer to use an integer like flag of the loop While. I made this loop which is the structured way of controlling the execution restart.

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

In practice this code will close whenever it is not typed reiniciar. I reproduced the behavior of the code in the question, although this doesn’t 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

I put in the Github for future reference.

  • Ah, thank you so much for the answer, bigown! I had an idea that it would be better to wear a bow tie While, but had no idea how to implement it and because of that I used Goto. And I also thought that Program.GetArgument(20) was what I wanted, but was proved wrong :)

  • But, if I want the program to close and reopen quickly if I type "restart", with the program coming back as open for the first time, what would be the steps to get to that restart method?

  • I don’t know a way to do this and I don’t even know if it makes sense to do it.

  • Just to have a beautiful design, so the console screen does not get full of phrases.

  • Clear the screen then, closing and starting again will be worse TextWindow.Clear()

  • Putz, I had no idea! Thank you again!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.