我正在尝试找到错误,但还是不明白。 我一直在调试和搜索,并找到了一些相关的主题,但只有我现在不需要的解决方案,我很好奇为什么这段代码不起作用:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define BUFFER 256
int main()
{
int missionCode;
char *desc = (char*)malloc(sizeof(char)*BUFFER);
do {
printf("Please enter the mission code (or -1 for exit): ");
scanf("%d", &missionCode);
fflush(NULL);
if (missionCode==-1)
return 1;
} while (missionCode>10);
do {
printf("Please enter a string:\n");
scanf("%[^\n]s", desc); //it doesn't stop here!
fflush(NULL);
if (!strcmp("exit",desc))
return 1;
} while (strlen(desc)<20);
printf("your string:\n%s", desc);
return 0;
}
第二个循环中的scanf\flushall出了问题,但我不知道是什么问题。 顺便说一下,这当然是C语言。
NULL
指针,即fflush(NULL)
? - Ganeshfflush(NULL)
刷新所有打开的输出流。 - Daniel Fischer