Scanf trouble
Hello Experts. Could someone save me from a fit of anger that will register on
the Richter scale please. Below I have an example function. The problem I'm
having seems small, however it appears to defy logic at least to me.
I want to use the variable Question to ask the user if the while loop is to
be repeated again. The function goes through the first time and without even
activating the scanf, terminates the program. As you can see I gave Question
the value to continue the loop right away but during debug the value shows a
linefeed value. How can I get scanf to stop and take the answer y or n?
#include <stdio.h> main () {
FILE *data;
char Question = 'y';
if (( data = fopen ("Name", "a+")) == (FILE *) NULL ){
fprintf (stderr, " Can't open database!\n");
exit (1);
}
while (Question == 'y' || Question == 'Y'){
/* Function blah blah */
/* I even tried putting Question = 'y'; or = NULL in the middle of
the while loop without luck. */
printf ("Continue y/n ? ");
scanf ("%c", &Question);
} fclose (data); } Thanks for saving my sanity…well actually you might
be too late there.
Henry