How to go back at a specific point of the code in c++?

Asked

Viewed 81 times

-3

I need to go back into the code at three specific points and I don’t know how to do it. I need at different times of my code I need to return to one of the three points already established, but they are points that are not at the beginning of the code, so I don’t know how to do it because I need it to go back and run the rest of the code normally. For example, I’m running the 500 line of code and, depending on the user’s interaction, I need to return the 250 line, on the 700 line, he asks again if I want to go back to 250, and again on the 1100 line?

  • Ever tried to modularize your code? Maybe it’s what you need, so I understand you have a main with more than 1200 lines is this?

  • Yes, I have a main with more than 1200 lines, and at this point where I need it to go back, I need it to go through the whole code again after line 250, for example

  • Post something compilable, even if it’s not the whole program

1 answer

0

If you want to go back in code, you usually end up thinking about goto, however the use of the droplet for any program larger than a functionality test is not recommended or even constantly hated, because the droplet breaks the linearity that a program should have when it is read by a human, generating the Spaghetti code that makes everything more complicated to understand, maintain and adapt for use in more places, basically, is a bad practice not only of programming, as for you and everyone who are reading your code, what you need is not of GOTO, after all, not programming in Assembly, what you need is simply function, as modular as possible.

A very simple way to illustrate this is to imagine the following code where you need to ask for a number:

#include <stdio.h>
#include <windows.h>

main(){
    
    pontoInicial:
    
    int i;
    printf("\nDigite um numero: ");
    scanf("%i", &i);
    printf("%i\n", i);
    system("pause");
     
    //Muito código depois e você precisa voltar para pedir o número novamente
    //e rodar o código a partir dele apenas se usaria com o checkpoint feito anteriormente
    goto pontoInicial;
}

But this is very dirty code and it’s easy to imagine that it doesn’t work in the long run, much less for large programs, so it’s easy to create a function to do this and return what you want (or void without Return if you don’t need to):

#include <stdio.h>
#include <windows.h>

int retornaNumero(){
    int temp;
    printf("\nDigite um numero: ");
    scanf("%d", &temp);
    printf("%d\n", temp);
    system("pause");
    return temp;
}

main(){
        
    int i = retornaNumero();
    //Assim as coisas ficaram muito mais simples e você pode usar quaaantas vezes quiser e o
    //código continua seguindo seu fluxograma de maneira correta e tudo fica mais limpo e modular
}

Function useful and modularization of the code are very important for maintenance and ease of reading, highly recommend trying to do this with your code, and beware of forcing recursions

Browser other questions tagged

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