Continue "do while" from last point

Asked

Viewed 98 times

1

There is a registration program that I must perform, I put the registration options and etc. All the code is saved in the vectors and are as expected, but when I return to main(), it goes back to number 1, because I had to initialize it in the first lines of the case.

I want to know some paths where I can continue from the last point, for example, I closed 10 entries, return to the menu to perform other actions, but when I return the option of customer registration, I want it to continue from the last (11 and etc). Block of 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();
  • return main()? It’s infinite recursive your code?

  • 1

    Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

2 answers

2

The first thing you need to change is to eliminate the recursive flame that will end up causing problems. It may cause nothing in the exercise, but it will learn wrong. This will burst the stack in a prolonged run. Resolve this with a loop. Then just keep the counter startup out of the loop that will keep the number that was.

Actually a lot of stuff in this code isn’t good, but I’m not gonna try to touch 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");
    }
}

I put in the Github for future reference.

0

The solution presented by Maniero is valid.

Your enemy here is the call return main();, may seem interesting the idea of saving lines of code by 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 (the main one).

What happens to your program:

  1. Starts in Main
  2. Declares variables.
  3. Initializes I into 1 (vectors start at zero, don’t worry, it’s a very common error at first).
  4. Makes the registration until user wants to return to the menu.
  5. Main returns()
  6. Declares Variables (since there is no reference, they start again at zero) ....

Browser other questions tagged

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