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.
#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
Solution 1:
You must pass the address of the variable to scanf:
scanf("%c", &answer);
Solution 2:
Use "&answer". And get rid of the extraneous "fflush()" commands...
Better, substitute "answer = getchar ()".