case 2
을 case 1
(즉, while
루프 이후)이라고 말하면 아래 코드는 두 개의 웃는 얼굴을 만듭니다. 그러나 case 1
이어야하므로 printSentence();
이 작동합니다.루프 후 C 콘솔 응용 프로그램이 웃는 얼굴을 내 보냅니다.
#include <stdio.h>
#include <string.h>
char *enterSentence();
void printSentence(char *);
char *sentence;
int willContinue = 1;
main() {
while (willContinue) {
int a;
scanf("%d", &a);
switch (a) {
case 1:
getchar();
sentence = enterSentence();
printSentence(sentence);
break;
case 2:
getchar();
printSentence(sentence);
break;
case 3:
willContinue = 0; //exit
break;
}
}
}
char *enterSentence() {
char temp[999];
gets(temp);
return temp;
}
void printSentence(char *asd) {
puts(asd);
}
.
. //more code
.
나는 문제가 여기에 어떤 도움을 주셔서 감사 무엇인지 궁금 ..
'sentence'는 각 루프 후에 지워지지 않는 것처럼 보입니다. – Blender
오! 'gets()'를 사용하지 마십시오. 안전하게 사용할 수 없으며 최신 C 표준 (C11)에서 제거되었습니다. – pmg