【发布时间】:2020-12-05 14:59:33
【问题描述】:
在输入 1 后,程序执行这个命令 printf("Janela ou porta?(j/p)");然后无缘无故关闭
#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;
}
}
【问题讨论】:
-
另外,我建议您避免使用“goto”,除非有非常具体的优化要求。
-
如果您在输入字符后 后才看到提示,请在
printf()语句后添加fflush(stdout)。 -
另外,您需要将
scanf("%c",&resp);更改为scanf(" %c",&resp);,注意添加的空格。请参阅scanf() leaves the newline char in the buffer。一些解释:scanf的大多数格式说明符会自动过滤前导空格,但%c和%[]和%n不会。在%前面添加一个空格会指示scanf在此处也过滤前导空格。