Code:
/* Quick Y/N input example
Very simple program. We declare a character variable. Then we begin an infinite loop,
and prompt the user. After tossing the first letter of the user's input into the variable
via the getchar() function, we check the value of the input to see if it is a Y or N.
If it is a Y then we break the loop and continue.
If it is a N then you deal with it however you want. In this case we exit the program. Note
that we check for the lower (a) and uppercase (A) versions of the letter, as they are
totally different values.
If it is meaningless input then the loop repeats, prompting the user for input again.
There are a lot of things I would do to make this program a lot better (error checking,
clearing out the keyboard buffer, etc.), but for the sake of simplicity this gets the
point across.
*/
#include <stdio.h>
int main()
{ char YesNo;
for (;;) /* Infinite loop. */
{ printf ("Continue? (y/n): ");
YesNo=getchar();
if (YesNo == 'y' || YesNo == 'Y') break; /* Note - || means "or" */
if (YesNo == 'n' || YesNo == 'N') return 1;
}
printf("Continue program here.\n");
return 0;
}
While I don't want to do your homework for you, the best way to learn is to look at code.