[英]Scanf causes C program to crash
This simple issue is causing my entire program to crash during the first input. 这个简单的问题导致我的整个程序在第一次输入时崩溃。 If I remove the input, the program works fine but once I add scanf into the code and enter the input the program crashes. 如果我删除输入,程序工作正常,但一旦我将scanf添加到代码并输入输入程序崩溃。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXEMPS 3
// stub program code
int main (void){
char answer;
do
{
printf("\n Do you have another(Y/N): ");
scanf("%c", answer);
}while(answer == 'Y' || answer == 'y');
getchar();
printf(" Press any key ... ");
return 0;
} // main
您必须将变量的地址传递给scanf:
scanf("%c", &answer);
Use "&answer". 使用“和答案”。 And get rid of the extraneous "fflush()" commands... 并摆脱无关的“fflush()”命令......
Better, substitute "answer = getchar ()". 更好,替换“answer = getchar()”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.