Continue "from while" of last point

1

There is a program of registrations that I have to make, I put the registration options and so on. All the code is saved in the vectors and they are as expected, but when I return to main() , it returns to number 1, since I had to initialize it in the first lines of the case.

I want to know some ways where I can continue from the last point, for example, I closed 10 registers, I return to the menu to perform other actions, but when I return to the customer registration option, I want it to continue from the last one ( 11 and etc.). Case 1.

int main(void) {

//Variaveis

int opcao;
clientes cadastro[100];
int i;
char sair_cad;



//Codigo

printf("\n\n1 - Cadastrar Novo Cliente\n");
printf("2 - Cliente\n");
printf("3 - Alterar Cliente\n");
printf("4 - Excluir Cliente\n");
printf("5 - Cadastrar Automovel\n");
printf("6 - Automovel\n");
printf("7 - Alterar Automovel\n");
printf("8 - Excluir Automovel\n");
printf("9 - Locacao\n");
printf("\n  Selecione uma opcao por favor: ");
scanf("%d", &opcao);
getchar();

switch (opcao)
{
case 1: // Codigo do Cadastro de Clientes

    i = 1;

    do
    {
        system("cls");
        cadastro[i].codigo_cliente = i;
        printf("\n\nCodigo do cliente:  %d", i);

        printf("\nNome:  ");
        scanf("%[^\n]s", &cadastro[i].nome);
        setbuf(stdin, NULL);
        printf("RG:  ");
        scanf("%s", &cadastro[i].rg);
        setbuf(stdin, NULL);
        printf("CPF:  ");
        scanf("%s", &cadastro[i].cpf);
        setbuf(stdin, NULL);
        printf("Endereco:  ");
        scanf("%[^\n]s", &cadastro[i].endereco);
        setbuf(stdin, NULL);
        printf("Data de Nascimento:  ");
        scanf("%s", &cadastro[i].data_nasc);
        setbuf(stdin, NULL);
        printf("Registro Habilitacao:  ");
        scanf("%s", &cadastro[i].registo_habilitacao);
        setbuf(stdin, NULL);
        getchar();
        system("cls");
        printf("\n\n\t\t\t\tCadastro Salvo com Sucesso!");
        printf("\n\n Deseja realizar outro cadastro: S/N ?:   ");
        scanf("%c", &sair_cad);
        setbuf(stdin, NULL);

        i = i + 1;


    } while (sair_cad != 'n');
    system("cls");

    return main();
    
asked by anonymous 19.10.2017 / 18:34

2 answers

2

The first thing you need to change is to eliminate the recursive flame that is going to end up causing problems. It may not cause anything in the exercise, but will learn wrong. This will burst the battery in an extended run. Solve this with a tie. Then just keep the counter initialization out of the box that will keep the number it was.

Actually a lot of stuff in this code is not a good one, but I will not try to mess with it.

int main(void) {
    int opcao;
    clientes cadastro[100];
    int i = 1;
    do {
        printf("\n\n1 - Cadastrar Novo Cliente\n");
        printf("2 - Cliente\n");
        printf("3 - Alterar Cliente\n");
        printf("4 - Excluir Cliente\n");
        printf("5 - Cadastrar Automovel\n");
        printf("6 - Automovel\n");
        printf("7 - Alterar Automovel\n");
        printf("8 - Excluir Automovel\n");
        printf("9 - Locacao\n");
        printf("\n  Selecione uma opcao por favor: ");
        scanf("%d", &opcao);
        getchar();
        switch (opcao) {
        case 1: // Codigo do Cadastro de Clientes
            do  {
                char sair_cad;
                system("cls");
                cadastro[i].codigo_cliente = i;
                printf("\n\nCodigo do cliente:  %d", i);
                printf("\nNome:  ");
                scanf("%[^\n]s", &cadastro[i].nome);
                setbuf(stdin, NULL);
                printf("RG:  ");
                scanf("%s", &cadastro[i].rg);
                setbuf(stdin, NULL);
                printf("CPF:  ");
                scanf("%s", &cadastro[i].cpf);
                setbuf(stdin, NULL);
                printf("Endereco:  ");
                scanf("%[^\n]s", &cadastro[i].endereco);
                setbuf(stdin, NULL);
                printf("Data de Nascimento:  ");
                scanf("%s", &cadastro[i].data_nasc);
                setbuf(stdin, NULL);
                printf("Registro Habilitacao:  ");
                scanf("%s", &cadastro[i].registo_habilitacao);
                setbuf(stdin, NULL);
                getchar();
                system("cls");
                printf("\n\n\t\t\t\tCadastro Salvo com Sucesso!");
                printf("\n\n Deseja realizar outro cadastro: S/N ?:   ");
                scanf("%c", &sair_cad);
                setbuf(stdin, NULL);
                i++;
            } while (sair_cad != 'n');
        case 2:
        } while (opcao != 0);
        system("cls");
    }
}
    
19.10.2017 / 18:46
0

The solution presented by Maniero is valid.

Your enemy here is called return main(); , it may seem interesting to save lines of code reusing the function, but in your case it turns out to be bad, after all you are trying to solve the whole problem only in one function ).

What happens to your program:

  • Starts in Main
  • Declares variables.
  • Initializes I to 1 (vectors start at zero, do not worry, it's a very common error at the beginning).
  • Make the registration until user wants to return to the menu.
  • Returns main ()
  • Declares Variables (since there is no reference, they start again at zero state) ....
  • 24.10.2017 / 14:41