當我在case 1
(換句話說,在一個while
循環後)呼叫case 2
時,下面的代碼會產生兩個笑臉。然而printSentence();
的工作原理應該是在case 1
。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
.
我不知道什麼是這裏的問題,感謝您的幫助..
'句子'在每個循環後似乎都不會被清除。 – Blender 2011-12-23 21:16:47
哦!不要使用'gets()'。它不能安全使用,並已從最新的C標準(C11)中刪除。 – pmg 2011-12-23 21:30:08