Function ends before asking for scanf value

Asked

Viewed 74 times

0

Can anyone figure out why when in the function accepting users I call main(), main() does not scan? That is, when I call the main again it does not let me choose any option and immediately closes the program. Thanks

void main(){

int j=0;
char opcao;

FILE *pedidos;

pedidos=fopen("../Projeto/Login/Users/pedidos", "r");


char leitor[100000];                             //listar o primeiro pedido de novo registo

    for(int i=0; i<5; i++){
        fgets(leitor,10000,pedidos);
        printf("%s", leitor);
    }
    printf("Pretende autorizar o registo deste utilizador? (y/n):");
    scanf("%c", &opcao);

    switch(opcao){
        case 'y':
        aceitarUtilizadores();
        break;
        case 'n':
        //rejeitarUtilizador();
        break;
    }


    }
void aceitarUtilizadores(){

char leitor[100];

FILE*ativos, *pedidos, *temp;

pedidos=fopen("../Projeto/Login/Users/pedidos", "r");

ativos=fopen("../Projeto/Login/Users/UserData", "a");

temp=fopen("../Projeto/Login/Users/temp", "w");

for(int i=0; i<5;i++){ 

        fgets(leitor,100,pedidos);

        fputs(leitor,ativos);
    }

    while((fgets(leitor,100,pedidos))!=NULL){ //escreve os restantes pedidos para um ficheiro temporario
        fputs(leitor,temp);
    }
    fclose(pedidos);
    fclose(ativos);
    fclose(temp);
    temp=fopen("../Projeto/Login/Users/temp","r");
    pedidos=fopen("../Projeto/Login/Users/pedidos","w"); //abre o ficheiro dos pedidos apagando o seu conteudo

    while((fgets(leitor,100,temp))!=NULL){ //copia os pedidos que faltam ser analisados para o ficheiro de pedidos
        fputs(leitor,pedidos);
    }
    remove("../Projeto/Login/Users/temp");
    fclose(pedidos);
    pedidos=fopen("../Projeto/Login/Users/pedidos", "r");
    rewind(pedidos);
    system("clear");
    if((fgets(leitor,100,pedidos))!=NULL){
        fclose(pedidos);
        main();
    }



    fclose(pedidos);
}
  • I don’t quite understand your question. Do you want the program to go back to 'main()' after returning from the accept()' function, asking whether or not the user wants to accept the registration? In other words, you want the program to go back on line: printf("Do you want to authorize the registration of this user? (y/n):"); ???

  • Hello, I didn’t quite understand your question, either, when compiling what was the mistake? I may be wrong, but if you are using Windows for example it does not accept the system("clear") command, maybe that is it, but send the error or clear your doubts. Valeuu

  • Figboy tried to trade the scanf for getch or getche?

2 answers

0

printf("Pretende autorizar o registo deste utilizador? (y/n):");
scanf("%s", &opcao);

recent friend had a problem similar to yours, resolved by changing in scanf("%c") to scanf("%s").

It don’t hurt to try there, good luck.

0


Sometimes there is a kind of "garbage" in the keyboard buffer that is when you press enter, then when you enter the next scanf it receives this enter again and for it is as if it had already inserted a value, to solve you can use this line before the scanf fflush(stdin); (In windows, Linux is another command, if I’m not mistaken) or just give a space inside the quotes of scanf which also works: scanf(" %d", &seuInt);, by default I always give this space inside the scanf so you don’t have to use the fflush, use the fflush just before getchar()

  • 1

    That’s exactly what was happening, when changing scanf("%c", &option); to scanf(" %c", &option); solved the problem

  • I’m glad I could help

Browser other questions tagged

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