0

After typing 1 the program does this command printf("Janela ou porta?(j/p)"); and then closes for no apparent reason

#include <stdio.h>
#include<time.h>
#include<stdlib.h>

int main(){

    int resposta;
    char resp;
    ola:

    printf("Janela ou porta?(j/p)");

    scanf("%c",&resp);

    if (resp=='p'){

        printf("Go back(Type 1)");

        do{

            scanf("%d",&resposta);

        }while(resposta!=1);
        goto ola;

    }
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
tomhoq
  • 59
  • 1
  • 6
  • 1
    Also, I suggest you avoid "goto" usage unless there are very specific optimization requirements. – Sven Nilsson Dec 05 '20 at 15:06
  • If you don't see the prompt until *after* you enter the character, add `fflush(stdout)` after the `printf()` statement. – Weather Vane Dec 05 '20 at 15:14
  • Also, you'll need to change `scanf("%c",&resp);` to `scanf(" %c",&resp);` note the added space. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Some explanation: most of the format specifiers for `scanf` automatically filter leading whitespace, but `%c` and `%[]` and `%n` do not. Adding a space in front of the `%` instructs `scanf` to filter leading whitespace here too. – Weather Vane Dec 05 '20 at 15:16

1 Answers1

1

When you input "1", "1" != "p" so you will never get to your while statement, you also won't trigger the goto statement. Like the other answer said, your program just ends.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Andrejcc
  • 134
  • 8